std/json
std/json
Section titled “std/json”std/json — JSON encode / decode over the built-in JsonValue enum. json_encode(v) walks the JsonValue tree and produces canonical JSON text. json_parse(s) is the inverse — recursive descent over a __JsonParser state struct, returning Option[JsonValue] (None on any malformed input). Numbers are stored verbatim as JNumber’s string payload; the responsibility for valid numeric formatting sits with the caller. String escapes are decoded on parse (\", \\, \/, \b, \f, \n, \r, \t, \uXXXX for BMP code points). Surrogate pairs aren’t combined yet. Migrated from the auto-injected prelude per docs/PRELUDE-TO-MODULES.md.
json_encode
Section titled “json_encode”pub function json_encode(v: JsonValue): stringjson_parse
Section titled “json_parse”pub function json_parse(s: string): Option[JsonValue]json_get
Section titled “json_get”pub function json_get(obj: JsonValue, key: string): Option[JsonValue]json_get(obj, key) — look up key in a JObject. Returns
None when obj isn’t a JObject or key isn’t present.
Foundation for the typed getters below.
json_get_string
Section titled “json_get_string”pub function json_get_string(obj: JsonValue, key: string): Option[string]json_get_string(obj, key) — typed-extract a JString.
Returns None when the key is missing or the value isn’t
a string.
json_get_i32
Section titled “json_get_i32”pub function json_get_i32(obj: JsonValue, key: string): Option[i32]json_get_i32(obj, key) — typed-extract a JNumber as i32.
Returns None when the key is missing, the value isn’t a
number, or the number doesn’t parse as an integer.
json_get_bool
Section titled “json_get_bool”pub function json_get_bool(obj: JsonValue, key: string): Option[boolean]json_get_bool(obj, key) — typed-extract a JBool.
json_get_array
Section titled “json_get_array”pub function json_get_array(obj: JsonValue, key: string): Option[JsonValue[]]json_get_array(obj, key) — typed-extract a JArray.
Returns the array’s element list directly so callers can
len() / index over it without further matching.
json_get_object
Section titled “json_get_object”pub function json_get_object(obj: JsonValue, key: string): Option[JsonValue]json_get_object(obj, key) — typed-extract a JObject as
a JsonValue (chain extracts through nested objects).
Doesn’t unwrap to Map[string, JsonValue] — the JsonValue
shape composes with the other json_get_* helpers, so
chains read more naturally as:
var addr = json_get_object(user, “address”); var city = json_get_string(addr.unwrap_or(JNull), “city”);
(The unwrap_or pattern works around chained-Option
unwrapping until lang grows a ?. operator.)
json_is_null
Section titled “json_is_null”pub function json_is_null(v: JsonValue): booleanjson_is_null(v) — true iff v is JNull. Convenience
over the bare match for the common nullability check.