Here's one way of doing it, using a reviver function. The advantage is that the parsing process directly takes care of instantiating the correct object types, rather than doing it in two steps.
var shapes = { 'Rectangle': Rectangle }, //same as THREE in your code
shapeJson = '{ "shape": ["Rectangle", [2, 4]] }',
shape = shapeFromJson(shapeJson);
console.log(shape);
function instanciate(ctor, args) {
var instance = Object.create(ctor.prototype);
ctor.apply(instance, args);
return instance;
}
function createShape(shape, args) {
return instanciate(shapes[shape], args);
}
function shapeFromJson(json) {
return JSON.parse(json, function (key, val) {
return key? val : createShape(val.shape[0], val.shape[1]);
});
}
function Rectangle(height, width) {
this.height = height;
this.width = width;
}