|
1 | 1 | /**
|
2 |
| - * jQuery JSON Plugin v2.4-edge (2011-09-25) |
| 2 | + * jQuery JSON Plugin v2.4-edge (2012-05-13) |
3 | 3 | *
|
4 | 4 | * @author Brantley Harris, 2009-2011
|
5 |
| - * @author Timo Tijhof, 2011 |
| 5 | + * @author Timo Tijhof, 2011-2012 |
6 | 6 | * @source This plugin is heavily influenced by MochiKit's serializeJSON, which is
|
7 | 7 | * copyrighted 2005 by Bob Ippolito.
|
8 | 8 | * @source Brantley Harris wrote this plugin. It is based somewhat on the JSON.org
|
|
11 | 11 | * I uphold.
|
12 | 12 | * @license MIT License <http://www.opensource.org/licenses/mit-license.php>
|
13 | 13 | */
|
| 14 | +/*global jQuery */ |
| 15 | +/*jslint continue: true, plusplus: true */ |
| 16 | +(function ($) { |
| 17 | + "use strict"; |
14 | 18 |
|
15 |
| -(function( $ ) { |
16 |
| - |
17 |
| - var escapeable = /["\\\x00-\x1f\x7f-\x9f]/g, |
| 19 | + var escapeable = /["\\\x00-\x1f\x7f-\x9f]/g, |
18 | 20 | meta = {
|
19 | 21 | '\b': '\\b',
|
20 | 22 | '\t': '\\t',
|
|
37 | 39 | * function.
|
38 | 40 | *
|
39 | 41 | */
|
40 |
| - $.toJSON = typeof JSON === 'object' && JSON.stringify |
41 |
| - ? JSON.stringify |
42 |
| - : function( o ) { |
43 |
| - |
44 |
| - if ( o === null ) { |
| 42 | + $.toJSON = typeof JSON === 'object' && JSON.stringify ? JSON.stringify : function (o) { |
| 43 | + if (o === null) { |
45 | 44 | return 'null';
|
46 | 45 | }
|
47 | 46 |
|
48 |
| - var type = typeof o, $type = $.type( o ); |
| 47 | + var pairs, i, k, name, val, |
| 48 | + type = typeof o, |
| 49 | + $type = $.type(o); |
49 | 50 |
|
50 |
| - if ( type === 'undefined' ) { |
| 51 | + if (type === 'undefined') { |
51 | 52 | return undefined;
|
52 | 53 | }
|
53 |
| - if ( type === 'number' || type === 'boolean' ) { |
54 |
| - return '' + o; |
| 54 | + if (type === 'number' || type === 'boolean') { |
| 55 | + return String(o); |
55 | 56 | }
|
56 |
| - if ( type === 'string') { |
57 |
| - return $.quoteString( o ); |
| 57 | + if (type === 'string') { |
| 58 | + return $.quoteString(o); |
58 | 59 | }
|
59 |
| - if ( type === 'object' ) { |
60 |
| - if ( typeof o.toJSON === 'function' ) { |
61 |
| - return $.toJSON( o.toJSON() ); |
| 60 | + if (type === 'object') { |
| 61 | + if (typeof o.toJSON === 'function') { |
| 62 | + return $.toJSON(o.toJSON()); |
62 | 63 | }
|
63 |
| - if ( $type === 'date' ) { |
64 |
| - var month = o.getUTCMonth() + 1, |
| 64 | + if ($type === 'date') { |
| 65 | + var month = o.getUTCMonth() + 1, |
65 | 66 | day = o.getUTCDate(),
|
66 | 67 | year = o.getUTCFullYear(),
|
67 | 68 | hours = o.getUTCHours(),
|
68 | 69 | minutes = o.getUTCMinutes(),
|
69 | 70 | seconds = o.getUTCSeconds(),
|
70 | 71 | milli = o.getUTCMilliseconds();
|
71 | 72 |
|
72 |
| - if ( month < 10 ) { |
| 73 | + if (month < 10) { |
73 | 74 | month = '0' + month;
|
74 | 75 | }
|
75 |
| - if ( day < 10 ) { |
| 76 | + if (day < 10) { |
76 | 77 | day = '0' + day;
|
77 | 78 | }
|
78 |
| - if ( hours < 10 ) { |
| 79 | + if (hours < 10) { |
79 | 80 | hours = '0' + hours;
|
80 | 81 | }
|
81 |
| - if ( minutes < 10 ) { |
| 82 | + if (minutes < 10) { |
82 | 83 | minutes = '0' + minutes;
|
83 | 84 | }
|
84 |
| - if ( seconds < 10 ) { |
| 85 | + if (seconds < 10) { |
85 | 86 | seconds = '0' + seconds;
|
86 | 87 | }
|
87 |
| - if ( milli < 100 ) { |
| 88 | + if (milli < 100) { |
88 | 89 | milli = '0' + milli;
|
89 | 90 | }
|
90 |
| - if ( milli < 10 ) { |
| 91 | + if (milli < 10) { |
91 | 92 | milli = '0' + milli;
|
92 | 93 | }
|
93 | 94 | return '"' + year + '-' + month + '-' + day + 'T' +
|
94 | 95 | hours + ':' + minutes + ':' + seconds +
|
95 | 96 | '.' + milli + 'Z"';
|
96 | 97 | }
|
97 |
| - if ( $.isArray( o ) ) { |
98 |
| - var ret = []; |
99 |
| - for ( var i = 0; i < o.length; i++ ) { |
100 |
| - ret.push( $.toJSON( o[i] ) || 'null' ); |
| 98 | + |
| 99 | + pairs = []; |
| 100 | + |
| 101 | + if ($.isArray(o)) { |
| 102 | + for (i = 0; i < o.length; i++) { |
| 103 | + pairs.push($.toJSON(o[i]) || 'null'); |
101 | 104 | }
|
102 |
| - return '[' + ret.join(',') + ']'; |
| 105 | + return '[' + pairs.join(',') + ']'; |
103 | 106 | }
|
104 |
| - var name, |
105 |
| - val, |
106 |
| - pairs = []; |
107 |
| - for ( var k in o ) { |
| 107 | + |
| 108 | + // Plain object |
| 109 | + for (k in o) { |
108 | 110 | // Only include own properties,
|
109 | 111 | // Filter out inherited prototypes
|
110 |
| - if ( !hasOwn.call( o, k ) ) { |
| 112 | + if (!hasOwn.call(o, k)) { |
111 | 113 | continue;
|
112 | 114 | }
|
113 | 115 |
|
114 | 116 | // Keys must be numerical or string. Skip others
|
115 | 117 | type = typeof k;
|
116 |
| - if ( type === 'number' ) { |
| 118 | + if (type === 'number') { |
117 | 119 | name = '"' + k + '"';
|
118 | 120 | } else if (type === 'string') {
|
119 | 121 | name = $.quoteString(k);
|
|
125 | 127 | // Invalid values like these return undefined
|
126 | 128 | // from toJSON, however those object members
|
127 | 129 | // shouldn't be included in the JSON string at all.
|
128 |
| - if ( type === 'function' || type === 'undefined' ) { |
| 130 | + if (type === 'function' || type === 'undefined') { |
129 | 131 | continue;
|
130 | 132 | }
|
131 |
| - val = $.toJSON( o[k] ); |
132 |
| - pairs.push( name + ':' + val ); |
| 133 | + val = $.toJSON(o[k]); |
| 134 | + pairs.push(name + ':' + val); |
133 | 135 | }
|
134 |
| - return '{' + pairs.join( ',' ) + '}'; |
| 136 | + return '{' + pairs.join(',') + '}'; |
135 | 137 | }
|
136 | 138 | };
|
137 | 139 |
|
|
141 | 143 | *
|
142 | 144 | * @param src {String}
|
143 | 145 | */
|
144 |
| - $.evalJSON = typeof JSON === 'object' && JSON.parse |
145 |
| - ? JSON.parse |
146 |
| - : function( src ) { |
| 146 | + $.evalJSON = typeof JSON === 'object' && JSON.parse ? JSON.parse : function (src) { |
147 | 147 | return eval('(' + src + ')');
|
148 | 148 | };
|
149 | 149 |
|
|
153 | 153 | *
|
154 | 154 | * @param src {String}
|
155 | 155 | */
|
156 |
| - $.secureEvalJSON = typeof JSON === 'object' && JSON.parse |
157 |
| - ? JSON.parse |
158 |
| - : function( src ) { |
159 |
| - |
160 |
| - var filtered = |
| 156 | + $.secureEvalJSON = typeof JSON === 'object' && JSON.parse ? JSON.parse : function (src) { |
| 157 | + var filtered = |
161 | 158 | src
|
162 |
| - .replace( /\\["\\\/bfnrtu]/g, '@' ) |
163 |
| - .replace( /"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g, ']') |
164 |
| - .replace( /(?:^|:|,)(?:\s*\[)+/g, ''); |
| 159 | + .replace(/\\["\\\/bfnrtu]/g, '@') |
| 160 | + .replace(/"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g, ']') |
| 161 | + .replace(/(?:^|:|,)(?:\s*\[)+/g, ''); |
165 | 162 |
|
166 |
| - if ( /^[\],:{}\s]*$/.test( filtered ) ) { |
167 |
| - return eval( '(' + src + ')' ); |
168 |
| - } else { |
169 |
| - throw new SyntaxError( 'Error parsing JSON, source is not valid.' ); |
| 163 | + if (/^[\],:{}\s]*$/.test(filtered)) { |
| 164 | + return eval('(' + src + ')'); |
170 | 165 | }
|
| 166 | + throw new SyntaxError('Error parsing JSON, source is not valid.'); |
171 | 167 | };
|
172 | 168 |
|
173 | 169 | /**
|
|
181 | 177 | * >>> jQuery.quoteString('"Where are we going?", she asked.')
|
182 | 178 | * "\"Where are we going?\", she asked."
|
183 | 179 | */
|
184 |
| - $.quoteString = function( string ) { |
185 |
| - if ( string.match( escapeable ) ) { |
186 |
| - return '"' + string.replace( escapeable, function( a ) { |
| 180 | + $.quoteString = function (string) { |
| 181 | + if (string.match(escapeable)) { |
| 182 | + return '"' + string.replace(escapeable, function (a) { |
187 | 183 | var c = meta[a];
|
188 |
| - if ( typeof c === 'string' ) { |
| 184 | + if (typeof c === 'string') { |
189 | 185 | return c;
|
190 | 186 | }
|
191 | 187 | c = a.charCodeAt();
|
|
195 | 191 | return '"' + string + '"';
|
196 | 192 | };
|
197 | 193 |
|
198 |
| -})( jQuery ); |
| 194 | +}(jQuery)); |
0 commit comments