fromJSON

Extract a value from a json object by its key.

Throws if json is not of JSONType.object. Return defaultVal if the key does not exist.

  1. T fromJSON(JSONValue json, JsonizeOptions options)
  2. T fromJSON(JSONValue json, string key, JsonizeOptions options)
  3. T fromJSON(JSONValue json, string key, T defaultVal, JsonizeOptions options)
    T
    fromJSON
    (
    T
    )

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.

defaultVal T

value to return if key is not found

options JsonizeOptions

configures the deserialization behavior.

Examples

Substitute default values when keys aren't present.

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

Meta