RejectedSoftware Forums

Sign up

Use of null values in serialization

Databases, MongoDB included have a concept of a null value. Unfortunately value types in D do not. D has a standard way of creating a nullable value in std.typecons.

Looking at the serialization code in vibe, the only way to work with null values is to use classes. However, in the project I am porting I rely on null to work out if a user has set a boolean type or not. Take this example:

struct User {
    bool subscribed;
}

I ideally I would like to know if the user has subscribed, unsubscribed or hasn't answered at all. The simplest solution in D would be to use Nullable!bool as the the type. However the serialization framework doesn't support this.

My questions are twofold.

  • Is there a way I can retrofit this kind of serialization to work with my existing models without changing the core Vibe.d code?
  • Failing that, is there a reason we shouldn't modify the serialization code to recognise Nullable types?

Below I have unit tests which have a sample usage which I think would be ideal. Thanks in advance!

unittest {
	import std.typecons;
	import vibe.data.json;

	struct User {
		Nullable!bool subscribed;
	}

	User u;

	auto json = serializeToJson(u);
	assert(json.subscribed == Json.undefined);
}

unittest {
	import std.typecons;
	import vibe.data.json;

	auto json = Json.emptyObject;

	json.subscribed = Json.undefined;

	struct User {
		Nullable!bool subscribed;
	}

	User u;
	u.subscribed = true; // Set this so we can be sure the deserialize sets it back to null

	deserializeJson(u, json);
	assert(u.subscribed.isNull);
}

Re: Use of null values in serialization

On Tue, 26 Aug 2014 00:33:19 GMT, David Monagle wrote:

  • Failing that, is there a reason we shouldn't modify the serialization code to recognise Nullable types?

In fact, the serialization code supports Nullable!T since a few weeks! (See https://github.com/rejectedsoftware/vibe.d/blob/master/source/vibe/data/serialization.d#L222)

So in particular, starting from 0.7.21-alpha.2 your code should work as it is.

Re: Use of null values in serialization

On Tue, 26 Aug 2014 06:32:13 GMT, Sönke Ludwig wrote:

In fact, the serialization code supports Nullable!T since a few weeks! (See https://github.com/rejectedsoftware/vibe.d/blob/master/source/vibe/data/serialization.d#L222)

Thank you Sönke! I missed that. May I say it's been a pleasure working with this framework so far. It's also fantastic that you are so diligent answering questions on this forum, sorry for asking about a solved issue!

Re: Use of null values in serialization

Am 26.08.2014 09:47, schrieb David Monagle:

On Tue, 26 Aug 2014 06:32:13 GMT, Sönke Ludwig wrote:

In fact, the serialization code supports Nullable!T since a few weeks! (See https://github.com/rejectedsoftware/vibe.d/blob/master/source/vibe/data/serialization.d#L222)

Thank you Sönke! I missed that. May I say it's been a pleasure working with this framework so far. It's also fantastic that you are so diligent answering questions on this forum, sorry for asking about a solved issue!

Thanks! And no problem for asking about this, I wouldn't expect anyone
to read the current source code for breakfast or to keep track of the
commit log ;)

Re: Use of null values in serialization

On Tue, 26 Aug 2014 11:36:32 +0200, Sönke Ludwig wrote:

Thanks! And no problem for asking about this, I wouldn't expect anyone
to read the current source code for breakfast or to keep track of the
commit log ;)

Perhaps embarrassingly, I have spent many a breakfast reading over your source code. I think D is a great language and a good move from C++ for my needs. Vibe is what made it an extremely attractive move. I commend your source code, very readable and has taught me an awful lot about the practical uses of the D language.