Lyric v4.5.0

std.json

JsonValue enum

pub enum JsonValue

One JSON value — the tree parse answers and serialize writes.

Null

The literal null.

Bool(bool)

true or false.

Int(int)

A number the text wrote without fraction or exponent, exact in the int range.

Float(float)

A number with a fraction or an exponent — or an integer too large for Int.

Str(string)

A string.

Arr(List<JsonValue>)

An array.

Obj(Map<string, JsonValue>)

An object. Member order is the map's: unspecified.

pub fn isNull(): bool

Whether this is the JSON null — the value, not an absent optional.

pub fn asBool(): ?bool

The boolean, or null when this is not one.

pub fn asInt(): ?int

The integer, or null when this is not Int. A Float with an integral value stays null: which of the two a number is was decided by the text it came from.

pub fn asFloat(): ?float

The number as a float — Float directly, Int widened — or null when this is not a number. JSON has one number type; two variants is this module keeping precision, not two kinds of JSON value.

pub fn asString(): ?string

The string, or null when this is not one.

pub fn asArray(): ?List<JsonValue>

The elements, or null when this is not an array. The list is the value's own — changing it changes the value.

pub fn asObject(): ?Map<string, JsonValue>

The members, or null when this is not an object. The map is the value's own — changing it changes the value.

pub fn at(index: int): ?JsonValue

The element at index, or null when this is not an array or the index is outside it.

pub fn field(name: string): ?JsonValue

The member named name, or null when this is not an object or has no such member.

stdlib/std/json.lyr:28

serialize function

pub fn serialize(value: JsonValue): string

The value as compact JSON: no whitespace anywhere.

stdlib/std/json.lyr:124

serializePretty function

pub fn serializePretty(value: JsonValue, indent: int): string

The value as JSON with a newline per element and indent spaces per nesting level, the shape a person reads. An indent below zero is treated as zero.

stdlib/std/json.lyr:132

JsonError class

pub class JsonError :: [Throwable]

Where and why a document was refused: the first place the parser could not continue, and what it expected there. line and column are 1-based and counted in code points.

line: int
column: int
offset: int

The 0-based code-point offset of the refusal — line and column, unfolded.

expected: string

What the parser expected at that place, as prose: "a value", "',' or '}'", …

pub fn message(): string

stdlib/std/json.lyr:303

parse function

pub fn parse(text: string): ?JsonValue

The value a JSON document spells, or null when the text spells none: a syntax error anywhere, anything after the value, a lone surrogate escape, a raw control character in a string, or containers nested more than 128 deep. Strict RFC 8259 — no comments, no trailing commas, no NaN, no single quotes. The whole document is one value; leading and trailing whitespace around it is fine.

Duplicate object keys follow Map.set: the last one wins, as in JavaScript.

The depth bound is the module's own, not the runtime's: input is data, and data that is not JSON must come back as null rather than as a panic about call depth.

stdlib/std/json.lyr:331

parseOrThrow function

pub fn parseOrThrow(text: string): JsonValue throws JsonError

parse, with the reason when the text spells no value: where the document broke, and what the parser expected there. The strictness is parse's — the two are one parser.

stdlib/std/json.lyr:347