PageRenderTime 183ms CodeModel.GetById 101ms app.highlight 7ms RepoModel.GetById 73ms app.codeStats 1ms

/ajax/scripts/json.js

http://showslow.googlecode.com/
JavaScript | 129 lines | 100 code | 5 blank | 24 comment | 5 complexity | 7f5bd3537a3e95fc8e102989c664fdb6 MD5 | raw file
  1/*
  2 *  Copied directly from http://www.json.org/json.js.
  3 */
  4
  5/*
  6    json.js
  7    2006-04-28
  8
  9    This file adds these methods to JavaScript:
 10
 11        object.toJSONString()
 12
 13            This method produces a JSON text from an object. The
 14            object must not contain any cyclical references.
 15
 16        array.toJSONString()
 17
 18            This method produces a JSON text from an array. The
 19            array must not contain any cyclical references.
 20
 21        string.parseJSON()
 22
 23            This method parses a JSON text to produce an object or
 24            array. It will return false if there is an error.
 25*/
 26
 27SimileAjax.JSON = new Object();
 28
 29(function () {
 30    var m = {
 31        '\b': '\\b',
 32        '\t': '\\t',
 33        '\n': '\\n',
 34        '\f': '\\f',
 35        '\r': '\\r',
 36        '"' : '\\"',
 37        '\\': '\\\\'
 38    };
 39    var s = {
 40        array: function (x) {
 41            var a = ['['], b, f, i, l = x.length, v;
 42            for (i = 0; i < l; i += 1) {
 43                v = x[i];
 44                f = s[typeof v];
 45                if (f) {
 46                    v = f(v);
 47                    if (typeof v == 'string') {
 48                        if (b) {
 49                            a[a.length] = ',';
 50                        }
 51                        a[a.length] = v;
 52                        b = true;
 53                    }
 54                }
 55            }
 56            a[a.length] = ']';
 57            return a.join('');
 58        },
 59        'boolean': function (x) {
 60            return String(x);
 61        },
 62        'null': function (x) {
 63            return "null";
 64        },
 65        number: function (x) {
 66            return isFinite(x) ? String(x) : 'null';
 67        },
 68        object: function (x) {
 69            if (x) {
 70                if (x instanceof Array) {
 71                    return s.array(x);
 72                }
 73                var a = ['{'], b, f, i, v;
 74                for (i in x) {
 75                    v = x[i];
 76                    f = s[typeof v];
 77                    if (f) {
 78                        v = f(v);
 79                        if (typeof v == 'string') {
 80                            if (b) {
 81                                a[a.length] = ',';
 82                            }
 83                            a.push(s.string(i), ':', v);
 84                            b = true;
 85                        }
 86                    }
 87                }
 88                a[a.length] = '}';
 89                return a.join('');
 90            }
 91            return 'null';
 92        },
 93        string: function (x) {
 94            if (/["\\\x00-\x1f]/.test(x)) {
 95                x = x.replace(/([\x00-\x1f\\"])/g, function(a, b) {
 96                    var c = m[b];
 97                    if (c) {
 98                        return c;
 99                    }
100                    c = b.charCodeAt();
101                    return '\\u00' +
102                        Math.floor(c / 16).toString(16) +
103                        (c % 16).toString(16);
104                });
105            }
106            return '"' + x + '"';
107        }
108    };
109
110    SimileAjax.JSON.toJSONString = function(o) {
111        if (o instanceof Object) {
112            return s.object(o);
113        } else if (o instanceof Array) {
114            return s.array(o);
115        } else {
116            return o.toString();
117        }
118    };
119    
120    SimileAjax.JSON.parseJSON = function () {
121        try {
122            return !(/[^,:{}\[\]0-9.\-+Eaeflnr-u \n\r\t]/.test(
123                    this.replace(/"(\\.|[^"\\])*"/g, ''))) &&
124                eval('(' + this + ')');
125        } catch (e) {
126            return false;
127        }
128    };
129})();