encoding/json/src/json library
A pure-Dart replacement for dart:convert's jsonEncode / jsonDecode,
matching the official signatures (toEncodable, reviver).
With options: null (the default) both directions behave like vanilla
dart:convert. Two opt-in features exist, one per direction:
-
Encode - JsonEncodeOptions.bigIntEncoding (only for
BigIntvalues, and only when notoEncodableis given):BigIntEncoding.string: the BigInt is written as a JSON string.BigIntEncoding.intOrString: if the BigInt fits a Dartinton the current platform (signed 64-bit on the VM, +-(2^53-1) on the web) it is written as a plain JSON number, exactly likejsonEncode(int); otherwise it is written as a JSON string.
-
Decode - JsonDecodeOptions.badStringAsBinary: a JSON string whose bytes are not valid UTF-8 decodes as a raw
List<int>instead of throwing. Nothing else about decoding differs fromdart:convert(an oversized integer literal decodes to adouble, a lone\uD800escape decodes to a String holding that lone surrogate).
Extras kept from before: indent for pretty-printing and JsonBinary
for writing raw bytes back out (only when options is non-null).
Everything is hand-implemented over List<int> bytes (no
dart:typed_data), so it behaves identically on native and web.
Classes
- JsonBinary
-
Marks raw bytes that should be written back out as a JSON string where
each byte is one UTF-16 code unit (0..255) — the same shape malformed
binary-in-a-string payloads normally arrive in. Only used on the
encode side; decode returns a plain
List<int>for malformed strings (seeJsonDecodeOptions.badStringAsBinary). Wrap a decodedList<int>inJsonBinary(...)if you need to round-trip it back out losslessly. Like BigInt, this is only handled specially when you pass a non-nulloptionsand notoEncodable— see the library doc comment above. - JsonCodec
- JsonDecodeOptions
- JsonEncodeOptions
Enums
- BigIntEncoding
-
How a
BigIntis written byjsonEncode(encode side only).