Download Reference Manual
The Developer's Library for D
About Wiki Forums Source Search Contact

Serialization

Moderators: kris

Posted: 03/31/10 08:37:03

I'm just wondering if serialization would fit within the goals of tango? If that would be a good addition to tango.

Author Message

Posted: 03/31/10 17:28:41 -- Modified: 03/31/10 17:29:53 by
kris

there are a few options to consider:

  • D-specific serialization -- this has been implemented in a variety of ways ranging from classes to mixins to templates (or a combination of all three). IMO, it needs direct compiler support to make it truly seamless and clean.
  • generic, language-agnostic serialization -- using an externally defined protocol via tools such as Thrift or ProtoBuffers to generate the appropriate D code.

The latter is attractive to many folks, and I think someone has begun work on ProtoBuffers for D. But yes ... it's something we're really interested in

Posted: 04/01/10 08:14:27

The reason I'm asking is because I've been working on a serialization library (D-specific) for a while and thought it might be a good addition to tango. I can agree that it would be better with some compiler additions/changes. I don't know how other have implemented their serialization libraries but I'm quite satisfied how the API/interface turned out for the library, especially with the lack of compiler support.

The the library can serialize most of the types: objects, structs, strings, arrays, associative arrays, pointers, typedefs and primitives. All these types can be serialized without any modification on the existing types, the only special case is when an object is serialized through a base class reference, then it requires the user to register a method/function to do the serialization (basically just cast the object to the right type and then pass it back to the serializer).

The API basically looks like this:

class A {
	int x;
	
	equals_t opEquals (Object other) {
		if (auto a = cast(A) other)
			return a.x == x;					
		
		return false;
	}
}

A a = new A;

auto serializer = new Serializer!(XMLArchive!(char));
auto data = serializer.serialize(a);
auto a2 = serializer.deserialize!(A)(data);

assert(a == a2);

The library also support fields that should not be serialized and you can register methods to be event handlers that are called before and after serialization and deserialization. The library is almost complete, I think the only thing I have left to implement, that I've planed, is some kind of system to (de)serialize different versions of classes/structs. Currently the only supported archive format is XML.

Posted: 08/28/10 12:14:54

If there's still interest in this my library is now available on dsource: http://dsource.org/projects/orange/