|
1 |
| -/* |
| 1 | +/** |
2 | 2 | * jQuery JSON Plugin
|
3 |
| - * version: 2.1 (2009-08-14) |
| 3 | + * version: 2.2 (2011-09-16) |
4 | 4 | *
|
5 | 5 | * This document is licensed as free software under the terms of the
|
6 | 6 | * MIT License: http://www.opensource.org/licenses/mit-license.php
|
7 | 7 | *
|
8 |
| - * Brantley Harris wrote this plugin. It is based somewhat on the JSON.org |
| 8 | + * Brantley Harris wrote this plugin. It is based somewhat on the JSON.org |
9 | 9 | * website's http://www.json.org/json2.js, which proclaims:
|
10 | 10 | * "NO WARRANTY EXPRESSED OR IMPLIED. USE AT YOUR OWN RISK.", a sentiment that
|
11 | 11 | * I uphold.
|
12 | 12 | *
|
13 |
| - * It is also influenced heavily by MochiKit's serializeJSON, which is |
| 13 | + * It is also influenced heavily by MochiKit's serializeJSON, which is |
14 | 14 | * copyrighted 2005 by Bob Ippolito.
|
15 | 15 | */
|
16 |
| - |
17 |
| -(function($) { |
18 |
| - /** jQuery.toJSON( json-serializble ) |
19 |
| - Converts the given argument into a JSON respresentation. |
20 | 16 |
|
21 |
| - If an object has a "toJSON" function, that will be used to get the representation. |
22 |
| - Non-integer/string keys are skipped in the object, as are keys that point to a function. |
| 17 | +(function( $ ) { |
| 18 | + /** |
| 19 | + * jQuery.toJSON |
| 20 | + * Converts the given argument into a JSON respresentation. |
| 21 | + * |
| 22 | + * @param o {Mixed} The json-serializble *thing* to be converted |
| 23 | + * |
| 24 | + * If an object has a "toJSON" function, that will be used to get the representation. |
| 25 | + * Non-integer/string keys are skipped in the object, as are keys that point to a |
| 26 | + * function. |
| 27 | + * |
| 28 | + */ |
| 29 | + $.toJSON = function( o ) { |
| 30 | + if ( typeof JSON === 'object' && JSON.stringify ) { |
| 31 | + return JSON.stringify( o ); |
| 32 | + } |
23 | 33 |
|
24 |
| - json-serializble: |
25 |
| - The *thing* to be converted. |
26 |
| - **/ |
27 |
| - $.toJSON = function(o) |
28 |
| - { |
29 |
| - if (typeof(JSON) == 'object' && JSON.stringify) |
30 |
| - return JSON.stringify(o); |
31 |
| - |
32 |
| - var type = typeof(o); |
33 |
| - |
34 |
| - if (o === null) |
35 |
| - return "null"; |
36 |
| - |
37 |
| - if (type == "undefined") |
38 |
| - return undefined; |
39 |
| - |
40 |
| - if (type == "number" || type == "boolean") |
41 |
| - return o + ""; |
42 |
| - |
43 |
| - if (type == "string") |
44 |
| - return $.quoteString(o); |
45 |
| - |
46 |
| - if (type == 'object') |
47 |
| - { |
48 |
| - if (typeof o.toJSON == "function") |
49 |
| - return $.toJSON( o.toJSON() ); |
50 |
| - |
51 |
| - if (o.constructor === Date) |
52 |
| - { |
53 |
| - var month = o.getUTCMonth() + 1; |
54 |
| - if (month < 10) month = '0' + month; |
| 34 | + var type = typeof o; |
55 | 35 |
|
56 |
| - var day = o.getUTCDate(); |
57 |
| - if (day < 10) day = '0' + day; |
| 36 | + if ( o === null ) { |
| 37 | + return 'null'; |
| 38 | + } |
| 39 | + if ( type === 'undefined' ) { |
| 40 | + return undefined; |
| 41 | + } |
| 42 | + if ( type === 'number' || type === 'boolean' ) { |
| 43 | + return o + ''; |
| 44 | + } |
| 45 | + if ( type === 'string') { |
| 46 | + return $.quoteString( o ); |
| 47 | + } |
| 48 | + if ( type === 'object' ) { |
| 49 | + if ( typeof o.toJSON === 'function' ) { |
| 50 | + return $.toJSON( o.toJSON() ); |
| 51 | + } |
| 52 | + if ( o.constructor === Date ) { |
| 53 | + var month = o.getUTCMonth() + 1; |
| 54 | + if ( month < 10 ) { |
| 55 | + month = '0' + month; |
| 56 | + } |
| 57 | + var day = o.getUTCDate(); |
| 58 | + if ( day < 10 ) { |
| 59 | + day = '0' + day; |
| 60 | + } |
| 61 | + var year = o.getUTCFullYear(); |
| 62 | + var hours = o.getUTCHours(); |
| 63 | + if ( hours < 10 ) { |
| 64 | + hours = '0' + hours; |
| 65 | + } |
| 66 | + var minutes = o.getUTCMinutes(); |
| 67 | + if ( minutes < 10 ) { |
| 68 | + minutes = '0' + minutes; |
| 69 | + } |
| 70 | + var seconds = o.getUTCSeconds(); |
| 71 | + if ( seconds < 10 ) { |
| 72 | + seconds = '0' + seconds; |
| 73 | + } |
| 74 | + var milli = o.getUTCMilliseconds(); |
| 75 | + if ( milli < 100 ) { |
| 76 | + milli = '0' + milli; |
| 77 | + } |
| 78 | + if ( milli < 10 ) { |
| 79 | + milli = '0' + milli; |
| 80 | + } |
| 81 | + return '"' + year + '-' + month + '-' + day + 'T' + |
| 82 | + hours + ':' + minutes + ':' + seconds + |
| 83 | + '.' + milli + 'Z"'; |
| 84 | + } |
| 85 | + if ( o.constructor === Array ) { |
| 86 | + var ret = []; |
| 87 | + for ( var i = 0; i < o.length; i++ ) { |
| 88 | + ret.push( $.toJSON( o[i] ) || 'null' ); |
| 89 | + } |
| 90 | + return '[' + ret.join(',') + ']'; |
| 91 | + } |
| 92 | + var pairs = []; |
| 93 | + for ( var k in o ) { |
| 94 | + var name; |
| 95 | + var type = typeof k; |
| 96 | + if ( type === 'number' ) { |
| 97 | + name = '"' + k + '"'; |
| 98 | + } else if (type === 'string') { |
| 99 | + name = $.quoteString(k); |
| 100 | + } else { |
| 101 | + //skip non-string or number keys |
| 102 | + continue; |
| 103 | + } |
58 | 104 |
|
59 |
| - var year = o.getUTCFullYear(); |
60 |
| - |
61 |
| - var hours = o.getUTCHours(); |
62 |
| - if (hours < 10) hours = '0' + hours; |
63 |
| - |
64 |
| - var minutes = o.getUTCMinutes(); |
65 |
| - if (minutes < 10) minutes = '0' + minutes; |
66 |
| - |
67 |
| - var seconds = o.getUTCSeconds(); |
68 |
| - if (seconds < 10) seconds = '0' + seconds; |
69 |
| - |
70 |
| - var milli = o.getUTCMilliseconds(); |
71 |
| - if (milli < 100) milli = '0' + milli; |
72 |
| - if (milli < 10) milli = '0' + milli; |
| 105 | + if ( typeof o[k] === 'function' ) { |
| 106 | + //skip pairs where the value is a function. |
| 107 | + continue; |
| 108 | + } |
| 109 | + var val = $.toJSON( o[k] ); |
| 110 | + pairs.push(name + ':' + val); |
| 111 | + } |
| 112 | + return '{' + pairs.join(', ') + '}'; |
| 113 | + } |
| 114 | + }; |
73 | 115 |
|
74 |
| - return '"' + year + '-' + month + '-' + day + 'T' + |
75 |
| - hours + ':' + minutes + ':' + seconds + |
76 |
| - '.' + milli + 'Z"'; |
77 |
| - } |
| 116 | + /** |
| 117 | + * jQuery.evalJSON |
| 118 | + * Evaluates a given piece of json source. |
| 119 | + * |
| 120 | + * @param src {String} |
| 121 | + */ |
| 122 | + $.evalJSON = function( src ) { |
| 123 | + if ( typeof JSON === 'object' && JSON.parse ) { |
| 124 | + return JSON.parse( src ); |
| 125 | + } |
| 126 | + return eval('(' + src + ')'); |
| 127 | + }; |
78 | 128 |
|
79 |
| - if (o.constructor === Array) |
80 |
| - { |
81 |
| - var ret = []; |
82 |
| - for (var i = 0; i < o.length; i++) |
83 |
| - ret.push( $.toJSON(o[i]) || "null" ); |
| 129 | + /** |
| 130 | + * jQuery.secureEvalJSON |
| 131 | + * Evals JSON in a way that is *more* secure. |
| 132 | + * |
| 133 | + * @param src {String} |
| 134 | + */ |
| 135 | + $.secureEvalJSON = function( src ) { |
| 136 | + if ( typeof JSON === 'object' && JSON.parse ) { |
| 137 | + return JSON.parse( src ); |
| 138 | + } |
84 | 139 |
|
85 |
| - return "[" + ret.join(",") + "]"; |
86 |
| - } |
87 |
| - |
88 |
| - var pairs = []; |
89 |
| - for (var k in o) { |
90 |
| - var name; |
91 |
| - var type = typeof k; |
| 140 | + var filtered = src; |
| 141 | + filtered = filtered.replace(/\\["\\\/bfnrtu]/g, '@'); |
| 142 | + filtered = filtered.replace(/"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g, ']'); |
| 143 | + filtered = filtered.replace(/(?:^|:|,)(?:\s*\[)+/g, ''); |
92 | 144 |
|
93 |
| - if (type == "number") |
94 |
| - name = '"' + k + '"'; |
95 |
| - else if (type == "string") |
96 |
| - name = $.quoteString(k); |
97 |
| - else |
98 |
| - continue; //skip non-string or number keys |
99 |
| - |
100 |
| - if (typeof o[k] == "function") |
101 |
| - continue; //skip pairs where the value is a function. |
102 |
| - |
103 |
| - var val = $.toJSON(o[k]); |
104 |
| - |
105 |
| - pairs.push(name + ":" + val); |
106 |
| - } |
| 145 | + if ( /^[\],:{}\s]*$/.test( filtered ) ) { |
| 146 | + return eval('(' + src + ')'); |
| 147 | + } else { |
| 148 | + throw new SyntaxError( 'Error parsing JSON, source is not valid.' ); |
| 149 | + } |
| 150 | + }; |
107 | 151 |
|
108 |
| - return "{" + pairs.join(", ") + "}"; |
109 |
| - } |
110 |
| - }; |
| 152 | + var _escapeable = /["\\\x00-\x1f\x7f-\x9f]/g; |
| 153 | + var _meta = { |
| 154 | + '\b': '\\b', |
| 155 | + '\t': '\\t', |
| 156 | + '\n': '\\n', |
| 157 | + '\f': '\\f', |
| 158 | + '\r': '\\r', |
| 159 | + '"' : '\\"', |
| 160 | + '\\': '\\\\' |
| 161 | + }; |
111 | 162 |
|
112 |
| - /** jQuery.evalJSON(src) |
113 |
| - Evaluates a given piece of json source. |
114 |
| - **/ |
115 |
| - $.evalJSON = function(src) |
116 |
| - { |
117 |
| - if (typeof(JSON) == 'object' && JSON.parse) |
118 |
| - return JSON.parse(src); |
119 |
| - return eval("(" + src + ")"); |
120 |
| - }; |
121 |
| - |
122 |
| - /** jQuery.secureEvalJSON(src) |
123 |
| - Evals JSON in a way that is *more* secure. |
124 |
| - **/ |
125 |
| - $.secureEvalJSON = function(src) |
126 |
| - { |
127 |
| - if (typeof(JSON) == 'object' && JSON.parse) |
128 |
| - return JSON.parse(src); |
129 |
| - |
130 |
| - var filtered = src; |
131 |
| - filtered = filtered.replace(/\\["\\\/bfnrtu]/g, '@'); |
132 |
| - filtered = filtered.replace(/"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g, ']'); |
133 |
| - filtered = filtered.replace(/(?:^|:|,)(?:\s*\[)+/g, ''); |
134 |
| - |
135 |
| - if (/^[\],:{}\s]*$/.test(filtered)) |
136 |
| - return eval("(" + src + ")"); |
137 |
| - else |
138 |
| - throw new SyntaxError("Error parsing JSON, source is not valid."); |
139 |
| - }; |
| 163 | + /** |
| 164 | + * jQuery.quoteString |
| 165 | + * Returns a string-repr of a string, escaping quotes intelligently. |
| 166 | + * Mostly a support function for toJSON. |
| 167 | + * Examples: |
| 168 | + * >>> jQuery.quoteString('apple') |
| 169 | + * "apple" |
| 170 | + * |
| 171 | + * >>> jQuery.quoteString('"Where are we going?", she asked.') |
| 172 | + * "\"Where are we going?\", she asked." |
| 173 | + */ |
| 174 | + $.quoteString = function( string ) { |
| 175 | + if ( string.match( _escapeable ) ) { |
| 176 | + return '"' + string.replace( _escapeable, function( a ) { |
| 177 | + var c = _meta[a]; |
| 178 | + if ( typeof c === 'string' ) { |
| 179 | + return c; |
| 180 | + } |
| 181 | + c = a.charCodeAt(); |
| 182 | + return '\\u00' + Math.floor(c / 16).toString(16) + (c % 16).toString(16); |
| 183 | + }) + '"'; |
| 184 | + } |
| 185 | + return '"' + string + '"'; |
| 186 | + }; |
140 | 187 |
|
141 |
| - /** jQuery.quoteString(string) |
142 |
| - Returns a string-repr of a string, escaping quotes intelligently. |
143 |
| - Mostly a support function for toJSON. |
144 |
| - |
145 |
| - Examples: |
146 |
| - >>> jQuery.quoteString("apple") |
147 |
| - "apple" |
148 |
| - |
149 |
| - >>> jQuery.quoteString('"Where are we going?", she asked.') |
150 |
| - "\"Where are we going?\", she asked." |
151 |
| - **/ |
152 |
| - $.quoteString = function(string) |
153 |
| - { |
154 |
| - if (string.match(_escapeable)) |
155 |
| - { |
156 |
| - return '"' + string.replace(_escapeable, function (a) |
157 |
| - { |
158 |
| - var c = _meta[a]; |
159 |
| - if (typeof c === 'string') return c; |
160 |
| - c = a.charCodeAt(); |
161 |
| - return '\\u00' + Math.floor(c / 16).toString(16) + (c % 16).toString(16); |
162 |
| - }) + '"'; |
163 |
| - } |
164 |
| - return '"' + string + '"'; |
165 |
| - }; |
166 |
| - |
167 |
| - var _escapeable = /["\\\x00-\x1f\x7f-\x9f]/g; |
168 |
| - |
169 |
| - var _meta = { |
170 |
| - '\b': '\\b', |
171 |
| - '\t': '\\t', |
172 |
| - '\n': '\\n', |
173 |
| - '\f': '\\f', |
174 |
| - '\r': '\\r', |
175 |
| - '"' : '\\"', |
176 |
| - '\\': '\\\\' |
177 |
| - }; |
178 |
| -})(jQuery); |
| 188 | +})( jQuery ); |
0 commit comments