PageRenderTime 61ms CodeModel.GetById 49ms app.highlight 9ms RepoModel.GetById 1ms app.codeStats 0ms

/source/Plug-in/jq/json.js

http://prosporous.googlecode.com/
JavaScript | 226 lines | 214 code | 11 blank | 1 comment | 2 complexity | abd32e4a8bbfc47153c50b62aba22984 MD5 | raw file
  1
  2
  3
  4
  5var JSON = $.JSON;
  6if (!JSON) {
  7    JSON = {};
  8}
  9
 10(function () {
 11    "use strict";
 12
 13    function f(n) {
 14        // Format integers to have at least two digits.
 15        return n < 10 ? '0' + n : n;
 16    }
 17
 18    if (typeof Date.prototype.toJSON !== 'function') {
 19
 20        Date.prototype.toJSON = function (key) {
 21
 22            return isFinite(this.valueOf()) ?
 23                this.getUTCFullYear() + '-' +
 24                f(this.getUTCMonth() + 1) + '-' +
 25                f(this.getUTCDate()) + 'T' +
 26                f(this.getUTCHours()) + ':' +
 27                f(this.getUTCMinutes()) + ':' +
 28                f(this.getUTCSeconds()) + 'Z' : null;
 29        };
 30
 31        String.prototype.toJSON =
 32            Number.prototype.toJSON =
 33            Boolean.prototype.toJSON = function (key) {
 34                return this.valueOf();
 35            };
 36    }
 37
 38    var cx = /[\u0000\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g,
 39        escapable = /[\\\"\x00-\x1f\x7f-\x9f\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g,
 40        gap,
 41        indent,
 42        meta = { // table of character substitutions
 43            '\b': '\\b',
 44            '\t': '\\t',
 45            '\n': '\\n',
 46            '\f': '\\f',
 47            '\r': '\\r',
 48            '"' : '\\"',
 49            '\\': '\\\\'
 50        },
 51        rep;
 52
 53
 54    function quote(string) {
 55
 56        escapable.lastIndex = 0;
 57        return escapable.test(string) ? '"' + string.replace(escapable, function (a) {
 58            var c = meta[a];
 59            return typeof c === 'string' ? c :
 60                '\\u' + ('0000' + a.charCodeAt(0).toString(16)).slice(-4);
 61        }) + '"' : '"' + string + '"';
 62    }
 63
 64
 65    function str(key, holder) {
 66        var i, // The loop counter.
 67            k, // The member key.
 68            v, // The member value.
 69            length,
 70            mind = gap,
 71            partial,
 72            value = holder[key];
 73
 74        if (value && typeof value === 'object' && typeof value.toJSON === 'function') {
 75            value = value.toJSON(key);
 76        }
 77
 78        if (typeof rep === 'function') {
 79            value = rep.call(holder, key, value);
 80        }
 81
 82        switch (typeof value) {
 83        case 'string':
 84            return quote(value);
 85        case 'number':
 86            return isFinite(value) ? String(value) : 'null';
 87        case 'boolean':
 88        case 'null':
 89            return String(value);
 90        case 'object':
 91            if (!value){
 92                return 'null';
 93            }
 94            gap += indent;
 95            partial = [];
 96
 97            if (Object.prototype.toString.apply(value) === '[object Array]') {
 98                length = value.length;
 99                for (i = 0; i < length; i += 1) {
100                    partial[i] = str(i, value) || 'null';
101                }
102
103                v = partial.length === 0 ? '[]' : gap ?
104                    '[\n' + gap + partial.join(',\n' + gap) + '\n' + mind + ']' :
105                    '[' + partial.join(',') + ']';
106                gap = mind;
107                return v;
108            }
109
110            if (rep && typeof rep === 'object') {
111                length = rep.length;
112                for (i = 0; i < length; i += 1) {
113                    k = rep[i];
114                    if (typeof k === 'string') {
115                        v = str(k, value);
116                        if (v) {
117                            partial.push(quote(k) + (gap ? ': ' : ':') + v);
118                        }
119                    }
120                }
121            } else {
122                for (k in value) {
123                    if (Object.prototype.hasOwnProperty.call(value, k)) {
124                        v = str(k, value);
125                        if (v) {
126                            partial.push(quote(k) + (gap ? ': ' : ':') + v);
127                        }
128                    }
129                }
130            }
131
132            v = partial.length === 0 ? '{}' : gap ?
133                '{\n' + gap + partial.join(',\n' + gap) + '\n' + mind + '}' :
134                '{' + partial.join(',') + '}';
135            gap = mind;
136            return v;
137        }
138    }
139
140
141    if (typeof JSON.stringify !== 'function') {
142        JSON.stringify = function (value, replacer, space) {
143
144
145            var i;
146            gap = '';
147            indent = '';
148
149
150            if (typeof space === 'number') {
151                for (i = 0; i < space; i += 1) {
152                    indent += ' ';
153                }
154
155
156
157            } else if (typeof space === 'string') {
158                indent = space;
159            }
160
161
162            rep = replacer;
163            if (replacer && typeof replacer !== 'function' &&
164                    (typeof replacer !== 'object' ||
165                    typeof replacer.length !== 'number')) {
166                throw new Error('JSON.stringify');
167            }
168
169
170            return str('', {'': value});
171        };
172    }
173
174
175    if (typeof JSON.parse !== 'function') {
176        JSON.parse = function (text, reviver) {
177            var j;
178            function walk(holder, key) {
179                var k, v, value = holder[key];
180                if (value && typeof value === 'object') {
181                    for (k in value) {
182                        if (Object.prototype.hasOwnProperty.call(value, k)) {
183                            v = walk(value, k);
184                            if (v !== undefined) {
185                                value[k] = v;
186                            } else {
187                                delete value[k];
188                            }
189                        }
190                    }
191                }
192                return reviver.call(holder, key, value);
193            }
194            text = String(text);
195            cx.lastIndex = 0;
196            if (cx.test(text)) {
197                text = text.replace(cx, function (a) {
198                    return '\\u' +
199                        ('0000' + a.charCodeAt(0).toString(16)).slice(-4);
200                });
201            }
202            if (/^[\],:{}\s]*$/
203                    .test(text.replace(/\\(?:["\\\/bfnrt]|u[0-9a-fA-F]{4})/g, '@')
204                        .replace(/"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g, ']')
205                        .replace(/(?:^|:|,)(?:\s*\[)+/g, ''))) {
206                j = eval('(' + text + ')');
207
208                return typeof reviver === 'function' ?
209                    walk({'': j}, '') : j;
210            }
211            throw new SyntaxError('JSON.parse');
212        };
213    }
214
215    (function($){
216      if (!$.toJSONString) {
217        $.toJSONString = function (obj,filter) {
218            return JSON.stringify(obj, filter);
219        };
220       // Object.prototype.parseJSON= function (filter) {
221       //     return JSON.parse(this, filter);
222       // };
223     }
224    })(jQuery);
225
226}());