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, } jsonStr = Str.toUtf8 "{\"name\":\"Röc Lang\"}" result : Result Language _ result = jsonStr |> Decode.fromBytes Json.utf8 # returns `Ok {name : "Röc Lang"}` name = decodedValue = result? Ok (Encode.toBytes decodedValue.name Json.utf8) expect name == Ok (Str.toUtf8 "\"Röc Lang\"")
Json
An opaque type with the Encode.EncoderFormatting
and
DecoderFormatting
abilities.
utf8
Returns a JSON Encode.Encoder
and Decoder