decodeDocument function

Future<Object?> decodeDocument(
  1. String input, {
  2. int maxBytes = 100 * 1024 * 1024,
})

Bounded JSON decoder that rejects duplicate object keys and yields on large input. Unlike jsonDecode's reviver, duplicate names cannot disappear before validation.

Implementation

Future<Object?> decodeDocument(
  String input, {
  int maxBytes = 100 * 1024 * 1024,
}) async {
  if (input.length > maxBytes || utf8.encode(input).length > maxBytes) {
    invalid('Document size limit exceeded.');
  }
  final reader = _JsonReader(input);
  final value = await reader.value(0);
  reader.space();
  if (reader.index != input.length) invalid('Unexpected JSON suffix.');
  return value;
}