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 { }
Thrown upon a parsing error.
Identifies the JSON type of a Value
.
A JSON value of null
.
A JSON value of true
or false
.
A JSON value of a number with a decimal portion.
A JSON value of a signed integer.
A JSON value of an unsigned integer.
A JSON string.
A JSON object, everything between {}.
A JSON array, everything between [].
A JSON value.
Return
The DomType
of the value stored in this node.
Is this Value
a JSON null type?
Return
true
if this is a null.
Set the type to null.
Get this as a boolean value.
Throws
-
DOMException
if thisValue
is not aDomType.Boolean
.
Set this Value
as a DomType.Boolean
, and give it the value b
.
Get this as a string value.
Throws
-
DOMException
if thisValue
is not aDomType.String
.
Set this Value
as a DomType.String
, and give it the value s
.
Get this as an integer value.
Throws
-
DOMException
if thisValue
is not aDomType.Long
.
Set this Value
as a DomType.Long
, and give it the value l
.
Get this as an unsigned integer value.
Throws
-
DOMException
if thisValue
is not aDomType.Ulong
.
Set this Value
as a DomType.Ulong
, and give it the value l
.
Get this as a floating point value.
Throws
-
DOMException
if thisValue
is not aDomType.Double
.
Set this Value
as a DomType.Double
, and give it the value d
.
Get this as an array of Value
.
Throws
-
DOMException
if thisValue
is not aDomType.Array
.
Set this Value
as a DomType.Array
, and give it the value array
.
Add val
to this Value
's array.
Throws
-
DOMException
if this is not aDomType.Array
.
Set type as DomType.Array
.
Retrieve a key from this Value
.
Throws
-
DOMException
if the lookup fails, or if thisValue
is not aDomType.Object
.
Does this object have a key s
?
Return
true
if this Value
has the given key.
Throws
-
DOMException
if thisValue
is not aDomType.Object
.
Set this Value
as an object, and set a key.
Parameters
k |
The key to set. |
v |
The value to associate with |
Set type as DomType.Object
.
Retrieve all the keys associated with this Value
.
Throws
-
DOMException
if thisValue
is not aDomType.Object
.
Retrieve all the values associated with this Value
.
Throws
-
DOMException
if thisValue
is not aDomType.Object
.
Parse a JSON string into a Value
.