Import std.data.json
std.data.json
JSON, as RFC 8259 has it: Json.Read reads a document into a Value, and Json.Write writes a Value as one; JsonReader reads a document's tokens one at a time, in order, and JsonWriter writes a document a token at a time, so neither holds more than the text. Integers stay exact, as Int64s, or UInt64s past them; other numbers are Doubles. The runtime reads the text, over fast_float for its Doubles.
std/data/json.fb
Types
JsonToken
enum
Public Enum JsonToken
What a JsonReader read.
-
The document's end, after its value.
-
An object's start.
-
An object's end.
-
An array's start.
-
An array's end.
-
A key of an object's.
-
A string.
-
A number that's an integer an Int64 holds.
-
An integer past an Int64's largest that a UInt64 holds.
-
Any other number.
-
true or false.
-
null.
JsonReader
class
Public Class JsonReader
Reads a JSON document's tokens, one each time Next is called, from text or from UTF-8 bytes, which it copies: an object's as BEGIN_OBJECT, then each KEY and its value, then END_OBJECT, and an array's as BEGIN_ARRAY, its values, and END_ARRAY. A byte order mark before the JSON is left out. It reads arrays and objects nested in each other up to depth deep.
-
Panics unless
depthis 1 or more. -
A JsonReader of
bytes, UTF-8 JSON, which nests up todepthdeep. -
The next token, END once the document's value is done, and END again after. Throws a ParseError where the text stops being JSON, with its line and column, each from 1: of SYNTAX for what can't be there, UNEXPECTED_END for text that ends too soon, and OUT_OF_RANGE for a number past a Double's range, or arrays and objects nested too deep.
-
The token Next gave last.
-
How many arrays and objects are open after the token: 0 for a document's lone value, 1 after its BEGIN_ARRAY or BEGIN_OBJECT and for each value in it, and 0 again after its END_ARRAY or END_OBJECT.
-
The line and column, each from 1, the column in code points, where the token starts.
-
The column, from 1, in code points, where the token starts.
-
A KEY's or a STRING's text. Panics for another token.
-
An INT's integer. Panics for another token.
-
A UINT's integer, or an INT's that isn't negative. Panics for another token.
-
A number as a Double: a DOUBLE's, or the nearest to an INT's or UINT's. Panics for another token.
-
A BOOL's. Panics for another token.
-
Reads past the value whose first token Next gave last, to its END_OBJECT or END_ARRAY when it's an object or an array. Throws as Next does.
JsonWriter
class
Public Class JsonWriter Implements Printable
Writes a JSON document a token at a time: BeginObject, then each Key and its value, then EndObject; BeginArray, its values, then EndArray; or one value alone. With an indent of 0, the text is on one line with no spaces, and otherwise each value of an array or an object is on a line of its own, indented that many spaces more than what holds it, and a key's value is after ": ". Each method panics when its token can't come next, as a Key outside an object, or a second value after the document's, at its caller.
-
Panics when
indentis below 0. -
Whether the document's value is all written.
-
Starts an object, whose keys and values come next. Panics where a value can't go.
-
Ends the object that's open. Panics when the innermost open one is an array, or a key has no value.
-
Starts an array, whose values come next. Panics where a value can't go.
-
Ends the array that's open. Panics when the innermost open one is an object.
-
An object's next key, whose value comes next.
-
Writes
valueas a string, in quotes, with JSON's escapes. Panics where a value can't go. -
Writes
valueas a number. Panics where a value can't go. -
Writes
valueas a number. Panics where a value can't go. -
A Double, as the shortest text that reads back as it, with ".0" after one that would read as an integer, so it reads back as a DOUBLE. Panics for NaN or an infinity, which JSON doesn't have.
-
Writes
valueas true or false. Panics where a value can't go. -
Writes null. Panics where a value can't go.
-
The JSON written so far.
Json
struct
Public Struct Json
A document's Value, and a Value's document.
-
@PanicsAtCaller Public Static Method Read(text: String, depth: Int = DEPTH): Value Throws ParseErrorstatic method #The Value of
text's JSON: an object as an OBJECT, its keys in order, the last of a key that repeats where its first was, and an integer that fits an Int64 as an INT, one past an Int64's largest that fits a UInt64 as a UINT, and any other number as a DOUBLE. Throws a ParseError as JsonReader's Next does, and for arrays and objects nested pastdepth. Panics unlessdepthis 1 or more. -
@PanicsAtCaller Public Static Method Read(bytes: UInt8[], depth: Int = DEPTH): Value Throws ParseErrorstatic method #The Value of UTF-8
bytes' JSON, as Read of text gives it. -
valueas JSON, as JsonWriter writes it withindent: a DOUBLE with a point or an exponent, so it reads back as one, and BYTES, which JSON has no kind for, as a string of their base64 (RFC 4648's, with '=' padding). Panics for a DOUBLE that's NaN or an infinity, which JSON doesn't have, or an indent below 0. -
@PanicsAtCaller Public Static Method Read<T>(text: String, depth: Int = DEPTH): T Throws ParseErrorstatic method #The T of
text's JSON: Read's Value, made a T byLoad<T>. Throws Read's ParseErrors, and Load's, whose Path says where in the document a value doesn't fit. -
@PanicsAtCaller Public Static Method Read<T>(bytes: UInt8[], depth: Int = DEPTH): T Throws ParseErrorstatic method #The T of UTF-8
bytes' JSON, asRead<T>of text gives it. -
xas JSON: Save's Value of it, written as Write writes a Value.
Report an issue with this page or with FutureBlitz on GitHub.