Skip to content

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.

pub function json_encode(v: JsonValue): string
pub function json_parse(s: string): Option[JsonValue]
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.

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.

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.

pub function json_get_bool(obj: JsonValue, key: string): Option[boolean]

json_get_bool(obj, key) — typed-extract a JBool.

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.

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.)

pub function json_is_null(v: JsonValue): boolean

json_is_null(v) — true iff v is JNull. Convenience over the bare match for the common nullability check.