Skip to content
FutureBlitz

    Install

    Import std.data.json

    JsonReader

    std.data.json

    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.

    Examples

    Local reader = JsonReader(r"[1, 2, 3]")
    Local sum: Int64 = 0
    Local token = Try reader.Next()
    While token != JsonToken.END
        If token == JsonToken.INT Then sum += reader.GetInt64()
        token = Try reader.Next()
    End While
    
    Expect(sum == 6)

    std/data/json.fb, line 79

    • @PanicsAtCaller Public New(text: String, depth: Int = DEPTH) constructor #

      A JsonReader of text, which nests up to depth deep. Panics unless depth is 1 or more.

    • @PanicsAtCaller Public New(bytes: UInt8[], depth: Int = DEPTH) constructor #

      A JsonReader of bytes, UTF-8 JSON, which nests up to depth deep.

    • Public Method Next(): JsonToken Throws ParseError method #

      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 early, and OUT_OF_RANGE for a number past a Double's range, or arrays and objects nested too deep.

    • @PanicsAtCaller Public Property Token: JsonToken property #

      The token Next gave last.

    • Public Property Depth: Int property #

      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.

    • Public Property Line: Int property #

      The line, from 1, where the token starts.

    • Public Property Column: Int property #

      The column, from 1, in code points, where the token starts.

    • @PanicsAtCaller Public Method GetString(): String method #

      A KEY's or a STRING's text. Panics for another token.

    • @PanicsAtCaller Public Method GetInt64(): Int64 method #

      An INT's integer. Panics for another token.

    • @PanicsAtCaller Public Method GetUInt64(): UInt64 method #

      A UINT's integer, or an INT's that isn't negative. Panics for another token.

    • @PanicsAtCaller Public Method GetDouble(): Double method #

      A number as a Double: a DOUBLE's, or the nearest to an INT's or UINT's. Panics for another token.

    • @PanicsAtCaller Public Method GetBool(): Bool method #

      A BOOL token's value. Panics for another token.

    • Public Method Skip() Throws ParseError method #

      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.

    Report an issue with this page or with FutureBlitz on GitHub.