douglascrockford/JSON-js — Issue #26: String.prototype.toJSON not defined if Date.prototype.toJSON defined
Author: petermichaux
Created: 2012-01-29 18:01:04+00:00
State: open

If Date.prototype.toJSON is defined by some other polyfill library that is loaded before json2.js, then String.prototype.toJSON will not be defined by json2.js. This is actually the case if es5-shim is loaded before json2.js.

It would be better if json2.js tested for the existence of String.prototype.toJSON itself rather than depending on the existence of some other property.

if (typeof String.prototype.toJSON !== 'function') {
String.prototype.toJSON = function (key) {
return this.valueOf();
};
}

Similarly for Number.prototype.toJSON and Boolean.prototype.toJSON.