test for native JSON support #227

wants to merge 1 commit into


None yet
8 participants

mlc commented Mar 23, 2011

here's a quick test for native JSON support, as is present in modern browsers. if it comes up false, you can load json2.js or whatever.

Wouldn't return JSON.parse; suffice?


mlc replied May 22, 2011

no, because that would throw an exception if there were no JSON object present in the browser. doing simply return JSON && JSON.parse; would not coerce the result to boolean like modernizr's other tests seem to do.

Sorry, my bad, I meant: return !!JSON.parse;. I was just referring to the fact that the existence of JSON.parse implies the existence of the JSON object. Wouldn't that make checking for both redundant?


mlc replied May 22, 2011

as I said above, if JSON is undefined, then attempting to access its parse property will throw an exception and crash the script.

Ah, fair point. I was wondering about that. I skipped over the first part of your reply in my haste to fix the mistake. Thanks for clarifying.

I double on that - JSON check would be great.

betamos commented Jun 28, 2011


mgiulio commented Aug 8, 2011

Me too.


ryanseddon commented Aug 9, 2011

Could you move it out of the core and into a separate file inside the feature-detects folder.

The test fails for me in IE 7. This works:

!!window.JSON && !!window.JSON.parse

@paulirish paulirish closed this in 0fd6d5f Dec 4, 2011

patrickkettner pushed a commit to patrickkettner/Modernizr that referenced this pull request Feb 22, 2015

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment