Posted: 04/01/10 04: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.