JsonizeIgnoreExtraKeys

Whether to silently ignore json keys that do not map to serialized members.

Values

ValueMeaning
no

silently ignore extra keys in the json object being deserialized

yes

fail if the json object contains a keys that does not map to a serialized field

Examples

Use of JsonizeIgnoreExtraKeys:

import std.json            : parseJSON;
import std.exception       : collectException, assertNotThrown;
import jsonizer.jsonize    : JsonizeMe;
import jsonizer.fromjson   : fromJSON;
import jsonizer.exceptions : JsonizeMismatchException;

static struct NoCares {
  mixin JsonizeMe;
  @jsonize {
    int i;
    float f;
  }
}

static struct VeryStrict {
  mixin JsonizeMe!(JsonizeIgnoreExtraKeys.no);
  @jsonize {
    int i;
    float f;
  }
}

// no extra fields, neither should throw
assertNotThrown(`{ "i": 5, "f": 0.2}`.parseJSON.fromJSON!NoCares);
assertNotThrown(`{ "i": 5, "f": 0.2}`.parseJSON.fromJSON!VeryStrict);

// extra field "s"
// `NoCares` ignores extra keys, so it will not throw
assertNotThrown(`{ "i": 5, "f": 0.2, "s": "hi"}`.parseJSON.fromJSON!NoCares);
// `VeryStrict` does not ignore extra keys
auto ex = collectException!JsonizeMismatchException(
    `{ "i": 5, "f": 0.2, "s": "hi"}`.parseJSON.fromJSON!VeryStrict);

assert(ex !is null, "extra field 's' should trigger JsonizeMismatchException");
assert(ex.targetType == typeid(VeryStrict));
assert(ex.missingKeys == [ ]);
assert(ex.extraKeys == [ "s" ]);

Meta