Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

if Prototype is loaded, remove its Array.toJSON #18

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions src/jschannel.js
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,21 @@
if (!window.JSON || !window.JSON.stringify || ! window.JSON.parse) {
throw("jschannel cannot run this browser, no JSON parsing/serialization");
}
if (window.Prototype) {
// Some versions of Prototype ship with a broken Array.toJSON
// which flattens arrays into strings! Since we have a working
// JSON.stringify we can safely remove it.
// See: http://stackoverflow.com/questions/710586/json-stringify-bizarreness
console.log("patching Prototype's faulty Array.toJSON")
var stringify = window.JSON.stringify;
window.JSON.stringify = function(value) {
var _array_tojson = Array.prototype.toJSON;
delete Array.prototype.toJSON;
var r = stringify(value);
Array.prototype.toJSON = _array_tojson;
return r;
}
}

/* basic argument validation */
if (typeof cfg != 'object') throw("Channel build invoked without a proper object argument");
Expand Down