Json

Parse json text into a set of inter-related structures. Typical usage is as follows:

auto json = new Json!(char);
json.parse (`{"t": true, "n":null, "array":["world", [4, 5]]}`);

Converting back to text format employs a delegate. This one emits document content to the console:

json.print ((char[] s) {Stdout(s);});

Constructing json within your code leverages a handful of factories within a document instance. This example creates a document from an array of values:

auto json = new Json!(char);

// [true, false, null, "text"]
with (json)
value = array (true, false, null, "text");

Setting the document to contain a simple object instead:

// {"a" : 10}
with (json)
value = object (pair("a", value(10)));

Objects may be constructed with multiple attribute pairs like so:

// {"a" : 10, "b" : true}
with (json)
value = object (pair("a", value(10)), pair("b", value(true)));

Substitute arrays, or other objects as values where appropriate:

// {"a" : [10, true, {"b" : null}]}
with (json)
value = object (pair("a", array(10, true, object(pair("b")))));

TODO: document how to extract content

Big thanks to dhasenan for suggesting the construction notation. We can't make effective use of operator-overloading, due to the use of struct pointers, so this syntax turned out to be the next best thing.

Constructors

this
this()

Construct a json instance, with a default value of null

Members

Aliases

Attribute
alias Attribute = NameValue*
Undocumented in source.
Composite
alias Composite = JsonObject*
Undocumented in source.
Value
alias Value = JsonValue*

use these types for external references

Enums

Type
enum Type

enumerates the seven acceptable JSON value types

Functions

array
Value array(...)

Create an array of values

object
Value object(Attribute[] set)

Create a composite from zero or more pairs, and return as a value

pair
Attribute pair(Const!(T)[] name, Value value)

Create an attribute/value pair, where value defaults to null

parse
Value parse(Const!(T)[] json)

Parse the given text and return a resultant Value type. Also sets the document value.

toString
Const!(T)[] toString(Const!(T)[] space, int decimals)

Return a text representation of this document

value
Value value()

Returns the root value of this document

value
Value value(Value v)

Set the root value of this document

value
Value value(Const!(T)[] v)

Create a text value

value
Value value(bool v)

Create a boolean value

value
Value value(double v)

Create a numeric value

value
Value value(Value[] vals)

Create a single Value from an array of Values

Structs

JsonObject
struct JsonObject

Represents a single json Object (a composite of named attribute/value pairs).

JsonValue
struct JsonValue

Represents a json value that is one of the seven types specified via the Json.Type enum

NameValue
struct NameValue

Represents an attribute/value pair. Aliased as Attribute

Examples

with (new Json!(char))
{
    root = object
        (
         pair ("edgar", value("friendly")),
         pair ("count", value(11.5)),
         pair ("array", value(array(1, 2)))
        );

    auto value = toString();
    test (value == `{"edgar":"friendly","count":11.5,"array":[1, 2]}`, value);
}

Meta