Sourcestd/json.d
import std.conv : to;// parse a file or string of json into a usable structurestring s =`{ "language": "D", "rating": 3.5, "code": "42" }`;JSONValue j = parseJSON(s);// j and j["language"] return JSONValue,// j["language"].str returns a stringwriteln(j["language"].str);// "D"writeln(j["rating"].floating);// 3.5// check a typelong x;if (const(JSONValue)* code ="code"in j){if (code.type() == JSONType.integer) x = code.integer;else x = to!int(code.str);}// create a json structJSONValue jj = ["language":"D" ];// rating doesnt exist yet, so use .object to assignjj.object["rating"] = JSONValue(3.5);// create an array to assign to listjj.object["list"] = JSONValue( ["a","b","c"] );// list already exists, so .object optionaljj["list"].array ~= JSONValue("D");string jjStr =`{"language":"D","list":["a","b","c","D"],"rating":3.5}`;writeln(jj.toString);// jjStr
JSONFloatLiteral: string;naninfnegativeInfJSONOptions: int;nonespecialFloatLiteralsescapeNonAsciiCharsdoNotEscapeSlashesstrictParsingpreserveObjectOrderJSONType: byte;null_stringintegeruintegerfloat_arrayobjecttrue_false_JSONValue;type() const;string s ="{ \"language\": \"D\" }";JSONValue j = parseJSON(s);writeln(j.type);// JSONType.objectwriteln(j["language"].type);// JSONType.string
str() const return scope;str(return scope stringv) return;JSONValue j = ["language":"D" ];// get valuewriteln(j["language"].str);// "D"// change existing key to new stringj["language"].str ="Perl";writeln(j["language"].str);// "Perl"
integer() const;integer(longv);integer.integer.uinteger() const;uinteger(ulongv);uinteger.uinteger.floating() const;floating(doublev);boolean() const;boolean(boolv);JSONValue j =true;writeln(j.boolean);// truej.boolean =false;writeln(j.boolean);// falsej.integer = 12;import std.exception : assertThrown;assertThrown!JSONException(j.boolean);
object() inout return;object(return scope JSONValue[string]v);object.object or the object is ordered.Note This is @system because of the following pattern:
auto a = &(json.object());json.uinteger = 0;// overwrite AA pointer(*a)["hello"] ="world";// segmentation fault
objectNoRef() inout;JSONValue json;json.object =null;json.objectNoRef["hello"] = JSONValue("world");assert("hello" !in json.object);
orderedObject() inout return;orderedObject(return scope OrderedObjectMember[]v);Note This is @system because of the following pattern:
auto a = &(json.orderedObject());json.uinteger = 0;// overwrite AA pointer(*a)["hello"] ="world";// segmentation fault
orderedObjectNoRef() inout;isOrdered() const;array() inout scope return;array(return scope JSONValue[]v) scope;array.array.Note This is @system because of the following pattern:
auto a = &(json.array());json.uinteger = 0;// overwrite array pointer(*a)[0] ="world";// segmentation fault
arrayNoRef() inout;JSONValue json;json.array = [JSONValue("hello")];json.arrayNoRef ~= JSONValue("world");assert(json.array.length == 1);
isNull() const;get(T)() inout const;get(T : JSONValue[string])() inout;NoteOnly numeric types,bool,string,JSONValue[string], andJSONValue[] types are accepted
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);// 123.0writeln(json["a"].get!int);// 123writeln(json["a"].get!uint);// 123writeln(json["b"].get!double);// 3.1415assertThrown!JSONException(json["b"].get!int);writeln(json["c"].get!string);// "text"writeln(json["d"].get!bool);// trueassertNotThrown(json["e"].get!(JSONValue[]));assertNotThrown(json["f"].get!(JSONValue[string]));staticassert(!__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);// -45assertThrown!ConvException(json["g"].get!uint);writeln(json["h"].get!ulong);// ulong.maxassertThrown!ConvException(json["h"].get!uint);assertNotThrown(json["h"].get!float);
arg)arg)arg) inout;arg is aJSONValue its value and type will be copied to the newJSONValue. Note that this is a shallow copy: if type isJSONType.object orJSONType.array then only the reference to the data will be copied. Otherwise,arg must be implicitly convertible to one of the following types:typeof(null),string,ulong,long,double, an associative arrayV[K] for anyV andK i.e. a JSON object, any array orbool. The type will be set accordingly.JSONValue j = JSONValue("a string" );j = JSONValue(42);j = JSONValue( [1, 2, 3] );writeln(j.type);// JSONType.arrayj = JSONValue( ["language":"D"] );writeln(j.type);// JSONType.object
emptyObject;JSONValue obj1 = JSONValue.emptyObject;writeln(obj1.type);// JSONType.objectobj1.object["a"] = JSONValue(1);writeln(obj1.object["a"]);// JSONValue(1)JSONValue obj2 = JSONValue.emptyObject;assert("a" !in obj2.object);obj2.object["b"] = JSONValue(5);assert(obj1 != obj2);
emptyOrderedObject;JSONValue obj = JSONValue.emptyOrderedObject;writeln(obj.type);// JSONType.objectassert(obj.isOrdered);obj["b"] = JSONValue(2);obj["a"] = JSONValue(1);writeln(obj["a"]);// JSONValue(1)writeln(obj["b"]);// JSONValue(2)string[] keys;foreach (string k, JSONValue v; obj) keys ~= k;writeln(keys);// ["b", "a"]
emptyArray;JSONValue arr1 = JSONValue.emptyArray;writeln(arr1.type);// JSONType.arraywriteln(arr1.array.length);// 0arr1.array ~= JSONValue("Hello");writeln(arr1.array.length);// 1writeln(arr1.array[0]);// JSONValue("Hello")JSONValue arr2 = JSONValue.emptyArray;writeln(arr2.array.length);// 0assert(arr1 != arr2);
opIndex(size_ti) inout;JSONValue j = JSONValue( [42, 43, 44] );writeln(j[0].integer);// 42writeln(j[1].integer);// 43
opIndex(return scope stringk) inout;JSONValue j = JSONValue( ["language":"D"] );writeln(j["language"].str);// "D"
opIndexAssign(T)(auto ref Tvalue, stringkey);opIndexAssign(T)(Targ, size_ti);key field tovalue.JSONValue j = JSONValue( ["language":"D"] );j["language"].str ="Perl";writeln(j["language"].str);// "Perl"
JSONValue j = JSONValue( ["Perl","C"] );j[1].str ="D";writeln(j[1].str);// "D"
opBinaryRight(string op : "in")(stringk) inout;JSONValue j = ["language":"D","author":"walter" ];string a = ("author"in j).str;*("author"in j) ="Walter";writeln(j["author"].str);// "Walter"
opEquals(const JSONValuerhs) const;opEquals(ref const JSONValuerhs) const;rhstoHash() const;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}`)));
opApply(scope int delegate(size_t index, ref JSONValue)dg);opApply interface for json arrays.opApply(scope int delegate(string key, ref JSONValue)dg);opApply interface for json objects.toString(in JSONOptionsoptions = JSONOptions.none) const;toString(Out)(Outsink, in JSONOptionsoptions = JSONOptions.none) const;toPrettyString(in JSONOptionsoptions = JSONOptions.none) const;toPrettyString(Out)(Outsink, in JSONOptionsoptions = JSONOptions.none) const;parseJSON(T)(Tjson, intmaxDepth = -1, JSONOptionsoptions = JSONOptions.none)Tjson | json-formatted string to parse |
intmaxDepth | maximum depth of nesting allowed, -1 disables depth checking |
JSONOptionsoptions | enable decoding string representations of NaN/Inf as float values |
parseJSON(T)(Tjson, JSONOptionsoptions)Tjson | json-formatted string to parse |
JSONOptionsoptions | enable decoding string representations of NaN/Inf as float values |
toJSON(ref const JSONValueroot, in boolpretty = false, in JSONOptionsoptions = JSONOptions.none);pretty is false no whitespaces are generated.Ifpretty is true serialized string is formatted to be human-readable.Set theJSONOptions.specialFloatLiterals flag is set inoptions to encode NaN/Infinity as strings.toJSON(Out)(auto ref Outjson, ref const JSONValueroot, in boolpretty = false, in JSONOptionsoptions = JSONOptions.none)JSONException:object.Exception;