Skip to content
FutureBlitz

    Install

    Import std.data.json

    JsonWriter

    std.data.json

    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.

    Examples

    Local writer = JsonWriter()
    writer.BeginObject()
    writer.Key("name")
    writer.WriteString("Ada")
    writer.Key("scores")
    writer.BeginArray()
    writer.WriteInt(3)
    writer.WriteInt(5)
    writer.EndArray()
    writer.EndObject()
    Expect(writer.IsComplete)
    Local written = Try Json.Read(writer.ToString())
    Expect(written.Path("scores[1]")?.AsInt() == 5)

    std/data/json.fb, line 237

    • @PanicsAtCaller Public New(indent: Int = 0) constructor #

      A JsonWriter that indents by indent spaces a level, or writes on one line for 0. Panics when indent is below 0.

    • Public Property IsComplete: Bool property #

      Whether the document's value is all written.

    • @PanicsAtCaller Public Method BeginObject() method #

      Starts an object, whose keys and values come next. Panics where a value can't go.

    • @PanicsAtCaller Public Method EndObject() method #

      Ends the object that's open. Panics when the innermost open one is an array, or a key has no value.

    • @PanicsAtCaller Public Method BeginArray() method #

      Starts an array, whose values come next. Panics where a value can't go.

    • @PanicsAtCaller Public Method EndArray() method #

      Ends the array that's open. Panics when the innermost open one is an object.

    • @PanicsAtCaller Public Method Key(name: String) method #

      An object's next key, whose value comes next.

    • @PanicsAtCaller Public Method WriteString(value: String) method #

      Writes value as a string, in quotes, with JSON's escapes. Panics where a value can't go.

    • @PanicsAtCaller Public Method WriteInt(value: Int64) method #

      Writes value as a number. Panics where a value can't go.

    • @PanicsAtCaller Public Method WriteUInt(value: UInt64) method #

      Writes value as a number. Panics where a value can't go.

    • @PanicsAtCaller Public Method WriteDouble(value: Double) method #

      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.

    • @PanicsAtCaller Public Method WriteBool(value: Bool) method #

      Writes value as true or false. Panics where a value can't go.

    • @PanicsAtCaller Public Method WriteNull() method #

      Writes null. Panics where a value can't go.

    • Public Method ToString(): String method #

      The JSON written so far.

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