How to detect JSON support in javascript? How to detect JSON support in javascript? json json

How to detect JSON support in javascript?


Taken from the json most famous implementation https://github.com/douglascrockford/JSON-js/blob/master/json2.js

var JSON;if (JSON && typeof JSON.parse === 'function') {    ....}

(I have merged the two if: if (!JSON) { of line 163 and if (typeof JSON.parse !== 'function') { of line 406.

The trick here is that the var JSON will get the value of the JSON object of the browser, undefined if not.

Note that in the latest version of the library they changed the code to something like:

if (typeof JSON === 'object' && typeof JSON.parse === 'function') {    ....}

(without pre-declaring the var JSON)


Might not exactly count as an answer to what was asked, but would perhaps parsing the user agent (navigator) and checking for versions you are confident support the parser be a possible alternative?