Serialization of derived classes

Hi,

I’m trying to save and load the state of the application, using a simple XML file using ofxXmlSerializer.

Everything works fine when using non-derived classes, but when introducing polymorphism I can’t deserialize anymore, as I don’t know a priori the derived class that should be constructed.

Do you know of any solution? (using ofBaseSerializable if possible…)

Thanks

you can use typeof(…) and check all the possible cases, there’ s no other solution in c++ since it doesn’t have introspection

like you can use:

if (typeof(object) == typeof(SomeDerivedClass)){
   ...
}else if (typeof(object) == typeof(SomeOtherDerivedClass)){
   ...
}

Hi arturo,

The main issue is that I have to send an ofParameterGroup containing the appropiate fields of the derived type to the ofBaseSerializer.deserialize() in order to load the field values into the group, but I don’t know the correct type yet.

class Base {
    float distance;
}

class FooBase : public Base{
    int number;
}

class BooBase : public Base{
    string name;
}

Serializing FooBase:

<FooBase>
<distance>2.0</distance>
<number>1</number>
</FooBase>

Calling ofBaseSerializer to deserialize this xml would require an ofParameterGroup with fields distance and number, but I don’t know I require this fields until I read the “tag” is “FooBase”.

This “manual” xml reading would mean I can’t use ofBaseSerializer.deserialize() any more.