JSON encoder and decoder for Cure (v0.23.0).

Runtime companion to the v0.21.0 @derive(JSON) extension. The decoder returns a Std.Json.Value ADT; encoders walk the ADT back out to a JSON string.

Examples

use Std.Json

match decode("{\"x\": 1}")
  Ok(v)    -> encode(v)                 # => "{\"x\":1}"
  Error(_) -> ""
use Std.Json

let body = Obj([pair("status", Str("ok")), pair("count", num_of_int(3))])
encode(body)                            # => {"status":"ok","count":3}

Types

  • type Value = Null | Bool | Num | Str | Arr | Obj

    Tag-union representation for any parsed JSON document.

    • Null <-> JSON null
    • Bool(b) <-> JSON true / false
    • Num(n) <-> JSON number (always a Float at runtime)
    • Str(s) <-> JSON string
    • Arr(xs) <-> JSON array
    • Obj(kvs) <-> JSON object (ordered list of JsonPair)

Functions

  • # fn __group__() -> Atom

    Group tag consumed by Cure.Stdlib.Preload.

  • # fn decode(src: String) -> Result(Value, String) extern

    Decode a JSON string into a Value. Returns Error(msg) on malformed input.

  • # fn encode(v: Value) -> String extern

    Encode a Value as a JSON string.

  • # fn num_of_int(n: Int) -> Value extern

    Widen a Cure Int to a JSON number Value via the integer path.

  • # fn pair(k: String, v: Value) -> JsonPair

    Build a JsonPair for use inside Obj(...).