Converting common types
assert(fromJSON!int(JSONValue(1)) == 1); assert(fromJSON!double(JSONValue(1.0)) == 1); assert(fromJSON!double(JSONValue(1.3)) == 1.3); assert(fromJSON!string(JSONValue("str")) == "str"); assert(fromJSON!bool(JSONValue(true)) == true); assert(fromJSON!bool(JSONValue(false)) == false); assert(fromJSON!JSONValue(JSONValue(true)) == JSONValue(true));
Converting arrays
assert(equal(fromJSON!(int[])(toJSON([1, 2])), [1, 2]));
Associative arrays
string[int] aa = [0 : "a", 1 : "b"]; auto aaCpy = fromJSON!(string[int])(toJSON(aa)); foreach (k, v; aa) { assert(aaCpy[k] == v); }
Structs from JSON
auto p = fromJSON!Point(parseJSON(q{{"x":-1,"y":2}})); assert(p.x == -1); assert(p.y == 2); p = fromJSON!Point(parseJSON(q{{"x":2}})); assert(p.x == 2); assert(p.y == 1); p = fromJSON!Point(parseJSON(q{{"y":3}})); assert(p.x == 0); assert(p.y == 3); p = fromJSON!Point(parseJSON(q{{"x":-1,"y":2,"z":3}})); assert(p.x == -1); assert(p.y == 2);
Class from JSON
auto p = fromJSON!PointC(parseJSON(q{{"x":-1,"y":2}})); assert(p.x == -1); assert(p.y == 2);
Convert class from JSON using "fromJSON"
auto p = fromJSON!PointPrivate(parseJSON(q{{"x":-1,"y":2}})); assert(p.x == -1); assert(p.y == 2);
Convert from JSONValue to any other type