Hello,
I'm currently struggeling a bit with inheritance on my models. What
i'm looking for is a way to have the serializer recognize the class
type for a property which can contain derived types. Example:
-------------------
public class BaseModel {
...
}
public class DerivedModelA extends BaseModel {
...
}
public class DerivedModelB extends BaseModel {
...
}
public class DerivedModelC extends BaseModel {
...
}
public class MainModel {
public var property:BaseModel;
}
-------------------
Now i would like to be able to set an instance of DerivedModelA,
DerivedModelB or DerivedModelC to MainModel.property, and have the
serializer correctly recognize it on serialization and
deserialization, ie when deserializing i'd like to have
MainModel.property to contain the type that was defined when
serializing, ie DerivedModelA, DerivedModelB or DerivedModelC.
I have searched through the sources, and if i'm not mistaken it all
comes down to DescriptionContext.getIncomingType, where the type is
retreived in case getRuntimeType ist set to true. As far as i
inderstood it only checks the element name and the namespace. So in
the end that would mean that i would have to create my own
serialization context so that i can override
DescriptionContext.getIncomingType to handle something like that - is
that correct?
I have a .NET background where i would simply use the XmlInclude
attribute for the base model to define the derived types, something
like this:
-------------------
[XmlInclude(typeof(DerivedModelA))]
[XmlInclude(typeof(DerivedModelB))]
[XmlInclude(typeof(DerivedModelC))]
public abstract class BaseModel {
...
}
public class MainModel {
public BaseModel property {
...
}
}
-------------------
The serializer is then able to recognize the various types for the
BaseModel typed property, and creates an appropriate type attribute
when serializing, something like this:
-------------------
<?xml version="1.0" encoding="utf-8"?>
<main xmlns:xsi="
http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="
http://www.w3.org/2001/XMLSchema">
<property xsi:type="DerivedModelA" />
</main>
-------------------
When deserializing it will use the type attribute to determine the
class type. I wish there were something similar in FlexXB :)
Regards, Tim