pure nothrow @nogc @property @safe JSONType
type() const;
Returns the JSONType of the value stored in this structure.
Examples:string s = "{ \"language\": \"D\" }"; JSONValue j = parseJSON(s); writeln(j.type); writeln(j["language"].type);
pure @property @trusted string
str() const return scope;
pure nothrow @nogc @property @trusted string
str(return scope string
v) return;
Value getter/setter for JSONType.string.
Throws:JSONException for read access if type is not JSONType.string.
Examples:JSONValue j = [ "language": "D" ]; writeln(j["language"].str); j["language"].str = "Perl"; writeln(j["language"].str);
pure @property @safe long
integer() const;
pure nothrow @nogc @property @safe long
integer(long
v);
Value getter/setter for JSONType.
integer.Throws:JSONException for read access if type is not JSONType.
integer.pure @property @safe ulong
uinteger() const;
pure nothrow @nogc @property @safe ulong
uinteger(ulong
v);
Value getter/setter for JSONType.
uinteger.Throws:JSONException for read access if type is not JSONType.
uinteger.pure @property @safe double
floating() const;
pure nothrow @nogc @property @safe double
floating(double
v);
Value getter/setter for JSONType.float_. Note that despite the name, this is a 64-bit double, not a 32-bit float.
Throws:JSONException for read access if type is not JSONType.float_.
pure @property @safe bool
boolean() const;
pure nothrow @nogc @property @safe bool
boolean(bool
v);
Value getter/setter for boolean stored in JSON.
Throws:JSONException for read access if this.type is not JSONType.true_ or JSONType.false_.
Examples:JSONValue j = true; writeln(j.boolean); j.boolean = false; writeln(j.boolean); j.integer = 12; import std.exception : assertThrown; assertThrown!JSONException(j.boolean);
pure @property ref @system inout(JSONValue[string])
object() inout return;
pure nothrow @nogc @property @trusted JSONValue[string]
object(return scope JSONValue[string]
v);
Value getter/setter for unordered JSONType.
object.Throws:JSONException for read access if type is not JSONType.
objector the object is ordered.Note This is @system because of the following pattern:
auto a = &(json.object()); json.uinteger = 0; (*a)["hello"] = "world";
pure @property @trusted inout(JSONValue[string])
objectNoRef() inout;
Value getter for unordered JSONType.object. Unlike object, this retrieves the object by value and can be used in @safe code.
One possible caveat is that, if the returned value is null, modifications will not be visible:
JSONValue json; json.object = null; json.objectNoRef["hello"] = JSONValue("world"); assert("hello" !in json.object);
Throws:JSONException for read access if type is not JSONType.object.
pure @property ref @system inout(OrderedObjectMember[])
orderedObject() inout return;
pure nothrow @nogc @property @trusted OrderedObjectMember[]
orderedObject(return scope OrderedObjectMember[]
v);
Value getter/setter for ordered JSONType.object.
Throws:JSONException for read access if type is not JSONType.object or the object is unordered.
Note This is @system because of the following pattern:
auto a = &(json.orderedObject()); json.uinteger = 0; (*a)["hello"] = "world";
pure @property @trusted inout(OrderedObjectMember[])
orderedObjectNoRef() inout;
Value getter for ordered JSONType.object. Unlike orderedObject, this retrieves the object by value and can be used in @safe code.
pure @property @trusted bool
isOrdered() const;
Returns true if the order of keys of the represented object is being preserved.
pure @property ref @system inout(JSONValue[])
array() inout scope return;
pure nothrow @nogc @property @trusted JSONValue[]
array(return scope JSONValue[]
v) scope;
Value getter/setter for JSONType.
array.Throws:JSONException for read access if type is not JSONType.
array.Note This is @system because of the following pattern:
auto a = &(json.array()); json.uinteger = 0; (*a)[0] = "world";
pure @property @trusted inout(JSONValue[])
arrayNoRef() inout;
Value getter for JSONType.array. Unlike array, this retrieves the array by value and can be used in @safe code.
One possible caveat is that, if you append to the returned array, the new values aren't visible in the
JSONValue:
JSONValue json; json.array = [JSONValue("hello")]; json.arrayNoRef ~= JSONValue("world"); assert(json.array.length == 1);
Throws:JSONException for read access if type is not JSONType.array.
pure nothrow @nogc @property @safe bool
isNull() const;
Test whether the type is JSONType.null_
pure @property @safe inout
(T)get(T)() inout const;
pure @property @trusted inout(T)
get(T : JSONValue[string])() inout;
A convenience getter that returns this JSONValue as the specified D type.
Note Only numeric types, bool, string, JSONValue[string], and JSONValue[] types are accepted
Throws:JSONException if T cannot hold the contents of this JSONValue ConvException in case of integer overflow when converting to T
Examples:import std.exception; import std.conv; string s = `{ "a": 123, "b": 3.1415, "c": "text", "d": true, "e": [1, 2, 3], "f": { "a": 1 }, "g": -45, "h": ` ~ ulong.max.to!string ~ `, }`; struct a { } immutable json = parseJSON(s); writeln(json["a"].get!double); writeln(json["a"].get!int); writeln(json["a"].get!uint); writeln(json["b"].get!double); assertThrown!JSONException(json["b"].get!int); writeln(json["c"].get!string); writeln(json["d"].get!bool); assertNotThrown(json["e"].get!(JSONValue[])); assertNotThrown(json["f"].get!(JSONValue[string])); static assert(!__traits(compiles, json["a"].get!a)); assertThrown!JSONException(json["e"].get!float); assertThrown!JSONException(json["d"].get!(JSONValue[string])); assertThrown!JSONException(json["f"].get!(JSONValue[])); writeln(json["g"].get!int); assertThrown!ConvException(json["g"].get!uint); writeln(json["h"].get!ulong); assertThrown!ConvException(json["h"].get!uint); assertNotThrown(json["h"].get!float);
this
(T)(T
arg)
if (!isStaticArray!T);
this
(T)(ref T
arg)
if (isStaticArray!T);
this
(T : JSONValue)(inout T
arg) inout;
Constructor for JSONValue. If
argis a JSONValue its value and type will be copied to the new JSONValue. Note that this is a shallow copy: if type is JSONType.object or JSONType.array then only the reference to the data will be copied. Otherwise,argmust be implicitly convertible to one of the following types: typeof(null), string, ulong, long, double, an associative array V[K] for any V and K i.e. a JSON object, any array or bool. The type will be set accordingly.Examples:JSONValue j = JSONValue( "a string" ); j = JSONValue(42); j = JSONValue( [1, 2, 3] ); writeln(j.type); j = JSONValue( ["language": "D"] ); writeln(j.type);
enum JSONValue
emptyObject;
An enum value that can be used to obtain a JSONValue representing an empty JSON object.
Examples:JSONValue obj1 = JSONValue.emptyObject; writeln(obj1.type); obj1.object["a"] = JSONValue(1); writeln(obj1.object["a"]); JSONValue obj2 = JSONValue.emptyObject; assert("a" !in obj2.object); obj2.object["b"] = JSONValue(5); assert(obj1 != obj2);
enum JSONValue
emptyOrderedObject;
An enum value that can be used to obtain a JSONValue representing an empty JSON object. Unlike emptyObject, the order of inserted keys is preserved.
Examples:JSONValue obj = JSONValue.emptyOrderedObject; writeln(obj.type); assert(obj.isOrdered); obj["b"] = JSONValue(2); obj["a"] = JSONValue(1); writeln(obj["a"]); writeln(obj["b"]); string[] keys; foreach (string k, JSONValue v; obj) keys ~= k; writeln(keys);
enum JSONValue
emptyArray;
An enum value that can be used to obtain a JSONValue representing an empty JSON array.
Examples:JSONValue arr1 = JSONValue.emptyArray; writeln(arr1.type); writeln(arr1.array.length); arr1.array ~= JSONValue("Hello"); writeln(arr1.array.length); writeln(arr1.array[0]); JSONValue arr2 = JSONValue.emptyArray; writeln(arr2.array.length); assert(arr1 != arr2);
pure ref @safe inout(JSONValue)
opIndex(size_t
i) inout;
Array syntax for JSON arrays.
Throws:JSONException if type is not JSONType.array.
Examples:JSONValue j = JSONValue( [42, 43, 44] ); writeln(j[0].integer); writeln(j[1].integer);
pure ref @safe inout(JSONValue)
opIndex(return scope string
k) inout;
Hash syntax for JSON objects.
Throws:JSONException if type is not JSONType.object.
Examples:JSONValue j = JSONValue( ["language": "D"] ); writeln(j["language"].str);
void
opIndexAssign(T)(auto ref T
value, string
key);
void
opIndexAssign(T)(T
arg, size_t
i);
Provides support for index assignments, which sets the corresponding value of the JSON object's
keyfield tovalue.If the JSONValue is JSONType.null_, then this function initializes it with a JSON object and then performs the index assignment.
Throws:JSONException if type is not JSONType.object or JSONType.null_.
Examples:JSONValue j = JSONValue( ["language": "D"] ); j["language"].str = "Perl"; writeln(j["language"].str);
Examples:JSONValue j = JSONValue( ["Perl", "C"] ); j[1].str = "D"; writeln(j[1].str);
@safe inout(JSONValue)*
opBinaryRight(string op : "in")(string
k) inout;
Provides support for the in operator.
Tests whether a key can be found in an object.
Returns:When found, the inout(JSONValue)* that matches to the key, otherwise null.
Throws:JSONException if the right hand side argument JSONType is not object.
Examples:JSONValue j = [ "language": "D", "author": "walter" ]; string a = ("author" in j).str; *("author" in j) = "Walter"; writeln(j["author"].str);
pure nothrow @nogc @safe bool
opEquals(const JSONValue
rhs) const;
pure nothrow @nogc @trusted bool
opEquals(ref const JSONValue
rhs) const;
Compare two JSONValues for equality
JSON arrays and objects are compared deeply. The order of object keys does not matter.
Floating point numbers are compared for exact equality, not approximal equality.
Different number types (unsigned, signed, and floating) will be compared by converting them to a common type, in the same way that comparison of built-in D
int,
uintand
floatworks.
Other than that, types must match exactly. Empty arrays are not equal to empty objects, and booleans are never equal to integers.
Returns:whether this JSONValue is equal to
rhspure nothrow @nogc @trusted hash_t
toHash() const;
Calculate a numerical hash value for this value, allowing JSONValue to be used in associative arrays.
Examples:assert(JSONValue(10).opEquals(JSONValue(10.0))); assert(JSONValue(10) != (JSONValue(10.5))); assert(JSONValue(1) != JSONValue(true)); assert(JSONValue.emptyArray != JSONValue.emptyObject); assert(parseJSON(`{"a": 1, "b": 2}`).opEquals(parseJSON(`{"b": 2, "a": 1}`)));
@system int
opApply(scope int delegate(size_t index, ref JSONValue)
dg);
Implements the foreach
opApplyinterface for json arrays.@system int
opApply(scope int delegate(string key, ref JSONValue)
dg);
Implements the foreach
opApplyinterface for json objects.@safe string
toString(in JSONOptions
options= JSONOptions.none) const;
Implicitly calls toJSON on this JSONValue.
options can be used to tweak the conversion behavior.
void
toString(Out)(Out
sink, in JSONOptions
options= JSONOptions.none) const;
@safe string
toPrettyString(in JSONOptions
options= JSONOptions.none) const;
Implicitly calls toJSON on this JSONValue, like toString, but also passes true as pretty argument.
options can be used to tweak the conversion behavior
void
toPrettyString(Out)(Out
sink, in JSONOptions
options= JSONOptions.none) const;
US