module watt.json.dom

Parse a JSON file into memory.

Example

val: Value = parse("{}");

A DOM parser parses an entire file into memory at once. This is often simpler, but if your files are very large, you may need to use the SAX parser, see watt.json.sax.

Code Map

//! Parse a JSON file into memory.
module watt.json.dom;


//! Identifies the JSON type of a Value.
enum DomType
{
	//! A JSON value of null.
	Null,
	//! A JSON value of true or false.
	Boolean,
	//! A JSON value of a number with a decimal portion.
	Double,
	//! A JSON value of a signed integer.
	Long,
	//! A JSON value of an unsigned integer.
	Ulong,
	//! A JSON string.
	String,
	//! A JSON object, everything between {}.
	Object,
	//! A JSON array, everything between [].
	Array,
}

//! Thrown upon a parsing error.
class DOMException : JSONException
{
public:
	this(msg: string, location: string) { }
}

//! A JSON value.
struct Value
{
public:
	union Store
	{
	public:
		boolean: bool;
		str: immutable(char)[];
		integer: i64;
		unsigned: u64;
		floating: f64;
	}


public:
	fn type() DomType { }
	//! Is this Value a JSON null type?
	fn isNull() bool { }
	//! Set the type to null.
	fn setNull() { }
	//! Get this as a boolean value.
	fn boolean() bool { }
	//! Set this Value as a DomType.Boolean, and give it the value b.
	fn boolean(b: bool) { }
	//! Get this as a string value.
	fn str() string { }
	//! Set this Value as a DomType.String, and give it the value s.
	fn str(s: const(char)[]) { }
	//! Get this as an integer value.
	fn integer() i64 { }
	//! Set this Value as a DomType.Long, and give it the value l.
	fn integer(l: i64) { }
	//! Get this as an unsigned integer value.
	fn unsigned() u64 { }
	//! Set this Value as a DomType.Ulong, and give it the value l.
	fn unsigned(l: u64) { }
	//! Get this as a floating point value.
	fn floating() f64 { }
	//! Set this Value as a DomType.Double, and give it the value d.
	fn floating(d: f64) { }
	//! Get this as an array of Value.
	fn array() Value[] { }
	//! Set this Value as a DomType.Array, and give it the value array.
	fn array(array: Value[]) { }
	//! Add val to this Value's array.
	fn arrayAdd(val: Value) { }
	//! Set type as DomType.Array.
	fn setArray() { }
	//! Retrieve a key from this Value.
	fn lookupObjectKey(s: string) Value { }
	//! Does this object have a key s?
	fn hasObjectKey(s: string) bool { }
	//! Set this Value as an object, and set a key.
	fn setObjectKey(k: string, v: Value) { }
	//! Set type as DomType.Object.
	fn setObject() { }
	//! Retrieve all the keys associated with this Value.
	fn keys() string[] { }
	//! Retrieve all the values associated with this Value.
	fn values() Value[] { }
}

//! Parse a JSON string into a Value.
fn parse(s: string) Value { }
class DOMException : JSONException

Thrown upon a parsing error.

enum DomType

Identifies the JSON type of a Value.

enum Null

A JSON value of null.

enum Boolean

A JSON value of true or false.

enum Double

A JSON value of a number with a decimal portion.

enum Long

A JSON value of a signed integer.

enum Ulong

A JSON value of an unsigned integer.

enum String

A JSON string.

enum Object

A JSON object, everything between {}.

enum Array

A JSON array, everything between [].

struct Value

A JSON value.

fn type() DomType

Return

The DomType of the value stored in this node.

fn isNull() bool

Is this Value a JSON null type?

Return

true if this is a null.

fn setNull()

Set the type to null.

fn boolean() bool

Get this as a boolean value.

Throws

  • DOMException if this Value is not a DomType.Boolean.

fn boolean(b: bool)

Set this Value as a DomType.Boolean, and give it the value b.

fn str() string

Get this as a string value.

Throws

  • DOMException if this Value is not a DomType.String.

fn str(s: const(char)[])

Set this Value as a DomType.String, and give it the value s.

fn integer() i64

Get this as an integer value.

Throws

  • DOMException if this Value is not a DomType.Long.

fn integer(l: i64)

Set this Value as a DomType.Long, and give it the value l.

fn unsigned() u64

Get this as an unsigned integer value.

Throws

  • DOMException if this Value is not a DomType.Ulong.

fn unsigned(l: u64)

Set this Value as a DomType.Ulong, and give it the value l.

fn floating() f64

Get this as a floating point value.

Throws

  • DOMException if this Value is not a DomType.Double.

fn floating(d: f64)

Set this Value as a DomType.Double, and give it the value d.

fn array() Value[]

Get this as an array of Value.

Throws

  • DOMException if this Value is not a DomType.Array.

fn array(array: Value[])

Set this Value as a DomType.Array, and give it the value array.

fn arrayAdd(val: Value)

Add val to this Value's array.

Throws

  • DOMException if this is not a DomType.Array.

fn setArray()

Set type as DomType.Array.

fn lookupObjectKey(s: string) Value

Retrieve a key from this Value.

Throws

  • DOMException if the lookup fails, or if this Value is not a DomType.Object.

fn hasObjectKey(s: string) bool

Does this object have a key s?

Return

true if this Value has the given key.

Throws

  • DOMException if this Value is not a DomType.Object.

fn setObjectKey(k: string, v: Value)

Set this Value as an object, and set a key.

Parameters

k

The key to set.

v

The value to associate with k.

fn setObject()

Set type as DomType.Object.

fn keys() string[]

Retrieve all the keys associated with this Value.

Throws

  • DOMException if this Value is not a DomType.Object.

fn values() Value[]

Retrieve all the values associated with this Value.

Throws

  • DOMException if this Value is not a DomType.Object.

fn parse(s: string) Value

Parse a JSON string into a Value.