Not currently. It can be worked around somewhat by providing a custom
toRepresentation
method:struct MyStruct { int value = 42; Json[string] toRepresentation() { Json[string] ret; if (value != 42) ret["value"] = value; return ret; } static MyStruct fromRepresentation(Json[string] j) { MyStruct ret; if (auto pv = "value" in j) ret.value = pv.get!int; return ret; } }
As far as I understand I need to implement serialization for whole object in this case?
Instead of the
@optional!canSerialize
another (or an additional) possibility would be to handle@optional Nullable!int value;
in that way.
Doesn't work for string[]
for example.
Of course, skipping the value if it equals
.init
, like suggested in the OP, is also a possibility.Any further opinions or use cases?
REST server accept only one of two fields and reject request if you send both for example.