"foo": [[1, "string", "anotherstring" {object:true}]]I expect output like this from JSON.stringify:
"foo":[[1, \"string\", \"anotherstring\" {object:true}]]And I get it, from native JSON, from json2.js, and from the older YAHOO.lang.JSON. Mootools returned:
"foo":"[[1, \"string\", \"anotherstring\" {object:true}]]"The object attribute was represented as a string! So, I dug into the reasons why the shiny new JSON2 library had this issue, and only with Mootools present. Here is some history, and results of debugging.
typeof value.toJSON === 'function') { // (JSON2's test now returns false, so the serialization is handled by JSON, not Mootools)Below is the nitty gritty of how Mootools 1.2.4 can break JSON2. Mootools 1.2.4 Here's the line where Crockford's JSON2.js tries to optimize by using a native method:
function str(key, holder) { ... // If the value has a toJSON method, call it to obtain a replacement value. if (value && typeof value === 'object' && typeof value.toJSON === 'function') { value = value.toJSON(key); }Execution steps into Mootools' intrusive method:
Native.implement([Hash, Array, String, Number], { toJSON: function(){ return JSON.encode(this); } });Mootools $type function returns 'array' for a literal []. (Standard typeof operator returns 'object')
obj.$family.name // property $family was added by Mootools
March 2006 May 2006 July 2006 August 2006 January 2007 March 2007 April 2007 August 2007 March 2008 April 2008 August 2008 April 2010 May 2010 July 2010 September 2010 April 2011 November 2012 March 2013 December 2013