Json
4 types1 value21 functionsJSON (RFC 8259) values: parsing, encoding, and decoding into Bert values.
Objects keep their members in order, duplicate keys included, and a number
keeps the lexeme it was written with, so nothing is rounded. Decoders are
plain functions from a @Json to a Result, combined with ?.
Types
@Json
A JSON value. A number holds its lexeme, valid by the JSON grammar.
DecodeError
Where decoding failed, outermost key or index first, what the decoder wanted there, and a short description of the value it found.
Option
Shorthand for [Some(a) | None], a value that may be absent.
Result
Shorthand for [Ok(a) | Err(e)], the variant this module's fallible
functions return.
Values
null
The JSON null.
Functions
view
The value's outermost layer, for a match. Nested values stay @Json.
fromBool
The JSON true or false.
fromInt
The JSON number with the integer's decimal digits.
fromFloat
The shortest number that reads back as exactly this float. JSON has no
NaN or infinities, so those are null, as JavaScript writes them.
fromString
The JSON string holding s. It is not parsed: parse reads JSON text.
array
An array of these values, in this order.
object
An object with these members, in this order. A repeated key is kept.
parse
One JSON value, with nothing but whitespace around it. JsonSyntax gives
the byte offset of the first problem and a short description of what was
expected there.
parseBytes
parse for bytes, which must be UTF-8 first.
encode
Compact JSON text: no whitespace between tokens.
encodePretty
JSON text with each array element and object member on a line of its own,
indented by indent spaces per level of nesting, and a space after each
:. An empty array or object stays [] or {}. An indent of zero or
less is encode, and one above 10 is 10, as JSON.stringify has it.
int
A number written as a decimal integer in the Int range. A fraction or an
exponent is an error even when the value is whole, as 1.0 and 1e2 are.
float
Any number, as the float nearest its lexeme. One past the largest float is an infinity, as JavaScript reads it.
string
Decodes a JSON string.
bool
Decodes true or false.
list
Each element through decoder. A failure's path starts with the element's
index.
field
The value of the first member named key, through decoder. A value that
is not an object, or has no such member, is an error at the object's path;
a failure inside the member's value has key in front of its path.
optionalField
field for a member that may be left out: a missing member, or one whose
value is null, is None. A value that is not an object is still an error.
nullable
None for null, and anything else through decoder.
at
The value at the end of a path of keys, through decoder: at(j, ["a", "b"], d) is field(j, "a", \(x) -> field(x, "b", d)).
decode
Parse text, then decode the value.