Json
Json module: JSON encoding and decoding.
Types: JsonValue = Null | Bool(Bool) | Number(Float) | Str(String) | Array(List(JsonValue)) | Object(List((String, JsonValue)))
Note: Object uses an association list (ordered, allows duplicate keys). Key lookup uses the first occurrence.
Functions: parse(String) -> Result(JsonValue, String) to_string(JsonValue) -> String get(JsonValue, String) -> Option(JsonValue) get_in(JsonValue, List(String)) -> Option(JsonValue) encode_null() -> JsonValue encode_bool(Bool) -> JsonValue encode_number(Float) -> JsonValue encode_int(Int) -> JsonValue encode_string(String) -> JsonValue encode_array(List(JsonValue)) -> JsonValue encode_object(List((String, JsonValue))) -> JsonValue
Types
Functions
Attach an absolute byte offset to a DecodeError.
Render a DecodeError as $.users[3].id: expected Int, with the byte offset when known: $.id (byte 4096): expected Int.
Push step onto the front of a DecodeError's path, as a decoder does when it descends.
Construct a JSON array value.
Construct a JSON boolean value.
Construct a JSON number value from an Int.
Construct a JSON null value.
Construct a JSON number value from a Float.
Construct a JSON object value from a list of (key, value) pairs.
Construct a JSON string value.
Retrieve a field from a JSON Object by key. Returns Some(value) if found, or None otherwise. If jv is not an Object, returns None.
Json.get(Object([("x", Number(1.0))]), "x") -- Some(Number(1.0))
Json.get(Null, "x") -- NoneRetrieve a JSON Array element by 0-based index. Returns Some(value) if in range, or None otherwise (also None if jv is not an Array, or if i is negative).
Json.get_at(Array([Number(1.0), Number(2.0)]), 1)
-- Some(Number(2.0))
Json.get_at(Array([Number(1.0)]), 5)
-- NoneLook up a key in a JSON object's raw key-value list directly -- the list already bound by matching Object(kvs), rather than a JsonValue. Returns Some(value) if found, or None otherwise. Used by the generated from_json record decoder, which destructures Object(kvs) once and then looks up each field by name without re-matching the wrapper each time.
Json.get_field([("x", Number(1.0))], "x") -- Some(Number(1.0))
Json.get_field([], "x") -- NoneNested field access: traverse a path of keys into nested JSON Objects. Returns Some(value) if the full path exists, or None if any step fails.
Json.get_in(obj, ["a", "b", "c"]) -- equivalent to obj.a.b.c
Object-only. For paths that include array indices, use `get_path`.Mixed-segment path access for JSON values nested in objects and arrays. Each step is Key("name") for an object lookup or Index(i) for an array index. Returns Some(value) if the full path exists, or None if any step fails.
Json.get_path(jv, [Key("items"), Index(0), Key("name")])
-- equivalent to jv.items[0].nameParse a JSON string into a JsonValue. Returns Ok(value) on success, Err(message) on parse error.
Json.parse("null") -- Ok(Null)
Json.parse("42") -- Ok(Number(42.0))
Json.parse("[1, 2, 3]") -- Ok(Array([Number(1.0), ...]))
Json.parse("{\"x\": 1}") -- Ok(Object([("x", Number(1.0))]))Convert a JsonValue to its JSON string representation.
Json.to_string(Null) -- "null"
Json.to_string(Bool(true)) -- "true"
Json.to_string(Number(3.14)) -- "3.14"
Json.to_string(Str("hi")) -- "\"hi\""