fromJSON

Extract a value from a json object by its key.

Throws if json is not of JSON_TYPE.OBJECT or the key does not exist.

Parameters

T

Target type. can be any primitive/builtin D type, or any user-defined type using the JsonizeMe mixin.

json
Type: JSONValue

JSONValue to deserialize.

key
Type: string

key of desired value within the object.

options

configures the deserialization behavior.

Examples

Directly extract values from an object by their keys.

auto aa = ["a": 1, "b": 2];
auto json = JSONValue(aa);
assert(json.fromJSON!int("a") == 1);
assert(json.fromJSON!ulong("b") == 2L);

Deserialize an instance of a user-defined type from a json object.

1 import jsonizer.jsonize;
2 
3 static struct Foo {
4   mixin JsonizeMe;
5   @jsonize int i;
6   @jsonize string[] a;
7 }
8 
9 auto foo = `{"i": 12, "a": [ "a", "b" ]}`.parseJSON.fromJSON!Foo;
10 assert(foo == Foo(12, [ "a", "b" ]));

Meta