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

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
T[] toString(const(T)[] space, int decimals)

Return a text representation of this document

toString
immutable(char)[] toString()
Undocumented in source. Be warned that the author may not have intended to support it.
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();
     assert (value == `{"edgar":"friendly","count":11.5,"array":[1, 2]}`, value);
     }
// check with a separator of the tab character
with (new Json!(char))
     {
     root = object
          (
          pair ("edgar", value("friendly")),
          pair ("count", value(11.5)),
          pair ("array", value(array(1, 2)))
          );

     auto value = toString ("\t");
     assert (value == "{\n\t\"edgar\":\"friendly\",\n\t\"count\":11.5,\n\t\"array\":[\n\t\t1, \n\t\t2\n\t]\n}", value);
     }
// check with a separator of five spaces
with (new Json!(dchar))
     {
     root = object
          ( 
          pair ("edgar", value("friendly")),
          pair ("count", value(11.5)),
          pair ("array", value(array(1, 2)))
          );

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

Meta