decodeJsonbStrings function

dynamic decodeJsonbStrings(
  1. dynamic input, {
  2. int maxDepth = defaultDecodeJsonbStringsMaxDepth,
})

Recursively walks input and decodes any String value that begins with { or [ as JSON, replacing it with the decoded Map or List.

Intended for Postgres jsonb columns: depending on the driver (and codec configuration), a jsonb value may arrive as either a pre-decoded Map/List or as a raw JSON String. Applying this once to a row before handing it to a generated Model.fromJson removes that difference so the model's field-level mappers see the same shape either way.

Strings that do not start with { / [ (after trimming), or that fail to parse, are returned unchanged — so non-jsonb text columns are unaffected.

maxDepth caps the recursion. Defaults to defaultDecodeJsonbStringsMaxDepth; once exhausted, the function stops recursing and returns the partially-decoded value at that node. This prevents a hostile or pathologically nested payload from overflowing the stack — a real concern when consuming jsonb from upstream systems.

Implementation

dynamic decodeJsonbStrings(
  dynamic input, {
  int maxDepth = defaultDecodeJsonbStringsMaxDepth,
}) {
  if (maxDepth < 0) {
    throw ArgumentError.value(maxDepth, 'maxDepth', 'must be non-negative');
  }
  return _decodeJsonbStrings(input, maxDepth);
}