Skip to main content

Module node

Module node 

Source
Expand description

Order-preserving JSON tree and the Gson-compatible writer (DRND-05).

Gateway-written JSON is Gson pretty output. Render passes files through as bytes; this module only decides whether a file may be rewritten (uuid injection in 21-07) and gives the secret lint (21-08) a tree to walk. A file is JsonClass::GatewayFormat when write(parse(bytes)) == bytes, otherwise JsonClass::Opaque and render never mutates it.

§The tree

Node keeps object members in source order (never sorted: the spec’s “sorted keys” is wrong, ROADMAP constraint) and keeps every number, true, false and null as its original lexeme (Node::Lit), so 2.50, 1.0E10, -0.0 and 18446744073709551616 survive unchanged. serde_json runs with raw_value only; preserve_order and arbitrary_precision stay off.

§The writer: Gson pretty, HTML-safe

2-space indent, ": " between key and value, one member or element per line, {} and [] for empty containers, no trailing newline. Strings and keys are escaped as Gson’s JsonWriter does with HTML-safe on (ESC(hhhh) below is the six characters backslash, u, four lowercase hex digits):

CharacterWritten as
" and \backslash-escaped
TAB, BS, LF, CR, FFbackslash then t, b, n, r, f
other code points below U+0020ESC(00hh)
< > & = 'ESC(003c) ESC(003e) ESC(0026) ESC(003d) ESC(0027)
U+2028, U+2029ESC(2028), ESC(2029)
everything else, DEL and non-ASCII includedraw UTF-8

Raw UTF-8 for non-ASCII is decision CD4 (21-LIVE-CAPTURES.md): the 8.3.6 gateway wrote Température ✓ unescaped.

Errors are Strings carrying serde_json’s line/column text or a key name, never a value (T-21-22).

Enums§

JsonClass
Whether render may rewrite a JSON file.
Node
An order-preserving JSON document node.

Constants§

JSON_MAX_BYTES
Per-file size cap for a .json member (16 MiB). Larger files are refused before any parse (T-21-19).
MAX_DEPTH
Deepest container nesting parse accepts (serde_json’s own default recursion limit). Checked by a linear scan before the recursive build, so hostile nesting can neither overflow the stack nor cost O(depth x size) (T-21-19).

Functions§

classify
Classify a .json member’s bytes and return its parsed tree.
parse
Parse one JSON document into a Node.
write
Write node in the gateway’s Gson pretty format (see the module doc).