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 Apr 8, 2017
1 parent de569e0 commit 6d3736e
Show file tree
Hide file tree
Showing 2 changed files 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 @@ -492,8 +492,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 @@ -1707,3 +1713,23 @@ unittest {
auto deser = deserialize!(TestSerializer, Custom)(ser);
assert(deser.x == 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) == "{}");
}
Binary file added vibe-d-test-libevent
Binary file not shown.

0 comments on commit 6d3736e

Please sign in to comment.