Skip to content

Commit

Permalink
add a special case for Json/Bson optional fields
Browse files Browse the repository at this point in the history
  • Loading branch information
tchaloupka committed May 14, 2017
1 parent 70a87c2 commit a130be9
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions data/vibe/data/serialization.d
Original file line number Diff line number Diff line change
Expand Up @@ -495,8 +495,14 @@ private template serializeValueImpl(Serializer, alias Policy) {

//skip optional attributes from serialization
if ((opt & OptionalDirection.out_) == OptionalDirection.out_) {
import vibe.data.json : Json;
import vibe.data.bson : Bson;
static if (isInstanceOf!(Nullable, typeof(vt))) {
if (vt.isNull) continue;
} else static if (is(typeof(vt) == Json)) {
if (vt.type == Json.Type.undefined || vt.type == Json.Type.null_ || (vt.type == Json.Type.object && !vt.length)) continue;
} else static if (is(typeof(vt) == Bson)) {
if (vt.type == Bson.Type.undefined || vt.type == Bson.Type.null_ || (vt.type == Bson.Type.object && !vt.length)) continue;
} else {
if (vt == typeof(vt).init) continue;
}
Expand Down Expand Up @@ -1721,3 +1727,23 @@ unittest {
auto deser = deserialize!(TestSerializer, T)(ser);
assert(deser == 42);
}

unittest {
import vibe.data.json;
struct Foo {
@optional
Json bar;
}
Foo f;
assert(serializeToJsonString(f) == "{}");
}

unittest {
import vibe.data.bson;
struct Foo {
@optional
Bson bar;
}
Foo f;
assert(serializeToJsonString(f) == "{}");
}

0 comments on commit a130be9

Please sign in to comment.