Conceptually, my app has an interface and many objects that implement the interface in different ways. The objects will be created by various factory methods, and there will be more factories in future as my app evolves.
For example
var Shapes={};
var Shapes.createCircle=function(radius) {
return {draw:function...}
};
var Shapes.createRectangle=function(width,height) {
return {draw:function...}
};
I want to serialize these shapes in files. I plan to make each object store the name of its factory (e.g. "Shapes.createRectangle") and an array of arguments (e.g [10,20]), and then save these parts as a JSON string. That way, the deserialized objects can have all their functions restored, as well as their properties.
For example, to save an object I plan to store the JSON string
{"factory":"Shapes.createRectangle","args":[10,20]}
and then reconstruct the object in a future sessions by dynamically calling the factory method again.
Does this method of serializing polymorphic objects have any gotchas?
{"factory":"Shapes.createRectangle","args":[10,20]}.