std.json
std.json: parse JSON (RFC 8259) into a tree of `Value`s, and write a tree back out (`write`, `writePretty`, `stringify`).
`parse` and `parseDiag` take an `*mut Arena` and allocate the whole tree in it (strings are copies, so the text may go away), so one `arena.deinit()` frees it and a failed parse leaves nothing to clean up. Parsing is strict: no comments, no trailing commas, no leading zeros, no NaN or Infinity, no raw control characters or invalid UTF-8 in strings, no lone surrogate escapes, and nothing but whitespace (space, tab, line feed, carriage return) around the value. The tree is read with the methods of `Value`; each gives null for the wrong kind of value.
`write` and `writePretty` append to a `text.Builder`; a number is written with the shortest digits that read back as the same `f64`.
An error value is only a name, so `parseDiag` also adds one `std.diag` diagnostic with the byte offset and what was expected; `parse` does not.
ParseError
pub error ParseError { UnexpectedEnd, UnexpectedToken, TrailingData, InvalidNumber, NumberOutOfRange, InvalidEscape, InvalidSurrogate, ControlCharacter, InvalidUtf8, TooDeep, OutOfMemory }
Why a parse failed. `UnexpectedEnd`: the text ended inside a value. `UnexpectedToken`: a byte that cannot come here. `TrailingData`: more than whitespace after the value. `InvalidNumber`: a malformed number, including a leading zero. `NumberOutOfRange`: a number too large for an `f64` (a tiny one rounds to 0). `InvalidEscape`: a bad `\` escape or `\u` digits. `InvalidSurrogate`: a `\u` surrogate without its partner. `ControlCharacter`: a byte below 0x20 inside a string. `InvalidUtf8`: bad UTF-8 inside a string. `TooDeep`: more than `maxDepth` arrays and objects inside each other.
maxDepth
pub let maxDepth: usize = 512
The deepest nesting of arrays and objects that `parse` accepts.
Member
pub struct Member {
key: []u8
value: Value
}
A member of an object: its name and its value.
Value
pub enum Value {
Null
Bool { value: bool }
Number { value: f64 }
Integer { value: f64, int: i64 }
String { value: []u8 }
Array { items: []Value }
Object { members: []Member }
pub fn get(self: *Self, key: []u8) ?*Value
pub fn at(self: *Self, i: usize) ?*Value
pub fn asString(self: *Self) ?[]u8
pub fn asNumber(self: *Self) ?f64
pub fn asInt(self: *Self) ?i64
pub fn asBool(self: *Self) ?bool
pub fn asArray(self: *Self) ?[]Value
pub fn asObject(self: *Self) ?[]Member
pub fn isNull(self: *Self) bool
}
A JSON value. A string is UTF-8; an object keeps its members in the order of the text (a repeated name is kept, and `get` finds the last).
Integer { value: f64, int: i64 }-
A number written as an integer literal that fits `i64`: `int` is its exact value, `value` the same number as an `f64`. Only `parse` makes one; `asNumber`, `equals` and `write` treat it like a `Number`.
pub fn get(self: *Self, key: []u8) ?*Value-
The member called `key` of an object (the last one when the name is repeated); null for a missing name or a value that is not an object.
pub fn at(self: *Self, i: usize) ?*Value-
The element `i` of an array; null when out of range or not an array.
pub fn asString(self: *Self) ?[]u8-
The bytes of a string; null for any other value.
pub fn asNumber(self: *Self) ?f64-
The number; null for any other value.
pub fn asInt(self: *Self) ?i64-
The exact integer of a number written as an integer literal (no fraction, no exponent) that fits `i64`, also above 2^53 where `asNumber` rounds; null for any other value, for `1.0`, `1e2`, and for a literal outside `i64`.
pub fn asBool(self: *Self) ?bool-
The boolean; null for any other value.
pub fn asArray(self: *Self) ?[]Value-
The elements of an array; null for any other value.
pub fn asObject(self: *Self) ?[]Member-
The members of an object, in text order; null for any other value.
pub fn isNull(self: *Self) bool-
Is this `null`?
parse
pub fn parse(arena: *mut Arena, src: []u8) ParseError!Value
Parses one JSON text; everything lives in `arena`. On failure the error says what went wrong but not where (see `parseDiag`).
parseDiag
pub fn parseDiag(arena: *mut Arena, src: []u8, diags: *mut Diagnostics) ParseError!Value
Like `parse`, and on failure also adds one diagnostic to `diags`: its `start` is the byte offset where the text went wrong (the length of the text for `UnexpectedEnd`), its message says what was expected, and its code is the 1-based position of the error in `ParseError`.
write
pub fn write(b: *mut Builder, v: *Value) AllocError!void
Writes `v` as compact JSON (no whitespace) to `b`. A string is escaped per RFC 8259: `"` and `\` get a backslash, the control characters below 0x20 are `\n` `\t` `\r` `\b` `\f` or `\u00XX`, and every other byte (`/` and non-ASCII too) is copied as it is. Object members keep their order. A number is written as `writeNumber` does.
writePretty
pub fn writePretty(b: *mut Builder, v: *Value, indent: usize) AllocError!void
Like `write`, with a newline and 2 more spaces of indent for each level of array and object, and `": "` after a member name. An empty array or object stays `[]` or `{}`. `indent` is the number of spaces the value's own level is already indented by (0 for a whole document): the lines inside the value start 2 further in, and the closing bracket is `indent` in. The first line gets no indent, and no newline ends the text.
stringify
pub fn stringify(alloc: Allocator, v: *Value) AllocError![]mut u8
`v` as compact JSON (see `write`) in new memory from `alloc`; free it with `alloc.free`.
equals
pub fn equals(a: *Value, b: *Value) bool
Are `a` and `b` the same JSON value? Kinds must match, strings are compared by bytes, numbers by `==` (so 0 and -0 are equal, and a NaN is equal to nothing), arrays element by element, and objects member by member in order, names included (a repeated name counts each time).
writeNumber
pub fn writeNumber(b: *mut Builder, v: f64) AllocError!void
Appends `v` as a JSON number. A whole number below 2^53 in size has no fraction or exponent (`-0` stays `-0`). Any other number gets the shortest digits that read back as the same `f64`, laid out as `JSON.stringify` does: plain decimal for sizes from 1e-6 up to below 1e21 (`0.1`, `123456.789`, `1e20` as 100000000000000000000), else digits with an exponent (`1e+21`, `5e-324`, `-1.5e-10`). NaN and the infinities have no JSON form and the parser never makes them; they are written as `null`.