JSON.stringify support for IE7 – Continued


I previously posted JSON.stringify support for IE7.  The post describes issues with IE7 support for JSON.stringify.  I recently came across another solution that does not require referencing the Douglas Crockford’s json2.js script.

The Even Simpler Solution…

Add the following code to an existing script or right on the page.  The JavaScript code looks for JSON.stringify function from the browser, if it is null then it falls back to the method defined below.

By adding this code, you can use JSON.stringify without concern for browser support See Cross-browser JSON Serialization in JavaScript for additional details.

var JSON = JSON || {};
JSON.stringify = JSON.stringify || function (obj) {
    var t = typeof (obj);
    if (t != "object" || obj === null) {
        // simple data type
        if (t == "string") obj = '"' + obj + '"';
        return String(obj);
    else {
        // recurse array or object
        var n, v, json = [], arr = (obj && obj.constructor == Array);
        for (n in obj) {
            v = obj[n]; t = typeof (v);
            if (t == "string") v = '"' + v + '"';
            else if (t == "object" && v !== null) v = JSON.stringify(v);
            json.push((arr ? "" : '"' + n + '":') + String(v));
        return (arr ? "[" : "{") + String(json) + (arr ? "]" : "}");
Nate Bunton

Nate Bunton is a Lead Software Engineer at Meta Payment Systems. He has over 10 years developing software and leading teams. He works with the Microsoft technology stack focusing in ASP.NET, MVC, HTML, JavaScript, CSS and Web Security. Nate also focuses on Application Lifecycle Management (ALM) engaging his teams with key stakeholders using a variety of agile principals. At Meta Payment Systems, Nate has been a leader in driving technology in the enterprise from new Web Technology to Service Oriented Architecture using WCF and NServiceBus. Nate’s greatest passion is for User Experience, Web Technology and Engaging Teams. He is driven by his desire for continuous learning & improvement.

Posted in Web Development Tagged with: , , , , ,