json
Parse JSON (RFC 8259) into CXDM values and emit CXDM values back to JSON. Supports two parse modes (strict and lenient) and three emit modes (canonical, pretty, compact). The data subset round-trips losslessly; CX elements emit idiomatically by default or via a lossless `named` encoding on demand. Distinct from the cross-format CLI conversion path — this is the in-program library form.
json:parse
[$json:parse] -> any — Strictly parse a JSON string into a CXDM value, rejecting trailing commas, comments, and NaN/Infinity.
[?lib 'cx-stdlib/json']
[$json:parse "42"]
42
json:parse-with-opts
[$json:parse-with-opts] -> any — Parse a JSON string with options controlling leniency, number mode, and depth/byte limits.
[?lib 'cx-stdlib/json']
[$json:parse-with-opts "[1, 2,]" {lenient: true}]
[1, 2]
json:parse-bytes
[$json:parse-bytes] -> any — Parse JSON from a byte buffer, stripping a UTF-8 BOM and auto-detecting UTF-16 with BOM.
[?lib 'cx-stdlib/json']
[?lib 'cx-stdlib/bytes']
[$json:parse-bytes [$bytes:from-string-utf8 "42"]]
42
json:emit
[$json:emit] -> string — Emit a CXDM value as canonical JSON: compact, with map keys sorted for deterministic output.
[?lib 'cx-stdlib/json']
[$json:emit 42]
'42'
json:emit-pretty
[$json:emit-pretty] -> string — Emit a CXDM value as pretty-printed JSON with two-space indent, preserving map insertion order.
[?lib 'cx-stdlib/json']
[$json:emit-pretty {a: 1}]
'{ "a": 1 }'
json:emit-with-opts
[$json:emit-with-opts] -> string — Emit a CXDM value to JSON with options for indent, key sorting, ASCII escaping, lossless elements, and NaN handling.
[?lib 'cx-stdlib/json']
[$json:emit-with-opts [empty] {lossless: true}]
'{"$tag":"empty","$attrs":{},"$children":[]}'
json:emit-bytes
[$json:emit-bytes] -> bytes — Emit a CXDM value as canonical JSON encoded to a UTF-8 byte buffer.
[?lib 'cx-stdlib/json']
[?lib 'cx-stdlib/bytes']
[$bytes:to-string-utf8 [$json:emit-bytes 42]]
'42'
json:parse-stream
[$json:parse-stream] -> [sequence any] — Parse a JSONL/NDJSON stream where each input element is one complete JSON value, yielding parsed values.
[?lib 'cx-stdlib/json']
[$json:parse-stream ("1", "2", "3")]
(1, 2, 3)
json:parse-many
[$json:parse-many] -> [sequence any] — Parse multiple whitespace-separated JSON values from one string using JSON-aware boundary detection.
[?lib 'cx-stdlib/json']
[$json:parse-many "1 2 3"]
(1, 2, 3)
json:emit-stream
[$json:emit-stream] -> [sequence string] — Emit a sequence of CXDM values as JSONL, yielding one newline-terminated record per value.
[?lib 'cx-stdlib/json']
[?let [= $r [$json:emit-stream (1, 2)]] [$count $r]]
2
json:is-valid
[$json:is-valid] -> bool — Report whether a string is valid JSON in strict mode, without building a value tree.
[?lib 'cx-stdlib/json']
[$json:is-valid "{\"a\": 1}"]
true
json:encode
[$json:encode] -> string — Serialize a value to canonical CX form (not JSON) — a module re-export scaffold outside the json surface.
# json:encode = CX canonical form via $format-canonical (NOT JSON); a §12.4.4
# module re-export scaffold (paired with the private encoder-not-reexported),
# not part of the json §3 surface — also exercised by conformance/code.cxd.
[?lib 'cx-stdlib/json']
[$json:encode {a: 1}]
'{a: 1}'