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 =#
typeJsonPathStepJsonPathStep = JPathField(String) | JPathIndex(Int)#
typeDecodeErrorDecodeError = DecodeError(String, List(JsonPathStep), Int)#
typeJsonPathJsonPath = Key(String) | Index(Int)#

Functions

fndecode_error_atdecode_error_at(off, e)#

Attach an absolute byte offset to a DecodeError.

fndecode_error_to_stringdecode_error_to_string(e)#

Render a DecodeError as $.users[3].id: expected Int, with the byte offset when known: $.id (byte 4096): expected Int.

fndecode_error_underdecode_error_under(step, e)#

Push step onto the front of a DecodeError's path, as a decoder does when it descends.

fnencode_arrayencode_array(xs) do Array(xs) end#

Construct a JSON array value.

fnencode_boolencode_bool(b) do Bool(b) end#

Construct a JSON boolean value.

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

Construct a JSON number value from an Int.

fnencode_nullencode_null() do Null end#

Construct a JSON null value.

fnencode_numberencode_number(f) do Number(f) end#

Construct a JSON number value from a Float.

fnencode_objectencode_object(kvs) do Object(kvs) end#

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

fnencode_stringencode_string(s) do Str(s) end#

Construct a JSON string value.

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_atget_at(jv : JsonValue, i : Int) : Option(JsonValue)#

Retrieve 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)
    -- None
fnget_fieldget_field(kvs : List((String, JsonValue)), key : String) : Option(JsonValue)#

Look 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")                    -- 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

Object-only.  For paths that include array indices, use `get_path`.
fnget_pathget_path(jv : JsonValue, path : List(JsonPath)) : Option(JsonValue)#

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].name
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\""