@std/json

Overview

JSON parsing and serialization over the native tape parser.

JSON.parse turns a document into plain values: null decodes to null, true and false to Bool, numbers to Int or Float, strings to String, arrays to Array<Any>, and objects to opaque handles. Wrap an object handle in a Map with parseObject or asMap and read fields with Map.get.

Parsing builds one flat tape for the document. A vectorized structural indexer finds {, }, [, ], : and , outside string literals in 16-byte SIMD chunks with branch-free escaped-quote handling, scalars are stored unboxed in the tape, strings are slices of one string pool, and the whole document is a single buffer released with its owner. Keys come out in byte-sorted order and a repeated key keeps its last value, which is what makes stringify output canonical. get, has and len scan the pairs directly; set, delete, clear, keys and values materialize the document into a hash map first.

Numbers with no fraction and no exponent decode as Int up to 9223372036854775807, and every other number decodes as Float, larger integers included. Strings accept the \", \\, \/, \b, \f, \n, \r, \t and \uXXXX escapes, surrogate pairs included, and reject raw control bytes. Documents may nest 64 levels deep.

JSON.parse<T> and JSON.decode<T> read an object document straight into a Record or Class T, with no tape and no map: document keys are matched against declared fields by direct integer comparison, unknown keys are skipped without building values, missing keys decode as null, and nested objects and arrays decode to the same values parse yields.

Ownership: a parsed tree is freed when the Map owning its handle is freed. parseObject creates that owner, while asMap hands an existing handle to a new wrapper, so wrap a handle once and stop reading the value it came from.

When to use it

Reach for @std/json at every process boundary that speaks JSON: parsing request bodies and config files with JSON.parse, emitting responses with JSON.stringify, or pulling typed maps out with JSON.parseObject and JSON.asMap.

Capabilities

  • JSON.parse/stringify
  • parseObject to Map
  • asMap/asArray casts

Symbols

  • class JSON — JSON codec. A number with neither fraction nor exponent decodes to