How to determine which JSON object is used (Crockford or another)?

I am using Crockford json2.js . When I want to compress, I do JSON.stringify() ... works fine.

However, those who take a look at the code know that it discards existing JSON objects and properties. I suspect that the specific problem I am experiencing may be caused by this respect.

Is there a JSON object property that I can check to check if the browser uses a Crockford object or some other? It would be nice to do something like alert(JSON.version());

+6
source share
2 answers

You can choose one of the following methods:

 <script>window.JSON || document.write('<script src="js/json2.js"><\/script>')</script> 

This checks for window.JSON (supported by the browser) if it exists, use this, otherwise it imports json2.js from Crockford.

Update

 var whichJSON = null; if (! window.JSON) { document.write('<script src="js/json2.js"><\/script>'); whichJSON = 'Crockford Version'; } else { whichJSON = 'Browser Native Version'; } alert(whichJSON); 
+4
source

Before loading the Crockford script, you can check the global JSON object just like it:

 <script> var JSON, nativeJSON = true; if (!JSON) { var nativeJSON = false; document.write('<script src="js/json2.js"><\/script>'); } if (!nativeJSON) { // All JSON objects are using Crockford implementation } else { // All JSON objects from here on out are NOT using Crockford implementation } </script> 
+3
source

Source: https://habr.com/ru/post/918723/


All Articles