[phobos] Interest in having a serializer in Phobos?

Jacob doob at me.com
Sun Aug 8 07:37:47 PDT 2010


On 8 aug 2010, at 15:30, Michel Fortin wrote:

> Le 2010-08-08 à 6:46, Jacob a écrit :
> 
>> On 8 aug 2010, at 07:47, Andrei Alexandrescu wrote:
>> 
>>> I think that would be great. Knowing nothing about Orange, I visited the website and read the feature lists and the tutorial (the reference seems to be missing for now). The latter contains:
>>> 
>>> auto a2 = serializer.deserialize!(A)(data);
>>> 
>>> which seems to require compile-time knowledge of the deserialized type. I'd expect the library to support something like
>>> 
>>> Object a2 = serializer.deserialize!Object(data);
>> 
>> This is currently not possible in the library. I'm not sure if that would be possible, how would you deserialize a struct for example? There is no factory function for structs like there is for classes.
> 
> But there's no concept of derived struct. For a struct you always know the type at compile-time. The only way to hide a struct would be behind a void* or void[], but trying to serialize/unserialize that type automatically (without the user writing the serialization code itself) is pointless.

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.

> Or you could hide it behind a variant, in which case the variant's serialization should remember the type name so it can find a proper deserializer on the other side. How exactly it does that? Either with better runtime-reflection, or with pre-registered handlers on the unserializer's side (not very convenient).
> 
> 
>> Since all the static types of the objects would be Object how would I set the values when deserializing? Or would Variant be useful here? I have not used Variant.
> 
> At this point I've been unable to serialize/unserialize a variant.
> 
> Another interesting point: it's probably necessary to be tolerant of type differences. For instance, if I serialize a size_t on a machine and unserialize it elsewhere, it might not be the same underlying integral type.

That deserves to think about.

> -- 
> Michel Fortin
> michel.fortin at michelf.com
> http://michelf.com/
> 
> 
> 
> _______________________________________________
> phobos mailing list
> phobos at puremagic.com
> http://lists.puremagic.com/mailman/listinfo/phobos

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.puremagic.com/pipermail/phobos/attachments/20100808/150d9a51/attachment.html>


More information about the phobos mailing list