سؤال

If I run JSON.stringify(["hello"]) in the console the output is as expected:

<- "["hello"]"

If I run it twice like JSON.stringify(JSON.stringify(["hello"])) it results into the following as expected:

<- ""[\"hello\"]""

Now I was trying the same thing on this page. If I run JSON.stringify(["hello"]) in the console it returns the second example´s result right away.

<- ""[\"hello\"]""

I have no idea what is happening there. It looks like someone is overwriting the native JSON.stringify function. If so, how can I restore that?

هل كانت مفيدة؟

المحلول

I believe the problem is that something is adding String.prototype.toJSON and Array.prototype.toJSON. If I do this in the console on your page:

String.prototype.toJSON.toString()

I get

"function (){return this.inspect(true)}"

And similarly

Array.prototype.toJSON.toString()

gives me

"function (){var c=[];this.each(function(a){var b=Object.toJSON(a);if(!Object.isUndefined(b))c.push(b)});return'['+c.join(', ')+']'}"

JSON.stringify will look first to an object to see if it has a toJSON and, if so, will use it to get the JSON for that object (spec link). I suspect the implementation being added is not happily coexisting with the browser's JSON.stringify.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top