bea_codecs/json/decoder
JSON decoders for @derive(decoder):
import bea_codecs/json/decoder
@derive(decoder)
pub type Credentials {
Credentials(username: String, password: String)
}
credentials = decoder.from_json(body, Credentials.decoder())?
A derived decoder is a plain decode.Decoder from the standard
library, so it works with json.parse, decode.run on any Dynamic,
and every Gleam library that takes decoders.
The conventions are this module’s, not the compiler’s: a record is an
object with a key per field label; a field whose decoder accepts null
(an Option) may also be left out; an integer is accepted where a float
is expected; errors are the standard library’s, with the path to the
failing value.
Values
pub fn bool() -> decode.Decoder(Bool)
pub fn dict(
keys: decode.Decoder(k),
values: decode.Decoder(v),
) -> decode.Decoder(dict.Dict(k, v))
pub fn done(value: r) -> decode.Decoder(r)
pub fn field(
label: String,
of: decode.Decoder(a),
get: fn(r) -> a,
next: fn(a) -> decode.Decoder(r),
) -> decode.Decoder(r)
One field, under its label. Missing is the same as null: fine when the
field’s decoder accepts null, a “missing field” error otherwise.
pub fn float() -> decode.Decoder(Float)
JSON doesn’t tell 1 from 1.0: both are fine for a Float.
pub fn from_json(
text: String,
decoder: decode.Decoder(a),
) -> Result(a, json.DecodeError)
pub fn int() -> decode.Decoder(Int)
pub fn list(of: decode.Decoder(a)) -> decode.Decoder(List(a))
pub fn option(
of: decode.Decoder(a),
) -> decode.Decoder(option.Option(a))
null is None.
pub fn record(
constructor: String,
fields: fn() -> decode.Decoder(r),
) -> decode.Decoder(r)
A record is decoded from its fields.
pub fn string() -> decode.Decoder(String)