Std.Json
View source →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 | ObjTag-union representation for any parsed JSON document.
Null<-> JSONnullBool(b)<-> JSONtrue/falseNum(n)<-> JSON number (always aFloatat runtime)Str(s)<-> JSON stringArr(xs)<-> JSON arrayObj(kvs)<-> JSON object (ordered list ofJsonPair)
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. ReturnsError(msg)on malformed input. -
# fn encode(v: Value) -> String extern
Encode a
Valueas a JSON string. -
# fn num_of_int(n: Int) -> Value extern
Widen a Cure
Intto a JSON numberValuevia the integer path. -
# fn pair(k: String, v: Value) -> JsonPair
Build a
JsonPairfor use insideObj(...).