Json

JSON is a data format that is easy for humans to read and write. It is commonly used to exhange data between two systems such as a server and a client (e.g. web browser).

This module implements functionality to serialise and de-serialise Roc types to and from JSON data. Using the Encode and Decode builtins this process can be achieved without the need to write custom encoder and decoder functions to parse UTF-8 strings.

Here is a basic example which shows how to parse a JSON record into a Roc type named Language which includes a name field. The JSON string is decoded and then the field is encoded back into a UTF-8 string.

Language : {
    name : Str,
}

json_str = Str.to_utf8("{\"name\":\"Röc Lang\"}")

result : Result Language _
result =
    json_str
    |> Decode.from_bytes(Json.utf8) # returns `Ok({name : "Röc Lang"})`

name =
    decoded_value = result?

    Ok (Encode.to_bytes decoded_value.name Json.utf8)

expect name == Ok(Str.to_utf8("\"Röc Lang\""))

Json

An opaque type with the Encode.EncoderFormatting and DecoderFormatting abilities.

utf8

Returns a JSON Encode.Encoder and Decoder

utf8_with : { field_name_mapping ? FieldNameMapping, skip_missing_properties ? Bool, null_decode_as_empty ? Bool, empty_encode_as_null ? EncodeAsNull } -> Json

encode_as_null_option : { list ? Bool, tuple ? Bool, record ? Bool } -> EncodeAsNull