jsonDecode static method

dynamic jsonDecode(
  1. Object source, {
  2. Object? reviver(
    1. Object? key,
    2. Object? value
    )?,
  3. JsonDecodeOptions? options,
})

source may be a String, or raw bytes (List<int>) e.g. straight off an HTTP response — passing bytes directly is what lets malformed-UTF-8 string fields fall back to a raw List<int> instead of throwing.

reviver, if given, is called bottom-up for every decoded value — reviver(key, value) where key is the object property name (String), the array index (int), or null for the top-level value — exactly like dart:convert's jsonDecode(source, reviver: ...).

options is fully optional: leave it null (the default) for behavior identical to dart:convert's jsonDecode - an oversized integer literal becomes a double and a malformed-UTF-8 string throws, same as dart:convert. Pass one to opt into the BigInt/binary-string features (see the library doc above).

Implementation

static dynamic jsonDecode(
  Object source, {
  Object? Function(Object? key, Object? value)? reviver,
  JsonDecodeOptions? options,
}) {
  final List<int> bytes;
  if (source is List<int>) {
    bytes = source;
  } else if (source is String) {
    bytes = StringUtils.encode(source);
  } else {
    throw CoreErrors.invalidOperationArguments(
      "jsonDecode",
      reason: 'Must be a String or List<int>',
    );
  }
  return jsonDecodeBytes(bytes, reviver: reviver, options: options);
}