When using IE 11, I encountered an issue where calling Stringify on my objects did not recursively call toJson on all objects in the tree. I have implemented a custom toJson function.
Object.prototype.toJSON = function() {
if (!(this.constructor.name == "Array")) {
if (JSON._serialized[this._id]) {
if (this.material) {
return {
name: this.name,
material: this.material
};
} else {
return { referenceId: this._id };
}
}
var json = this.toJSONProperties();
json._id = this._id;
json._runID = this._runID;
json._className = this.constructor.name;
JSON._serialized[this._id] = true;
return json;
} else {
return this;
}
}
This toJson function works perfectly fine in chrome, firefox, and safari. However, it only gets called on the top level object when I try to stringify the object in IE 11 using
var json = JSON.stringify(object);
.
The concept I am trying to implement is to have a generic toJson function for all classes, and use var json = this.toJSONProperties();
on specific classes that I want to convert. Any thoughts on why this behavior only occurs in IE?
Update:
I managed to fix the issue by discovering that this.constructor.name
does not work as intended in IE.
By changing it to
this.constructor.toString().match(/function (.{1,})\(/)[1]
, I was able to resolve the error.