[phobos] Interest in having a serializer in Phobos?

Michel Fortin michel.fortin at michelf.com
Sun Aug 8 08:09:54 PDT 2010


Le 2010-08-08 à 10:37, Jacob a écrit :

> Yes, exactly, that is how the library currently works. But I can see how starting out by deserializing with the type Object could work. This is a description of how the serializer "thinks" when it deserializes a value:
> 
> auto a2 = serializer.deserialize!(Foo)(data);
> 
> "Ok, I'm deserializing a Foo"
> 
> 1. Start by creating a new instance of Foo
> 2. Loop through all the instance variables
> 
> "Oh, I found a struct of the type Bar"
> 
> 1. Create a new Bar
> 2. Loop through all the instance variables
> 3. Deserialize the values for each variable
> 4. Set the values for all the instance variables
> 
> 3. Set the value for the instance variable of type Bar
> 
> continue deserializing...
> 
> Using the approach above I have all (or as much as possible) compile time information available, like the types of all the instance variables, the serializer is in control. Using Andrei's approach it seems more like this:
> 
> "Start looking in the archive after types"
> "Ok, the archive wants me to deserialize an instance of Foo"
> 
> 1. Start by creating a new instance of Foo using reflection:
> 
> Object foo = Object.factory("Foo");
> 
> 2. In the archive, loop through all the instance variables
> 3. See if there is a corresponding field in the deserialized object by loop through all the instance variables using foo.getMembers
> 4. "Ok the archive wants me to deserialize a struct of the type Bar, hm how do I do that? I only have Bar as a string"
> 
> Using this approach all compile time information is lost, the archive is in control. Probably not the best explanation.

A better way to say it is that the archive tells you which class to instantiate, and this class should tell you (at runtime) how it can deserialize itself.

With my serialization module a class needs to be defined in a special way to be serializable: it needs to implement the encode/decode methods of the KeyArchivable interface (a mixin can implement them for you). And the class needs to have a default constructor. Or you could define an external handler for a certain class and register it prior unserialization. There's no other way with the current state of runtime-reflection in D.

-- 
Michel Fortin
michel.fortin at michelf.com
http://michelf.com/





More information about the phobos mailing list