This is a follow-up question to Javascript Serialization of Typed Objects. That solution works OK for objects whose type is known, but now I have an object of a type that is unknown by the code that will be performing the de-serialization. There's a base class "Sprite" that has a number of properties that need to be serialized. Any number of derived classes (such as "Player" and "Platform" etc) may derive from class Sprite and add their own properties. I also have a "MapLayer" object that contains a collection of Sprite-derived objects. How do I de-serialize the layer and all of its sprites such that each sprite will be of the correct derived type when de-serialization is complete. Do I need to use eval("new " + derivedTypeName + parameterList)? Is there a better way?
More details: The Sprite base class is hard-coded, but all the derived classes are generated code. I can make the code generator generate deserialize functions for every derived class, but how do I call them appropriately from the generic base class deserialization function? There's only one MapLayer class, and somehow it has to potentially call the deserialize function on all the classes derived from Sprite.