1

In IE 11, calling Stringify on my objects is not recursively calling toJson on all object in the tree. I have 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 works in chrome, firefox and safari, but in IE 11 this toJson function only gets called on the top level object when I call var json = JSON.stringify(object); The idea im working with is have toJson be a generic function on all classes, then have var json = this.toJSONProperties(); on the specific classes i want to convert. Any Ideas why this only fails in IE?

Update

I fixed the problem. It turns out that this.constructor.name doesn't work in IE. Changing it to this.constructor.toString().match(/function (.{1,})\(/)[1] fixes the error.

3
  • what's the def for toJSONProperties() ? Commented Jul 15, 2014 at 16:11
  • What is puzzling me is why it's working on other browsers: if the root object has a toJSON method, it should be called and no other attribute should be taken in consideration Commented Jul 15, 2014 at 16:19
  • Here is the toJSONProperties on the top class. Deck.prototype.toJSONProperties = function() { return { _components: this._components } }; Commented Jul 15, 2014 at 16:57

1 Answer 1

1

this.constructor.name doesn't work in IE. Changing it to this.constructor.toString().match(/function (.{1,})\(/)[1] fixes the issue.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.