Skip to main content

Module json

Module json 

Source
Expand description

JSON — parse, stringify, and read back — as blue’s own surface.

Pure computation with no host imports, so this is part of the runtime unconditionally (the wasm32-unknown-unknown consumer keeps it). The semantics are the substrate’s — tatara-lisp-script’s json.rs is the reference, and these are deliberately the same shapes — adapted to what blue can actually reach:

json_parse(s)          → nil / bool / int / float / string / list / alist
json_stringify(v)      → string
json_get(doc, key)     → value at key, or nil   (doc = alist or Map)
json_get_or(doc, key, default) → value at key, or default

§Why objects are alists, not Maps

Value::Map is the representation that round-trips {} exactly — the substrate proved that fix, and it is kept for the empty case — but a Map has no reachable reader: every map primitive blue’s own surface could spell is kebab-case (hash-map-get), and - is an operator in blue, so a json_parse that produced Maps would hand blue a document it could not open. Non-empty objects therefore parse to an association list of [key value] 2-lists, which blue reads with car/cdr/nth. json_get is the Rust-side reader, total over both shapes.

The cost is the same ambiguity the substrate records: a JSON array whose every element is a 2-list with a string first still stringifies as an object. Round-trip is exact for the shapes the fleet emits; the Map/List split is where a future migration lands if the map reader becomes reachable.

Functions§

install_json_stdlib
Install blue’s JSON surface.
json_to_value
Convert a serde_json::Value into a Value. Objects become association lists of [key value] 2-lists; the EMPTY object is Value::Map, the one representation that can say “object with no entries” and round-trip.
value_to_json
Convert a Value back into a serde_json::Value. Closures and native functions collapse to null.