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:

1 import std.json            : parseJSON;
2 import std.exception       : collectException, assertNotThrown;
3 import jsonizer.jsonize    : JsonizeMe;
4 import jsonizer.fromjson   : fromJSON;
5 import jsonizer.exceptions : JsonizeMismatchException;
6 
7 static struct NoCares {
8   mixin JsonizeMe;
9   @jsonize {
10     int i;
11     float f;
12   }
13 }
14 
15 static struct VeryStrict {
16   mixin JsonizeMe!(JsonizeIgnoreExtraKeys.no);
17   @jsonize {
18     int i;
19     float f;
20   }
21 }
22 
23 // no extra fields, neither should throw
24 assertNotThrown(`{ "i": 5, "f": 0.2}`.parseJSON.fromJSON!NoCares);
25 assertNotThrown(`{ "i": 5, "f": 0.2}`.parseJSON.fromJSON!VeryStrict);
26 
27 // extra field "s"
28 // `NoCares` ignores extra keys, so it will not throw
29 assertNotThrown(`{ "i": 5, "f": 0.2, "s": "hi"}`.parseJSON.fromJSON!NoCares);
30 // `VeryStrict` does not ignore extra keys
31 auto ex = collectException!JsonizeMismatchException(
32     `{ "i": 5, "f": 0.2, "s": "hi"}`.parseJSON.fromJSON!VeryStrict);
33 
34 assert(ex !is null, "extra field 's' should trigger JsonizeMismatchException");
35 assert(ex.targetType == typeid(VeryStrict));
36 assert(ex.missingKeys == [ ]);
37 assert(ex.extraKeys == [ "s" ]);

Meta