tango.text.json.Json

License:

BSD style: see license.txt

Version:

July 2008: Initial release

Authors:

Aaron, Kris
class Json(T) : private JsonParser!(T) #
Parse json text into a set of inter-related structures. Typical usage is as follows:
1
2
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:
1
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:

1
2
3
4
5
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:

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

Objects may be constructed with multiple attribute pairs like so:

1
2
3
// {"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:

1
2
3
// {"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.

alias JsonValue* Value [public] #
use these types for external references
enum Type [public] #
enumerates the seven acceptable JSON value types
this() #
Construct a json instance, with a default value of null
Value parse(T[] json) [final] #
Parse the given text and return a resultant Value type. Also sets the document value.
void print(void delegate(T[]) append, T[] separator = null) [final] #
Emit a text representation of this document to the given delegate
Value value() [final] #
Returns the root value of this document
Value value(Value v) [final] #
Set the root value of this document
Value value(T[] v) [final] #
Create a text value
Value value(bool v) [final] #
Create a boolean value
Value value(double v) [final] #
Create a numeric value
Value value(Value[] vals) [final] #
Create a single Value from an array of Values
Value array(...) [final] #
Create an array of values
Attribute pair(T[] name, Value value = null) #
Create an attribute/value pair, where value defaults to null
Value object(Attribute set[] set...) [final] #
Create a composite from zero or more pairs, and return as a value
Value createValue() [private] #
Internal factory to create values
Composite createObject() [private] #
Internal factory to create composites
Attribute createAttribute() [private] #
Internal factory to create attributes
void exception(char[] msg) [private] #
Throw a generic exception
Value parseValue() [private] #
Parse an instance of a value
Composite parseObject() [private] #
Parse an object declaration
Value[] parseArray() [private] #
Parse an array declaration
struct NameValue #
Represents an attribute/value pair. Aliased as Attribute
Attribute set(T[] key, Value val) #
Set a name and a value for this attribute
Returns itself, for use with Composite.add()
struct JsonObject #
Represents a single json Object (a composite of named attribute/value pairs).
This is aliased as Composite
Composite reset() #
Composite append(Attribute a) #
Append an attribute/value pair
Composite add(Attribute[] set...) #
Add a set of attribute/value pairs
Value[T[]] hashmap() #
Construct and return a hashmap of Object attributes. This will be a fairly costly operation, so consider alternatives where appropriate
Value value(T[] name) #
Return a corresponding value for the given attribute name. Does a linear lookup across the attribute set
Iterator attributes() #
Iterate over our attribute names and values
struct Iterator [static] #
Iterate over our attribute names. Note that we use a Fruct to handle this, since foreach does not operate cleanly with pointers (it doesn't automatically dereference them), whereas using x.attributes() does. We may also use this to do some name filtering
struct JsonValue #
Represents a json value that is one of the seven types specified via the Json.Type enum
Type type [public] #
the type of this node
bool opEquals(Type t) #
return true if this node is of the given type
bool toBool() #
Return true if this value represent True
T[] toString(T[] dst = null) #
Return the string content. Returns null if this value is not a string.
Uses dst for escape conversion where possible.
bool toString(void delegate(T[]) dg) #
Emit the string content to the given delegate, with escape conversion as required.
Returns false if this is not a String value
Composite toObject() #
Return the content as a Composite/Object. Returns null if this value is not a Composite.
real toNumber() #
Return the content as a double. Returns nan where the value is not numeric.
Value[] toArray() #
Return the content as an array. Returns null where the value is not an array.
Value set(T[] str, bool escaped = false) #
Set this value to represent a string. If 'escaped' is set, the string is assumed to have pre-converted escaping of reserved characters (such as \t).
Value set(Composite obj) #
Set this value to represent an object.
Value set(real num) #
Set this value to represent a number.
Value set(bool b) #
Set this value to represent a boolean.
Value set(Value[] a) #
Set this value to represent an array of values.
alias reset set #
Set this value to represent null
Value print(void delegate(T[]) append, T[] separator = null) #
Emit a text representation of this value to the provided delegate
Value set(Type type) [private] #
Set to a specified type
Value set(Json host, TypeInfo[] info, va_list args) [private] #
Set a variety of values into an array type
struct Allocator(T) #
Internal allocation mechansim
struct Array [private] #
Internal use for parsing array values
alias Allocator!(NameValue) Attrib [private] #
Internal document representation