json

JsonValue: String, Integer, Number, Boolean, Null, Array, Object, print, toString

JsonMember: key, value

JsonError: UnexpectedCharacter, InvalidLiteral, ExpectedStringKey, ExpectedColon, ExpectedCommaOrBrace, ExpectedCommaOrBracket, UnterminatedString, UnterminatedEscape, InvalidEscape, InvalidUnicodeEscape, UnpairedSurrogate, UnescapedControlCharacter, InvalidNumber, NumberOutOfRange, UnexpectedTrailingCharacters, MaximumDepthExceeded, print

Functions: parseJson

enum JsonValue : Printable

A JSON value: a tagged union of all JSON types.

Whole numbers that fit in 64 bits parse as Integer; fractional and overlarge numbers parse as Number. Objects preserve insertion order and allow duplicate keys.

String

String(StringBuf value),

Integer

Integer(int64 value),

Number

Number(float64 value),

Boolean

Boolean(bool value),

Null

Null

Array

Array(List<JsonValue> value),

Object

Object(List<JsonMember> value),

print

void print(OutputStream& stream)

Writes the value in compact JSON form.

Printable's toString() renders this same form.

toString

StringBuf toString(bool pretty)

Renders the value as JSON, indented with 2 spaces per level when pretty is true and on one line otherwise.

Numbers print in the shortest form that parses back to the same value. Non-finite Number values abort; JSON has no spelling for them.

struct JsonMember

One key-value pair of a JSON object.

key

StringBuf key;

The member's key.

value

JsonValue value;

The member's value.

enum JsonError: Printable

A JSON parse error: what went wrong and the byte offset where parsing stopped.

UnexpectedCharacter

UnexpectedCharacter(int offset),

InvalidLiteral

InvalidLiteral(int offset),

ExpectedStringKey

ExpectedStringKey(int offset),

ExpectedColon

ExpectedColon(int offset),

ExpectedCommaOrBrace

ExpectedCommaOrBrace(int offset),

ExpectedCommaOrBracket

ExpectedCommaOrBracket(int offset),

UnterminatedString

UnterminatedString(int offset),

UnterminatedEscape

UnterminatedEscape(int offset),

InvalidEscape

InvalidEscape(int offset),

InvalidUnicodeEscape

InvalidUnicodeEscape(int offset),

UnpairedSurrogate

UnpairedSurrogate(int offset),

UnescapedControlCharacter

UnescapedControlCharacter(int offset),

InvalidNumber

InvalidNumber(int offset),

NumberOutOfRange

NumberOutOfRange(int offset),

UnexpectedTrailingCharacters

UnexpectedTrailingCharacters(int offset),

MaximumDepthExceeded

MaximumDepthExceeded(int offset),

print

void print(OutputStream& stream)

Writes the error as text, with its byte offset.

parseJson

Result<JsonValue, JsonError> parseJson(string text)

Parses the JSON document in text.

Surrounding whitespace is allowed, but trailing characters are an error. Whole numbers that fit in 64 bits become Integer; other numbers become Number, with out-of-range numbers an error. Inputs nested more than 1000 levels deep are rejected. On failure returns which error occurred and at which byte offset.