views:

2678

answers:

3

Hi Quick questions that probably a piece of cake for someone in the know to asnwer.

I have a simple asp.net website that uses JSON for a bunch of stuff (and JSON.stringify)

All good in firefox etc, yet, in IE6 I run into an error with JSON being undefined.

Is there a way I can include a JSON implementation without breaking what I have already (using the native JSON objects in the other browsers). If so, how?

Thanks!

+4  A: 

Your version of firefox might be having built-in support for the JSON library. But ideally you should include the JSON js library from json.org (make a copy of it in your hosted domain).

Joy Dutta
Yes and I think recent versions of the json library detect if there's a native json to use.
Nosredna
A: 

Simply check for JSON.stringify and if it doesn't exist, use some other method to parse data into a JSON string.

Russ Cam
+8  A: 

The json2 library at http://json.org/json2.js is exactly what you're looking for. You can include it unconditionally, and it adds JSON.parse and JSON.stringify to your global namespace (only if there isn't one defined yet). It won't mess with any built-in JSON. From its source:

if (!this.JSON) {
    this.JSON = {};
}
...
if (typeof JSON.stringify !== 'function') {
...
if (typeof JSON.parse !== 'function') {

That's comprehensive! Even if for some reason you already have JSON.stringify but not JSON.parse (or vice versa) it'll still do the right thing, leaving the original ones in place.

darkporter