Jsonize

Shortcut for setting both JsonizeIn and JsonizeOut

Values

ValueMeaning
yes1

equivalent to JsonizeIn.yes, JsonizeOut.yes

opt2

equivalent to JsonizeIn.opt, JsonizeOut.opt

Examples

Use of Jsonize(In,Out):

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 static struct S {
7   mixin JsonizeMe;
8 
9   @jsonize {
10     int i; // i is non-opt (default)
11     @jsonize(Jsonize.opt) {
12       @jsonize("_s") string s; // s is optional
13       @jsonize(Jsonize.yes) float f; // f is non-optional (overrides outer attribute)
14     }
15   }
16 }
17 
18 assertNotThrown(`{ "i": 5, "f": 0.2}`.parseJSON.fromJSON!S);
19 auto ex = collectException!JsonizeMismatchException(`{ "i": 5 }`.parseJSON.fromJSON!S);
20 
21 assert(ex !is null, "missing non-optional field 'f' should trigger JsonizeMismatchException");
22 assert(ex.targetType == typeid(S));
23 assert(ex.missingKeys == [ "f" ]);
24 assert(ex.extraKeys == [ ]);

Meta