Beware of jacked toJSON implementations in Prototype and MooTools
I recently came across an annoying problem with the MooTools and Prototype JavaScript libraries where JSON serialization is concerned. Both add a toJSON function to Object which does not work very well at all. Both implementations do not serialize arrays nested in objects, so any Object containing an array will not be serialized properly (As taken from Google’s hosted version of Prototype:
toJSON: function(object) {
var type = typeof object;
switch (type) {
case 'undefined':
case 'function':
case 'unknown': return;
case 'boolean': return object.toString();
}
if (object === null) return 'null';
if (object.toJSON) return object.toJSON();
if (Object.isElement(object)) return;
var results = [];
for (var property in object) {
var value = Object.toJSON(object[property]);
if (!Object.isUndefined(value))
results.push(property.toJSON() + ': ' + value);
}
return '{' + results.join(', ') + '}';
}
Just a word of caution. I’ll try and file some bugs on the matter as time allows.
