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:

  1. Encode - JsonEncodeOptions.bigIntEncoding (only for BigInt values, and only when no toEncodable is given):

    • BigIntEncoding.string: the BigInt is written as a JSON string.
    • BigIntEncoding.intOrString: if the BigInt fits a Dart int on the current platform (signed 64-bit on the VM, +-(2^53-1) on the web) it is written as a plain JSON number, exactly like jsonEncode(int); otherwise it is written as a JSON string.
  2. 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 from dart:convert (an oversized integer literal decodes to a double, a lone \uD800 escape 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 (see JsonDecodeOptions.badStringAsBinary). Wrap a decoded List<int> in JsonBinary(...) if you need to round-trip it back out losslessly. Like BigInt, this is only handled specially when you pass a non-null options and no toEncodable — see the library doc comment above.
JsonCodec
JsonDecodeOptions
JsonEncodeOptions

Enums

BigIntEncoding
How a BigInt is written by jsonEncode (encode side only).