From ef3dc3d5d16913af44d3b30a30945e1605a8a23f Mon Sep 17 00:00:00 2001 From: Ed Summers Date: Thu, 22 Aug 2013 08:40:24 -0400 Subject: [PATCH 1/2] if Prototype is loaded, remove its Array.toJSON which flattens arrays into strings. Fixes #17 --- src/jschannel.js | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/jschannel.js b/src/jschannel.js index a4f6ba22..4a1b766d 100644 --- a/src/jschannel.js +++ b/src/jschannel.js @@ -233,6 +233,14 @@ 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("removing Prototype's faulty Array.toJSON") + delete Array.prototype.toJSON; + } /* basic argument validation */ if (typeof cfg != 'object') throw("Channel build invoked without a proper object argument"); From e9caa88ac9390385b57d263528f0869ae6a48ac9 Mon Sep 17 00:00:00 2001 From: Ed Summers Date: Thu, 22 Aug 2013 09:43:07 -0400 Subject: [PATCH 2/2] temporarily remove Prototype's Array.toJSON to address @csillag's concerns about disrupting Prototype --- src/jschannel.js | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/jschannel.js b/src/jschannel.js index 4a1b766d..8b393a88 100644 --- a/src/jschannel.js +++ b/src/jschannel.js @@ -238,8 +238,15 @@ // 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("removing Prototype's faulty Array.toJSON") - delete Array.prototype.toJSON; + 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 */