fromJSON

Extract a value from a json object by its key.

Throws if json is not of JSONType.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 JSONValue

JSONValue to deserialize.

key string

key of desired value within the object.

options JsonizeOptions

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.

import jsonizer.jsonize;

static struct Foo {
  mixin JsonizeMe;
  @jsonize int i;
  @jsonize string[] a;
}

auto foo = `{"i": 12, "a": [ "a", "b" ]}`.parseJSON.fromJSON!Foo;
assert(foo == Foo(12, [ "a", "b" ]));

Meta