[phobos] Interest in having a serializer in Phobos?

Jacob doob at me.com
Sun Aug 8 12:56:46 PDT 2010


On 8 aug 2010, at 21:35, Andrei Alexandrescu wrote:

> On 08/08/2010 01:39 PM, Jacob wrote:
>> On 8 aug 2010, at 17:27, Andrei Alexandrescu wrote:
>>> Deserialize into Object and then cast the Object to Shape.
>> 
>> To be able to cast it to a Shape you need to know the type at compile
>> time when you deserialize it. Or you have to register a method that
>> deserializes the object, which is exactly how it works now when you
>> deserialize through a base class reference.
> 
> You only need to know the _base_ type statically.

Yes, but using Object as the static type is not enough, see my example below.

>>>> Or would Variant be useful here? I have not used Variant.
>>> 
>>> Probably Variant would play a role when e.g. one wants to
>>> deserialize "the next primitive type" without needing to know
>>> exactly what type that is (e.g. different integer widths).
>>> 
>>> 
>>> Andrei _______________________________________________ phobos
>>> mailing list phobos at puremagic.com
>>> http://lists.puremagic.com/mailman/listinfo/phobos
>> 
>> I'm not sure if we understand each other correctly.
> 
> Most likely - sorry about that. Don't forget that all I'm going by is the tutorial, which is very brief.

I know, I will expand the tutorials and examples.

>> If you
>> deserialize into Object you eventually need to cast it to something
>> more useful and then you probably could have deserialized to that
>> type in the first place. The library can deserialize through base
>> class references (by register a deserialize method) but you would
>> have to start with a static type somewhere, not just Object. Do you
>> have a simple (code) example describing what you want to do?
> 
> I think the Shape example is simple enough to serve as a good baseline. Say you have a hierarchy rooted in Shape including e.g. Triangle, Circle, and Rectangle. Now say you have a drawing represented as a Shape[]. What steps do you need to take to save the drawing to disk and restore it later?

1. Create a new instance of the serializer
(a or b)
2a. Register a serializer and deserializer method for each runtime type with the serializer 
2b. Implement a toData and fromData method in each class
(you don't have to register methods for the static type, Shape in this case)
3. Serialize the array
4. Deserialize the array as the static type Shape[]

Now every object in the array should have been deserialized to its runtime type. I'll add a code example for this on the project page.

> Andrei
> _______________________________________________
> phobos mailing list
> phobos at puremagic.com
> http://lists.puremagic.com/mailman/listinfo/phobos



More information about the phobos mailing list