PageRenderTime 55ms CodeModel.GetById 44ms app.highlight 8ms RepoModel.GetById 1ms app.codeStats 0ms

/branches/jsdoc_tk_gui/setup/workingDirectory/Webeo/ao/data/Json.js

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