JSON to and from your own types. to_json encodes a value and from_json decodes text into a
type; both are generated from the type at compile time. When the shape is not known, decode
into a Json_Value instead. Behaviour per field is set with @json(...) notes.
The error's path in jsonpath form, as jsonb_path_query takes it: $ for the document itself,
$.address.tags[2], and a key that is not a plain identifier quoted: $."we ird". Allocated
with the temporary allocator.
Decodes a T from a source in pieces. The pieces are gathered in one buffer (the decoder wants
the whole document, and its strings are copied out of it, so the buffer is freed before this
returns) and then decoded exactly as from_json(T, text) would, so a document split at any
byte gives the same value or the same error. A source that fails gives a .READ error with
the source's text; a document longer than options.max_size is a .LIMIT error and the rest
is not read.
Writes the JSON of the value value points to to the writer w. This and json_decode are
what with= handlers call for the parts they do not encode themselves.
Appends text as a JSON string with its quotes. Control characters, quotes and backslashes
are escaped; bytes that are not valid UTF-8 become U+FFFD so the output is always valid JSON.
Starts a member: the separator, then the key. quoted_key is the key with its quotes and
escapes already applied (the generated code does that at compile time).
Say the value being read is at key / index of its parent. The generated decoders call
these while an error travels up, so the error's path reads address.tags[2].
Reads up to the next member of the object: its key (valid until the next read) and the colon
after it, or the closing brace. first is true before the first call.
A string value without copying: a view of the input, or of the reader's scratch space when
it has escapes. Valid until the next read; copy it to keep it.
Decodes a T from pairs. A key that repeats gives an array for an array field, and is a
"duplicate field" error for any other. Keys no field has are ignored unless T is marked
deny_unknown_fields. Everything allocated (strings, arrays) comes from context.allocator and
is released by free_json, as for from_json.
Decodes a T from one piece of text: an integer, float, bool, string or enum. from_param(int, "42") is 42; from_param(Color, "red") the member red; a string is taken as it is.
Why decoding failed and where. path names the value inside the document (tags[2],
address.zip); line and column count from 1, the column in characters. message and
path are allocated with context.allocator: free_json releases them.
What a repeated key in an object does. A Hash_Table already keeps the last value.
borrow: := false
Strings without escapes (and the keys and numbers of a Json_Value) are slices of the input
text instead of copies; strings with escapes are still allocated from context.allocator.
The input must outlive the result, and the result must never be released with free_json
(nor may a failed decode free through a heap allocator): decode with context.allocator
set to an arena or temp, whose free does nothing, and reset that instead. Ignored by the
streaming from_json and by from_query/from_param, whose text does not outlive the call.
A field that distinguishes a key that is missing, a null and a value. As a struct member a
missing key leaves .MISSING (never a missing-field error), null gives .NULL and any other
value decodes into value and gives .SET. Encoding omits the key for .MISSING, writes
null for .NULL and the value for .SET. Outside a struct member (the document, an array
element) .MISSING encodes as null. free_json releases value when it is .SET.
Where a document comes from when it is not in memory yet: fill puts the next piece of text in
buffer and returns how many bytes it wrote (0 at the end), and a non-empty string when the
source failed (what went wrong). data is handed to it as it is. Json does not know what the
pieces come from; Io.read_json makes one from an Io.Reader.
One JSON value. Only the fields of its kind are used:
BOOL boolean
NUMBER text, the number as written (exact for integers of any size; see json_as_s64)
STRING text, the contents with escapes resolved
ARRAY items
OBJECT members, in document order (duplicate keys are kept; json_get finds the last)
Everything a Json_Value owns comes from context.allocator; free_json releases it.
Reads one JSON document. Every reading procedure returns false after recording an error in
error; the first error wins, and callers must stop at once and return false themselves.