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):

import std.json            : parseJSON;
import std.exception       : collectException, assertNotThrown;
import jsonizer.jsonize    : JsonizeMe;
import jsonizer.fromjson   : fromJSON;
import jsonizer.exceptions : JsonizeMismatchException;
static struct S {
  mixin JsonizeMe;

  @jsonize {
    int i; // i is non-opt (default)
    @jsonize(Jsonize.opt) {
      @jsonize("_s") string s; // s is optional
      @jsonize(Jsonize.yes) float f; // f is non-optional (overrides outer attribute)
    }
  }
}

assertNotThrown(`{ "i": 5, "f": 0.2}`.parseJSON.fromJSON!S);
auto ex = collectException!JsonizeMismatchException(`{ "i": 5 }`.parseJSON.fromJSON!S);

assert(ex !is null, "missing non-optional field 'f' should trigger JsonizeMismatchException");
assert(ex.targetType == typeid(S));
assert(ex.missingKeys == [ "f" ]);
assert(ex.extraKeys == [ ]);

Meta