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