Skip to main content

Module json

Module json 

Source
Expand description

JSON codec. Ported from ~/dev/omnist/omnist/formats.py’s read_json/write_json/check_json.

§Depth guard reuse

read_json parses JSON text into a crate::document::Value, then builds a Doc via Doc::of – which calls crate::document::check_write_depth internally (see document.rs). write_json/check_json walk an already-built Doc (via Doc::to_grouped/Doc::root), whose every node was depth-checked at construction time – there is nothing left to re-guard on the way out, exactly the reasoning Doc::to_grouped’s own doc comment gives. So this module reuses the one shared depth guard transitively rather than adding a second (or third) copy.

§No native temporal type

Python’s JSON writer stringifies datetime.date/datetime.time values (JSON has no native temporal type) and records a temporal.stringified warning. This port’s crate::document::Scalar has no temporal variant at all (see document.rs’s module doc) – a date/time/datetime value is already a Scalar::Str holding its ISO spelling by the time it reaches this codec, so there is nothing left to adjust or report here. The only lossy JSON write left is NaN/Infinity/-Infinity, which JSON’s grammar has no token for.

§Integer digit cap (omnist-ts#54 / oml.rs precedent)

Live-checked against Python (omnist.formats.read_json): a JSON integer literal over 4300 digits raises ParseError (CPython’s sys.set_int_max_str_digits guard fires inside json.loads itself, before build_node ever sees a value); under the cap, arbitrary precision is accepted ('9' * 4300 reads as a plain Python int). This scanner applies the identical 4300-digit cap before attempting to parse the literal, mirroring oml.rs’s MAX_INT_DIGITS guard exactly (same constant, same “reject the digit run before conversion” shape). Because this port’s Scalar::Int is i64 (max ~19 digits), any literal over 19 digits fails as “out of range for a 64-bit integer” well before the 4300-digit cap would ever fire on its own – the same representational gap already documented in document.rs’s module doc for OML. The cap is kept anyway (dead in practice for i64, exactly like Python’s own limit for who never encounters it) purely to give a stable, specific error for egregiously long digit runs rather than a generic overflow message, and to keep this scanner structurally parallel to oml.rs’s.

Functions§

check_json
Report what writing JSON would adjust, without producing output.
read_json
Parse JSON text into a Doc.
write_json
Project a Doc to JSON text.