#Serializing a list of parent objects to JSON while keeping the information from their children

1 messages · Page 1 of 1 (latest)

kind lodge
#

I have a list of LevelObjectData objects, and they hold information that I want to save from my level editor. LevelObjectData has basic stuff like position, scale, rotation. That's serializing fine. The problem is that I have specific classes that inherit from LevelObjectData, for example RacerData, that have extra data specific to Racer objects. When I save the data, I do new LevelObjectData currentObject; and then a switch statement where, based on the object type, I instantiate currentObject as, for example, RacerData. Then I add it to a list of LevelObjectData in order to serialize it to JSON. However, while the data from the parent class LevelObjectData saves correctly, it does not save any extra information from the child classes that I instantiate each object as

civic hornet
#

Yep. That's expected. The serializer doesn't know anything about your sub classes. It serializes whatever type you're passing into it. Also, it might not be physically possible to have a polymorphic array serialized. It would need to be a json object, which is not what a list/array get serialized into.
Normally, you'd have separate lists foe each of the subclass serialized separately.

kind lodge
#

Yeah having multiple lists was the solution I just came up with a few minutes ago, though I was hoping there was a more elegant solution

#

Though if it works, it works. Thanks for responding