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
Parse 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\""
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") -- None
Nested 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
Construct a JSON null value.
Construct a JSON boolean value.
Construct a JSON number value from a Float.
Construct a JSON number value from an Int.
Construct a JSON string value.
Construct a JSON array value.
Construct a JSON object value from a list of (key, value) pairs.