After finding a solution for serializing objects with known types in Javascript Serialization of Typed Objects, I now face a new challenge. I have an object of an unknown type that needs to be deserialized by code that is unaware of its specific type. The "Sprite" base class has properties that require serialization, with various derived classes like "Player" and "Platform" adding their own properties. Additionally, there is a "MapLayer" containing Sprite-derived objects. How can I ensure that each sprite will be correctly deserialized into its appropriate derived type? Should I resort to using eval("new " + derivedTypeName + parameterList)? Are there better alternatives?
Here are more specifics: Although the Sprite base class is fixed, all derived classes are generated dynamically. While I can make the code generator create deserialize functions for each derived class, how can I invoke them accordingly from the generic base class deserialization function? The single MapLayer class must somehow trigger the deserialize function for every class derived from Sprite.