March Docs

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

typeJsonValueJsonValue =#

Functions

fnparseparse(s)#

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

fnto_stringto_string(jv)#

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\""

fngetget(jv : JsonValue, key : String) : Option(JsonValue)#

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

fnget_inget_in(jv : JsonValue, keys : List(String)) : Option(JsonValue)#

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

fnencode_nullencode_null() do Null end#

Construct a JSON null value.

fnencode_boolencode_bool(b) do Bool(b) end#

Construct a JSON boolean value.

fnencode_numberencode_number(f) do Number(f) end#

Construct a JSON number value from a Float.

fnencode_intencode_int(n) do Number(int_to_float(n)) end#

Construct a JSON number value from an Int.

fnencode_stringencode_string(s) do Str(s) end#

Construct a JSON string value.

fnencode_arrayencode_array(xs) do Array(xs) end#

Construct a JSON array value.

fnencode_objectencode_object(kvs) do Object(kvs) end#

Construct a JSON object value from a list of (key, value) pairs.