93 |
93 |
94 /** |
94 /** |
95 * Converts to and from JSON format. |
95 * Converts to and from JSON format. |
96 * |
96 * |
97 * @example <code> |
97 * @example <code> |
98 // create a new instance of Services_JSON |
98 // create a new instance of Services_JSON |
99 $json = new Services_JSON(); |
99 $json = new Services_JSON(); |
100 |
100 |
101 // convert a complexe value to JSON notation, and send it to the browser |
101 // convert a complexe value to JSON notation, and send it to the browser |
102 $value = array('foo', 'bar', array(1, 2, 'baz'), array(3, array(4))); |
102 $value = array('foo', 'bar', array(1, 2, 'baz'), array(3, array(4))); |
103 $output = $json->encode($value); |
103 $output = $json->encode($value); |
104 |
104 |
105 print($output); |
105 print($output); |
106 // prints: ["foo","bar",[1,2,"baz"],[3,[4]]] |
106 // prints: ["foo","bar",[1,2,"baz"],[3,[4]]] |
107 |
107 |
108 // accept incoming POST data, assumed to be in JSON notation |
108 // accept incoming POST data, assumed to be in JSON notation |
109 $input = file_get_contents('php://input', 1000000); |
109 $input = file_get_contents('php://input', 1000000); |
110 $value = $json->decode($input); |
110 $value = $json->decode($input); |
111 </code> |
111 </code> |
112 * |
112 * |
113 */ |
113 */ |
114 class Services_JSON |
114 class Services_JSON |
115 { |
115 { |
116 /** |
116 /** |
117 * constructs a new JSON instance |
117 * constructs a new JSON instance |
118 * |
118 * |
119 * @param int $use object behavior flags; combine with boolean-OR |
119 * @param int $use object behavior flags; combine with boolean-OR |
120 * |
120 * |
121 * possible values: |
121 * possible values: |
122 * - SERVICES_JSON_LOOSE_TYPE: loose typing. |
122 * - SERVICES_JSON_LOOSE_TYPE: loose typing. |
123 * "{...}" syntax creates associative arrays |
123 * "{...}" syntax creates associative arrays |
124 * instead of objects in decode(). |
124 * instead of objects in decode(). |
125 * - SERVICES_JSON_SUPPRESS_ERRORS: error suppression. |
125 * - SERVICES_JSON_SUPPRESS_ERRORS: error suppression. |
126 * Values which can't be encoded (e.g. resources) |
126 * Values which can't be encoded (e.g. resources) |
127 * appear as NULL instead of throwing errors. |
127 * appear as NULL instead of throwing errors. |
128 * By default, a deeply-nested resource will |
128 * By default, a deeply-nested resource will |
129 * bubble up with an error, so all return values |
129 * bubble up with an error, so all return values |
130 * from encode() should be checked with isError() |
130 * from encode() should be checked with isError() |
131 */ |
131 */ |
132 function Services_JSON($use = 0) |
132 function Services_JSON($use = 0) |
133 { |
133 { |
134 $this->use = $use; |
134 $this->use = $use; |
135 } |
135 } |
136 |
136 |
137 /** |
137 /** |
138 * convert a string from one UTF-16 char to one UTF-8 char |
138 * convert a string from one UTF-16 char to one UTF-8 char |
139 * |
139 * |
140 * Normally should be handled by mb_convert_encoding, but |
140 * Normally should be handled by mb_convert_encoding, but |
141 * provides a slower PHP-only method for installations |
141 * provides a slower PHP-only method for installations |
142 * that lack the multibye string extension. |
142 * that lack the multibye string extension. |
143 * |
143 * |
144 * @param string $utf16 UTF-16 character |
144 * @param string $utf16 UTF-16 character |
145 * @return string UTF-8 character |
145 * @return string UTF-8 character |
146 * @access private |
146 * @access private |
147 */ |
147 */ |
148 function utf162utf8($utf16) |
148 function utf162utf8($utf16) |
149 { |
149 { |
150 // oh please oh please oh please oh please oh please |
150 // oh please oh please oh please oh please oh please |
151 if(function_exists('mb_convert_encoding')) { |
151 if(function_exists('mb_convert_encoding')) { |
152 return mb_convert_encoding($utf16, 'UTF-8', 'UTF-16'); |
152 return mb_convert_encoding($utf16, 'UTF-8', 'UTF-16'); |
153 } |
153 } |
154 |
154 |
155 $bytes = (ord($utf16{0}) << 8) | ord($utf16{1}); |
155 $bytes = (ord($utf16{0}) << 8) | ord($utf16{1}); |
156 |
156 |
157 switch(true) { |
157 switch(true) { |
158 case ((0x7F & $bytes) == $bytes): |
158 case ((0x7F & $bytes) == $bytes): |
159 // this case should never be reached, because we are in ASCII range |
159 // this case should never be reached, because we are in ASCII range |
160 // see: http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8 |
160 // see: http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8 |
161 return chr(0x7F & $bytes); |
161 return chr(0x7F & $bytes); |
162 |
162 |
163 case (0x07FF & $bytes) == $bytes: |
163 case (0x07FF & $bytes) == $bytes: |
164 // return a 2-byte UTF-8 character |
164 // return a 2-byte UTF-8 character |
165 // see: http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8 |
165 // see: http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8 |
166 return chr(0xC0 | (($bytes >> 6) & 0x1F)) |
166 return chr(0xC0 | (($bytes >> 6) & 0x1F)) |
167 . chr(0x80 | ($bytes & 0x3F)); |
167 . chr(0x80 | ($bytes & 0x3F)); |
168 |
168 |
169 case (0xFFFF & $bytes) == $bytes: |
169 case (0xFFFF & $bytes) == $bytes: |
170 // return a 3-byte UTF-8 character |
170 // return a 3-byte UTF-8 character |
171 // see: http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8 |
171 // see: http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8 |
172 return chr(0xE0 | (($bytes >> 12) & 0x0F)) |
172 return chr(0xE0 | (($bytes >> 12) & 0x0F)) |
173 . chr(0x80 | (($bytes >> 6) & 0x3F)) |
173 . chr(0x80 | (($bytes >> 6) & 0x3F)) |
174 . chr(0x80 | ($bytes & 0x3F)); |
174 . chr(0x80 | ($bytes & 0x3F)); |
175 } |
175 } |
176 |
176 |
177 // ignoring UTF-32 for now, sorry |
177 // ignoring UTF-32 for now, sorry |
178 return ''; |
178 return ''; |
179 } |
179 } |
180 |
180 |
181 /** |
181 /** |
182 * convert a string from one UTF-8 char to one UTF-16 char |
182 * convert a string from one UTF-8 char to one UTF-16 char |
183 * |
183 * |
184 * Normally should be handled by mb_convert_encoding, but |
184 * Normally should be handled by mb_convert_encoding, but |
185 * provides a slower PHP-only method for installations |
185 * provides a slower PHP-only method for installations |
186 * that lack the multibye string extension. |
186 * that lack the multibye string extension. |
187 * |
187 * |
188 * @param string $utf8 UTF-8 character |
188 * @param string $utf8 UTF-8 character |
189 * @return string UTF-16 character |
189 * @return string UTF-16 character |
190 * @access private |
190 * @access private |
191 */ |
191 */ |
192 function utf82utf16($utf8) |
192 function utf82utf16($utf8) |
193 { |
193 { |
194 // oh please oh please oh please oh please oh please |
194 // oh please oh please oh please oh please oh please |
195 if(function_exists('mb_convert_encoding')) { |
195 if(function_exists('mb_convert_encoding')) { |
196 return mb_convert_encoding($utf8, 'UTF-16', 'UTF-8'); |
196 return mb_convert_encoding($utf8, 'UTF-16', 'UTF-8'); |
197 } |
197 } |
198 |
198 |
199 switch(strlen($utf8)) { |
199 switch(strlen($utf8)) { |
200 case 1: |
200 case 1: |
201 // this case should never be reached, because we are in ASCII range |
201 // this case should never be reached, because we are in ASCII range |
202 // see: http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8 |
202 // see: http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8 |
203 return $utf8; |
203 return $utf8; |
204 |
204 |
205 case 2: |
205 case 2: |
206 // return a UTF-16 character from a 2-byte UTF-8 char |
206 // return a UTF-16 character from a 2-byte UTF-8 char |
207 // see: http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8 |
207 // see: http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8 |
208 return chr(0x07 & (ord($utf8{0}) >> 2)) |
208 return chr(0x07 & (ord($utf8{0}) >> 2)) |
209 . chr((0xC0 & (ord($utf8{0}) << 6)) |
209 . chr((0xC0 & (ord($utf8{0}) << 6)) |
210 | (0x3F & ord($utf8{1}))); |
210 | (0x3F & ord($utf8{1}))); |
211 |
211 |
212 case 3: |
212 case 3: |
213 // return a UTF-16 character from a 3-byte UTF-8 char |
213 // return a UTF-16 character from a 3-byte UTF-8 char |
214 // see: http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8 |
214 // see: http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8 |
215 return chr((0xF0 & (ord($utf8{0}) << 4)) |
215 return chr((0xF0 & (ord($utf8{0}) << 4)) |
216 | (0x0F & (ord($utf8{1}) >> 2))) |
216 | (0x0F & (ord($utf8{1}) >> 2))) |
217 . chr((0xC0 & (ord($utf8{1}) << 6)) |
217 . chr((0xC0 & (ord($utf8{1}) << 6)) |
218 | (0x7F & ord($utf8{2}))); |
218 | (0x7F & ord($utf8{2}))); |
219 } |
219 } |
220 |
220 |
221 // ignoring UTF-32 for now, sorry |
221 // ignoring UTF-32 for now, sorry |
222 return ''; |
222 return ''; |
223 } |
223 } |
224 |
224 |
225 /** |
225 /** |
226 * encodes an arbitrary variable into JSON format |
226 * encodes an arbitrary variable into JSON format |
227 * |
227 * |
228 * @param mixed $var any number, boolean, string, array, or object to be encoded. |
228 * @param mixed $var any number, boolean, string, array, or object to be encoded. |
229 * see argument 1 to Services_JSON() above for array-parsing behavior. |
229 * see argument 1 to Services_JSON() above for array-parsing behavior. |
230 * if var is a strng, note that encode() always expects it |
230 * if var is a strng, note that encode() always expects it |
231 * to be in ASCII or UTF-8 format! |
231 * to be in ASCII or UTF-8 format! |
232 * |
232 * |
233 * @return mixed JSON string representation of input var or an error if a problem occurs |
233 * @return mixed JSON string representation of input var or an error if a problem occurs |
234 * @access public |
234 * @access public |
235 */ |
235 */ |
236 function encode($var) |
236 function encode($var) |
237 { |
237 { |
238 switch (gettype($var)) { |
238 switch (gettype($var)) { |
239 case 'boolean': |
239 case 'boolean': |
240 return $var ? 'true' : 'false'; |
240 return $var ? 'true' : 'false'; |
241 |
241 |
242 case 'NULL': |
242 case 'NULL': |
243 return 'null'; |
243 return 'null'; |
244 |
244 |
245 case 'integer': |
245 case 'integer': |
246 return (int) $var; |
246 return (int) $var; |
247 |
247 |
248 case 'double': |
248 case 'double': |
249 case 'float': |
249 case 'float': |
250 return (float) $var; |
250 return (float) $var; |
251 |
251 |
252 case 'string': |
252 case 'string': |
253 // STRINGS ARE EXPECTED TO BE IN ASCII OR UTF-8 FORMAT |
253 // STRINGS ARE EXPECTED TO BE IN ASCII OR UTF-8 FORMAT |
254 $ascii = ''; |
254 $ascii = ''; |
255 $strlen_var = strlen($var); |
255 $strlen_var = strlen($var); |
256 |
256 |
257 /* |
257 /* |
258 * Iterate over every character in the string, |
258 * Iterate over every character in the string, |
259 * escaping with a slash or encoding to UTF-8 where necessary |
259 * escaping with a slash or encoding to UTF-8 where necessary |
260 */ |
260 */ |
261 for ($c = 0; $c < $strlen_var; ++$c) { |
261 for ($c = 0; $c < $strlen_var; ++$c) { |
262 |
262 |
263 $ord_var_c = ord($var{$c}); |
263 $ord_var_c = ord($var{$c}); |
264 |
264 |
265 switch (true) { |
265 switch (true) { |
266 case $ord_var_c == 0x08: |
266 case $ord_var_c == 0x08: |
267 $ascii .= '\b'; |
267 $ascii .= '\b'; |
268 break; |
268 break; |
269 case $ord_var_c == 0x09: |
269 case $ord_var_c == 0x09: |
270 $ascii .= '\t'; |
270 $ascii .= '\t'; |
271 break; |
271 break; |
272 case $ord_var_c == 0x0A: |
272 case $ord_var_c == 0x0A: |
273 $ascii .= '\n'; |
273 $ascii .= '\n'; |
274 break; |
274 break; |
275 case $ord_var_c == 0x0C: |
275 case $ord_var_c == 0x0C: |
276 $ascii .= '\f'; |
276 $ascii .= '\f'; |
277 break; |
277 break; |
278 case $ord_var_c == 0x0D: |
278 case $ord_var_c == 0x0D: |
279 $ascii .= '\r'; |
279 $ascii .= '\r'; |
280 break; |
280 break; |
281 |
281 |
282 case $ord_var_c == 0x22: |
282 case $ord_var_c == 0x22: |
283 case $ord_var_c == 0x2F: |
283 case $ord_var_c == 0x2F: |
284 case $ord_var_c == 0x5C: |
284 case $ord_var_c == 0x5C: |
285 // double quote, slash, slosh |
285 // double quote, slash, slosh |
286 $ascii .= '\\'.$var{$c}; |
286 $ascii .= '\\'.$var{$c}; |
287 break; |
287 break; |
288 |
288 |
289 case (($ord_var_c >= 0x20) && ($ord_var_c <= 0x7F)): |
289 case (($ord_var_c >= 0x20) && ($ord_var_c <= 0x7F)): |
290 // characters U-00000000 - U-0000007F (same as ASCII) |
290 // characters U-00000000 - U-0000007F (same as ASCII) |
291 $ascii .= $var{$c}; |
291 $ascii .= $var{$c}; |
292 break; |
292 break; |
293 |
293 |
294 case (($ord_var_c & 0xE0) == 0xC0): |
294 case (($ord_var_c & 0xE0) == 0xC0): |
295 // characters U-00000080 - U-000007FF, mask 110XXXXX |
295 // characters U-00000080 - U-000007FF, mask 110XXXXX |
296 // see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8 |
296 // see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8 |
297 $char = pack('C*', $ord_var_c, ord($var{$c + 1})); |
297 $char = pack('C*', $ord_var_c, ord($var{$c + 1})); |
298 $c += 1; |
298 $c += 1; |
299 $utf16 = $this->utf82utf16($char); |
299 $utf16 = $this->utf82utf16($char); |
300 $ascii .= sprintf('\u%04s', bin2hex($utf16)); |
300 $ascii .= sprintf('\u%04s', bin2hex($utf16)); |
301 break; |
301 break; |
302 |
302 |
303 case (($ord_var_c & 0xF0) == 0xE0): |
303 case (($ord_var_c & 0xF0) == 0xE0): |
304 // characters U-00000800 - U-0000FFFF, mask 1110XXXX |
304 // characters U-00000800 - U-0000FFFF, mask 1110XXXX |
305 // see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8 |
305 // see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8 |
306 $char = pack('C*', $ord_var_c, |
306 $char = pack('C*', $ord_var_c, |
307 ord($var{$c + 1}), |
307 ord($var{$c + 1}), |
308 ord($var{$c + 2})); |
308 ord($var{$c + 2})); |
309 $c += 2; |
309 $c += 2; |
310 $utf16 = $this->utf82utf16($char); |
310 $utf16 = $this->utf82utf16($char); |
311 $ascii .= sprintf('\u%04s', bin2hex($utf16)); |
311 $ascii .= sprintf('\u%04s', bin2hex($utf16)); |
312 break; |
312 break; |
313 |
313 |
314 case (($ord_var_c & 0xF8) == 0xF0): |
314 case (($ord_var_c & 0xF8) == 0xF0): |
315 // characters U-00010000 - U-001FFFFF, mask 11110XXX |
315 // characters U-00010000 - U-001FFFFF, mask 11110XXX |
316 // see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8 |
316 // see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8 |
317 $char = pack('C*', $ord_var_c, |
317 $char = pack('C*', $ord_var_c, |
318 ord($var{$c + 1}), |
318 ord($var{$c + 1}), |
319 ord($var{$c + 2}), |
319 ord($var{$c + 2}), |
320 ord($var{$c + 3})); |
320 ord($var{$c + 3})); |
321 $c += 3; |
321 $c += 3; |
322 $utf16 = $this->utf82utf16($char); |
322 $utf16 = $this->utf82utf16($char); |
323 $ascii .= sprintf('\u%04s', bin2hex($utf16)); |
323 $ascii .= sprintf('\u%04s', bin2hex($utf16)); |
324 break; |
324 break; |
325 |
325 |
326 case (($ord_var_c & 0xFC) == 0xF8): |
326 case (($ord_var_c & 0xFC) == 0xF8): |
327 // characters U-00200000 - U-03FFFFFF, mask 111110XX |
327 // characters U-00200000 - U-03FFFFFF, mask 111110XX |
328 // see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8 |
328 // see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8 |
329 $char = pack('C*', $ord_var_c, |
329 $char = pack('C*', $ord_var_c, |
330 ord($var{$c + 1}), |
330 ord($var{$c + 1}), |
331 ord($var{$c + 2}), |
331 ord($var{$c + 2}), |
332 ord($var{$c + 3}), |
332 ord($var{$c + 3}), |
333 ord($var{$c + 4})); |
333 ord($var{$c + 4})); |
334 $c += 4; |
334 $c += 4; |
335 $utf16 = $this->utf82utf16($char); |
335 $utf16 = $this->utf82utf16($char); |
336 $ascii .= sprintf('\u%04s', bin2hex($utf16)); |
336 $ascii .= sprintf('\u%04s', bin2hex($utf16)); |
337 break; |
337 break; |
338 |
338 |
339 case (($ord_var_c & 0xFE) == 0xFC): |
339 case (($ord_var_c & 0xFE) == 0xFC): |
340 // characters U-04000000 - U-7FFFFFFF, mask 1111110X |
340 // characters U-04000000 - U-7FFFFFFF, mask 1111110X |
341 // see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8 |
341 // see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8 |
342 $char = pack('C*', $ord_var_c, |
342 $char = pack('C*', $ord_var_c, |
343 ord($var{$c + 1}), |
343 ord($var{$c + 1}), |
344 ord($var{$c + 2}), |
344 ord($var{$c + 2}), |
345 ord($var{$c + 3}), |
345 ord($var{$c + 3}), |
346 ord($var{$c + 4}), |
346 ord($var{$c + 4}), |
347 ord($var{$c + 5})); |
347 ord($var{$c + 5})); |
348 $c += 5; |
348 $c += 5; |
349 $utf16 = $this->utf82utf16($char); |
349 $utf16 = $this->utf82utf16($char); |
350 $ascii .= sprintf('\u%04s', bin2hex($utf16)); |
350 $ascii .= sprintf('\u%04s', bin2hex($utf16)); |
351 break; |
351 break; |
352 } |
352 } |
353 } |
353 } |
354 |
354 |
355 return '"'.$ascii.'"'; |
355 return '"'.$ascii.'"'; |
356 |
356 |
357 case 'array': |
357 case 'array': |
358 /* |
358 /* |
359 * As per JSON spec if any array key is not an integer |
359 * As per JSON spec if any array key is not an integer |
360 * we must treat the the whole array as an object. We |
360 * we must treat the the whole array as an object. We |
361 * also try to catch a sparsely populated associative |
361 * also try to catch a sparsely populated associative |
362 * array with numeric keys here because some JS engines |
362 * array with numeric keys here because some JS engines |
363 * will create an array with empty indexes up to |
363 * will create an array with empty indexes up to |
364 * max_index which can cause memory issues and because |
364 * max_index which can cause memory issues and because |
365 * the keys, which may be relevant, will be remapped |
365 * the keys, which may be relevant, will be remapped |
366 * otherwise. |
366 * otherwise. |
367 * |
367 * |
368 * As per the ECMA and JSON specification an object may |
368 * As per the ECMA and JSON specification an object may |
369 * have any string as a property. Unfortunately due to |
369 * have any string as a property. Unfortunately due to |
370 * a hole in the ECMA specification if the key is a |
370 * a hole in the ECMA specification if the key is a |
371 * ECMA reserved word or starts with a digit the |
371 * ECMA reserved word or starts with a digit the |
372 * parameter is only accessible using ECMAScript's |
372 * parameter is only accessible using ECMAScript's |
373 * bracket notation. |
373 * bracket notation. |
374 */ |
374 */ |
375 |
375 |
376 // treat as a JSON object |
376 // treat as a JSON object |
377 if (is_array($var) && count($var) && (array_keys($var) !== range(0, sizeof($var) - 1))) { |
377 if (is_array($var) && count($var) && (array_keys($var) !== range(0, sizeof($var) - 1))) { |
378 $properties = array_map(array($this, 'name_value'), |
378 $properties = array_map(array($this, 'name_value'), |
379 array_keys($var), |
379 array_keys($var), |
380 array_values($var)); |
380 array_values($var)); |
381 |
381 |
382 foreach($properties as $property) { |
382 foreach($properties as $property) { |
383 if(Services_JSON::isError($property)) { |
383 if(Services_JSON::isError($property)) { |
384 return $property; |
384 return $property; |
385 } |
385 } |
386 } |
386 } |
387 |
387 |
388 return '{' . join(',', $properties) . '}'; |
388 return '{' . join(',', $properties) . '}'; |
389 } |
389 } |
390 |
390 |
391 // treat it like a regular array |
391 // treat it like a regular array |
392 $elements = array_map(array($this, 'encode'), $var); |
392 $elements = array_map(array($this, 'encode'), $var); |
393 |
393 |
394 foreach($elements as $element) { |
394 foreach($elements as $element) { |
395 if(Services_JSON::isError($element)) { |
395 if(Services_JSON::isError($element)) { |
396 return $element; |
396 return $element; |
397 } |
397 } |
398 } |
398 } |
399 |
399 |
400 return '[' . join(',', $elements) . ']'; |
400 return '[' . join(',', $elements) . ']'; |
401 |
401 |
402 case 'object': |
402 case 'object': |
403 $vars = get_object_vars($var); |
403 $vars = get_object_vars($var); |
404 |
404 |
405 $properties = array_map(array($this, 'name_value'), |
405 $properties = array_map(array($this, 'name_value'), |
406 array_keys($vars), |
406 array_keys($vars), |
407 array_values($vars)); |
407 array_values($vars)); |
408 |
408 |
409 foreach($properties as $property) { |
409 foreach($properties as $property) { |
410 if(Services_JSON::isError($property)) { |
410 if(Services_JSON::isError($property)) { |
411 return $property; |
411 return $property; |
412 } |
412 } |
413 } |
413 } |
414 |
414 |
415 return '{' . join(',', $properties) . '}'; |
415 return '{' . join(',', $properties) . '}'; |
416 |
416 |
417 default: |
417 default: |
418 return ($this->use & SERVICES_JSON_SUPPRESS_ERRORS) |
418 return ($this->use & SERVICES_JSON_SUPPRESS_ERRORS) |
419 ? 'null' |
419 ? 'null' |
420 : new Services_JSON_Error(gettype($var)." can not be encoded as JSON string"); |
420 : new Services_JSON_Error(gettype($var)." can not be encoded as JSON string"); |
421 } |
421 } |
422 } |
422 } |
423 |
423 |
424 /** |
424 /** |
425 * array-walking function for use in generating JSON-formatted name-value pairs |
425 * array-walking function for use in generating JSON-formatted name-value pairs |
426 * |
426 * |
427 * @param string $name name of key to use |
427 * @param string $name name of key to use |
428 * @param mixed $value reference to an array element to be encoded |
428 * @param mixed $value reference to an array element to be encoded |
429 * |
429 * |
430 * @return string JSON-formatted name-value pair, like '"name":value' |
430 * @return string JSON-formatted name-value pair, like '"name":value' |
431 * @access private |
431 * @access private |
432 */ |
432 */ |
433 function name_value($name, $value) |
433 function name_value($name, $value) |
434 { |
434 { |
435 $encoded_value = $this->encode($value); |
435 $encoded_value = $this->encode($value); |
436 |
436 |
437 if(Services_JSON::isError($encoded_value)) { |
437 if(Services_JSON::isError($encoded_value)) { |
438 return $encoded_value; |
438 return $encoded_value; |
439 } |
439 } |
440 |
440 |
441 return $this->encode(strval($name)) . ':' . $encoded_value; |
441 return $this->encode(strval($name)) . ':' . $encoded_value; |
442 } |
442 } |
443 |
443 |
444 /** |
444 /** |
445 * reduce a string by removing leading and trailing comments and whitespace |
445 * reduce a string by removing leading and trailing comments and whitespace |
446 * |
446 * |
447 * @param $str string string value to strip of comments and whitespace |
447 * @param $str string string value to strip of comments and whitespace |
448 * |
448 * |
449 * @return string string value stripped of comments and whitespace |
449 * @return string string value stripped of comments and whitespace |
450 * @access private |
450 * @access private |
451 */ |
451 */ |
452 function reduce_string($str) |
452 function reduce_string($str) |
453 { |
453 { |
454 $str = preg_replace(array( |
454 $str = preg_replace(array( |
455 |
455 |
456 // eliminate single line comments in '// ...' form |
456 // eliminate single line comments in '// ...' form |
457 '#^\s*//(.+)$#m', |
457 '#^\s*//(.+)$#m', |
458 |
458 |
459 // eliminate multi-line comments in '/* ... */' form, at start of string |
459 // eliminate multi-line comments in '/* ... */' form, at start of string |
460 '#^\s*/\*(.+)\*/#Us', |
460 '#^\s*/\*(.+)\*/#Us', |
461 |
461 |
462 // eliminate multi-line comments in '/* ... */' form, at end of string |
462 // eliminate multi-line comments in '/* ... */' form, at end of string |
463 '#/\*(.+)\*/\s*$#Us' |
463 '#/\*(.+)\*/\s*$#Us' |
464 |
464 |
465 ), '', $str); |
465 ), '', $str); |
466 |
466 |
467 // eliminate extraneous space |
467 // eliminate extraneous space |
468 return trim($str); |
468 return trim($str); |
469 } |
469 } |
470 |
470 |
471 /** |
471 /** |
472 * decodes a JSON string into appropriate variable |
472 * decodes a JSON string into appropriate variable |
473 * |
473 * |
474 * @param string $str JSON-formatted string |
474 * @param string $str JSON-formatted string |
475 * |
475 * |
476 * @return mixed number, boolean, string, array, or object |
476 * @return mixed number, boolean, string, array, or object |
477 * corresponding to given JSON input string. |
477 * corresponding to given JSON input string. |
478 * See argument 1 to Services_JSON() above for object-output behavior. |
478 * See argument 1 to Services_JSON() above for object-output behavior. |
479 * Note that decode() always returns strings |
479 * Note that decode() always returns strings |
480 * in ASCII or UTF-8 format! |
480 * in ASCII or UTF-8 format! |
481 * @access public |
481 * @access public |
482 */ |
482 */ |
483 function decode($str) |
483 function decode($str) |
484 { |
484 { |
485 $str = $this->reduce_string($str); |
485 $str = $this->reduce_string($str); |
486 |
486 |
487 switch (strtolower($str)) { |
487 switch (strtolower($str)) { |
488 case 'true': |
488 case 'true': |
489 return true; |
489 return true; |
490 |
490 |
491 case 'false': |
491 case 'false': |
492 return false; |
492 return false; |
493 |
493 |
494 case 'null': |
494 case 'null': |
495 return null; |
495 return null; |
496 |
496 |
497 default: |
497 default: |
498 $m = array(); |
498 $m = array(); |
499 |
499 |
500 if (is_numeric($str)) { |
500 if (is_numeric($str)) { |
501 // Lookie-loo, it's a number |
501 // Lookie-loo, it's a number |
502 |
502 |
503 // This would work on its own, but I'm trying to be |
503 // This would work on its own, but I'm trying to be |
504 // good about returning integers where appropriate: |
504 // good about returning integers where appropriate: |
505 // return (float)$str; |
505 // return (float)$str; |
506 |
506 |
507 // Return float or int, as appropriate |
507 // Return float or int, as appropriate |
508 return ((float)$str == (integer)$str) |
508 return ((float)$str == (integer)$str) |
509 ? (integer)$str |
509 ? (integer)$str |
510 : (float)$str; |
510 : (float)$str; |
511 |
511 |
512 } elseif (preg_match('/^("|\').*(\1)$/s', $str, $m) && $m[1] == $m[2]) { |
512 } elseif (preg_match('/^("|\').*(\1)$/s', $str, $m) && $m[1] == $m[2]) { |
513 // STRINGS RETURNED IN UTF-8 FORMAT |
513 // STRINGS RETURNED IN UTF-8 FORMAT |
514 $delim = substr($str, 0, 1); |
514 $delim = substr($str, 0, 1); |
515 $chrs = substr($str, 1, -1); |
515 $chrs = substr($str, 1, -1); |
516 $utf8 = ''; |
516 $utf8 = ''; |
517 $strlen_chrs = strlen($chrs); |
517 $strlen_chrs = strlen($chrs); |
518 |
518 |
519 for ($c = 0; $c < $strlen_chrs; ++$c) { |
519 for ($c = 0; $c < $strlen_chrs; ++$c) { |
520 |
520 |
521 $substr_chrs_c_2 = substr($chrs, $c, 2); |
521 $substr_chrs_c_2 = substr($chrs, $c, 2); |
522 $ord_chrs_c = ord($chrs{$c}); |
522 $ord_chrs_c = ord($chrs{$c}); |
523 |
523 |
524 switch (true) { |
524 switch (true) { |
525 case $substr_chrs_c_2 == '\b': |
525 case $substr_chrs_c_2 == '\b': |
526 $utf8 .= chr(0x08); |
526 $utf8 .= chr(0x08); |
527 ++$c; |
527 ++$c; |
528 break; |
528 break; |
529 case $substr_chrs_c_2 == '\t': |
529 case $substr_chrs_c_2 == '\t': |
530 $utf8 .= chr(0x09); |
530 $utf8 .= chr(0x09); |
531 ++$c; |
531 ++$c; |
532 break; |
532 break; |
533 case $substr_chrs_c_2 == '\n': |
533 case $substr_chrs_c_2 == '\n': |
534 $utf8 .= chr(0x0A); |
534 $utf8 .= chr(0x0A); |
535 ++$c; |
535 ++$c; |
536 break; |
536 break; |
537 case $substr_chrs_c_2 == '\f': |
537 case $substr_chrs_c_2 == '\f': |
538 $utf8 .= chr(0x0C); |
538 $utf8 .= chr(0x0C); |
539 ++$c; |
539 ++$c; |
540 break; |
540 break; |
541 case $substr_chrs_c_2 == '\r': |
541 case $substr_chrs_c_2 == '\r': |
542 $utf8 .= chr(0x0D); |
542 $utf8 .= chr(0x0D); |
543 ++$c; |
543 ++$c; |
544 break; |
544 break; |
545 |
545 |
546 case $substr_chrs_c_2 == '\\"': |
546 case $substr_chrs_c_2 == '\\"': |
547 case $substr_chrs_c_2 == '\\\'': |
547 case $substr_chrs_c_2 == '\\\'': |
548 case $substr_chrs_c_2 == '\\\\': |
548 case $substr_chrs_c_2 == '\\\\': |
549 case $substr_chrs_c_2 == '\\/': |
549 case $substr_chrs_c_2 == '\\/': |
550 if (($delim == '"' && $substr_chrs_c_2 != '\\\'') || |
550 if (($delim == '"' && $substr_chrs_c_2 != '\\\'') || |
551 ($delim == "'" && $substr_chrs_c_2 != '\\"')) { |
551 ($delim == "'" && $substr_chrs_c_2 != '\\"')) { |
552 $utf8 .= $chrs{++$c}; |
552 $utf8 .= $chrs{++$c}; |
553 } |
553 } |
554 break; |
554 break; |
555 |
555 |
556 case preg_match('/\\\u[0-9A-F]{4}/i', substr($chrs, $c, 6)): |
556 case preg_match('/\\\u[0-9A-F]{4}/i', substr($chrs, $c, 6)): |
557 // single, escaped unicode character |
557 // single, escaped unicode character |
558 $utf16 = chr(hexdec(substr($chrs, ($c + 2), 2))) |
558 $utf16 = chr(hexdec(substr($chrs, ($c + 2), 2))) |
559 . chr(hexdec(substr($chrs, ($c + 4), 2))); |
559 . chr(hexdec(substr($chrs, ($c + 4), 2))); |
560 $utf8 .= $this->utf162utf8($utf16); |
560 $utf8 .= $this->utf162utf8($utf16); |
561 $c += 5; |
561 $c += 5; |
562 break; |
562 break; |
563 |
563 |
564 case ($ord_chrs_c >= 0x20) && ($ord_chrs_c <= 0x7F): |
564 case ($ord_chrs_c >= 0x20) && ($ord_chrs_c <= 0x7F): |
565 $utf8 .= $chrs{$c}; |
565 $utf8 .= $chrs{$c}; |
566 break; |
566 break; |
567 |
567 |
568 case ($ord_chrs_c & 0xE0) == 0xC0: |
568 case ($ord_chrs_c & 0xE0) == 0xC0: |
569 // characters U-00000080 - U-000007FF, mask 110XXXXX |
569 // characters U-00000080 - U-000007FF, mask 110XXXXX |
570 //see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8 |
570 //see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8 |
571 $utf8 .= substr($chrs, $c, 2); |
571 $utf8 .= substr($chrs, $c, 2); |
572 ++$c; |
572 ++$c; |
573 break; |
573 break; |
574 |
574 |
575 case ($ord_chrs_c & 0xF0) == 0xE0: |
575 case ($ord_chrs_c & 0xF0) == 0xE0: |
576 // characters U-00000800 - U-0000FFFF, mask 1110XXXX |
576 // characters U-00000800 - U-0000FFFF, mask 1110XXXX |
577 // see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8 |
577 // see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8 |
578 $utf8 .= substr($chrs, $c, 3); |
578 $utf8 .= substr($chrs, $c, 3); |
579 $c += 2; |
579 $c += 2; |
580 break; |
580 break; |
581 |
581 |
582 case ($ord_chrs_c & 0xF8) == 0xF0: |
582 case ($ord_chrs_c & 0xF8) == 0xF0: |
583 // characters U-00010000 - U-001FFFFF, mask 11110XXX |
583 // characters U-00010000 - U-001FFFFF, mask 11110XXX |
584 // see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8 |
584 // see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8 |
585 $utf8 .= substr($chrs, $c, 4); |
585 $utf8 .= substr($chrs, $c, 4); |
586 $c += 3; |
586 $c += 3; |
587 break; |
587 break; |
588 |
588 |
589 case ($ord_chrs_c & 0xFC) == 0xF8: |
589 case ($ord_chrs_c & 0xFC) == 0xF8: |
590 // characters U-00200000 - U-03FFFFFF, mask 111110XX |
590 // characters U-00200000 - U-03FFFFFF, mask 111110XX |
591 // see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8 |
591 // see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8 |
592 $utf8 .= substr($chrs, $c, 5); |
592 $utf8 .= substr($chrs, $c, 5); |
593 $c += 4; |
593 $c += 4; |
594 break; |
594 break; |
595 |
595 |
596 case ($ord_chrs_c & 0xFE) == 0xFC: |
596 case ($ord_chrs_c & 0xFE) == 0xFC: |
597 // characters U-04000000 - U-7FFFFFFF, mask 1111110X |
597 // characters U-04000000 - U-7FFFFFFF, mask 1111110X |
598 // see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8 |
598 // see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8 |
599 $utf8 .= substr($chrs, $c, 6); |
599 $utf8 .= substr($chrs, $c, 6); |
600 $c += 5; |
600 $c += 5; |
601 break; |
601 break; |
602 |
602 |
603 } |
603 } |
604 |
604 |
605 } |
605 } |
606 |
606 |
607 return $utf8; |
607 return $utf8; |
608 |
608 |
609 } elseif (preg_match('/^\[.*\]$/s', $str) || preg_match('/^\{.*\}$/s', $str)) { |
609 } elseif (preg_match('/^\[.*\]$/s', $str) || preg_match('/^\{.*\}$/s', $str)) { |
610 // array, or object notation |
610 // array, or object notation |
611 |
611 |
612 if ($str{0} == '[') { |
612 if ($str{0} == '[') { |
613 $stk = array(SERVICES_JSON_IN_ARR); |
613 $stk = array(SERVICES_JSON_IN_ARR); |
614 $arr = array(); |
614 $arr = array(); |
615 } else { |
615 } else { |
616 if ($this->use & SERVICES_JSON_LOOSE_TYPE) { |
616 if ($this->use & SERVICES_JSON_LOOSE_TYPE) { |
617 $stk = array(SERVICES_JSON_IN_OBJ); |
617 $stk = array(SERVICES_JSON_IN_OBJ); |
618 $obj = array(); |
618 $obj = array(); |
619 } else { |
619 } else { |
620 $stk = array(SERVICES_JSON_IN_OBJ); |
620 $stk = array(SERVICES_JSON_IN_OBJ); |
621 $obj = new stdClass(); |
621 $obj = new stdClass(); |
622 } |
622 } |
623 } |
623 } |
624 |
624 |
625 array_push($stk, array('what' => SERVICES_JSON_SLICE, |
625 array_push($stk, array('what' => SERVICES_JSON_SLICE, |
626 'where' => 0, |
626 'where' => 0, |
627 'delim' => false)); |
627 'delim' => false)); |
628 |
628 |
629 $chrs = substr($str, 1, -1); |
629 $chrs = substr($str, 1, -1); |
630 $chrs = $this->reduce_string($chrs); |
630 $chrs = $this->reduce_string($chrs); |
631 |
631 |
632 if ($chrs == '') { |
632 if ($chrs == '') { |
633 if (reset($stk) == SERVICES_JSON_IN_ARR) { |
633 if (reset($stk) == SERVICES_JSON_IN_ARR) { |
634 return $arr; |
634 return $arr; |
635 |
635 |
636 } else { |
636 } else { |
637 return $obj; |
637 return $obj; |
638 |
638 |
639 } |
639 } |
640 } |
640 } |
641 |
641 |
642 //print("\nparsing {$chrs}\n"); |
642 //print("\nparsing {$chrs}\n"); |
643 |
643 |
644 $strlen_chrs = strlen($chrs); |
644 $strlen_chrs = strlen($chrs); |
645 |
645 |
646 for ($c = 0; $c <= $strlen_chrs; ++$c) { |
646 for ($c = 0; $c <= $strlen_chrs; ++$c) { |
647 |
647 |
648 $top = end($stk); |
648 $top = end($stk); |
649 $substr_chrs_c_2 = substr($chrs, $c, 2); |
649 $substr_chrs_c_2 = substr($chrs, $c, 2); |
650 |
650 |
651 if (($c == $strlen_chrs) || (($chrs{$c} == ',') && ($top['what'] == SERVICES_JSON_SLICE))) { |
651 if (($c == $strlen_chrs) || (($chrs{$c} == ',') && ($top['what'] == SERVICES_JSON_SLICE))) { |
652 // found a comma that is not inside a string, array, etc., |
652 // found a comma that is not inside a string, array, etc., |
653 // OR we've reached the end of the character list |
653 // OR we've reached the end of the character list |
654 $slice = substr($chrs, $top['where'], ($c - $top['where'])); |
654 $slice = substr($chrs, $top['where'], ($c - $top['where'])); |
655 array_push($stk, array('what' => SERVICES_JSON_SLICE, 'where' => ($c + 1), 'delim' => false)); |
655 array_push($stk, array('what' => SERVICES_JSON_SLICE, 'where' => ($c + 1), 'delim' => false)); |
656 //print("Found split at {$c}: ".substr($chrs, $top['where'], (1 + $c - $top['where']))."\n"); |
656 //print("Found split at {$c}: ".substr($chrs, $top['where'], (1 + $c - $top['where']))."\n"); |
657 |
657 |
658 if (reset($stk) == SERVICES_JSON_IN_ARR) { |
658 if (reset($stk) == SERVICES_JSON_IN_ARR) { |
659 // we are in an array, so just push an element onto the stack |
659 // we are in an array, so just push an element onto the stack |
660 array_push($arr, $this->decode($slice)); |
660 array_push($arr, $this->decode($slice)); |
661 |
661 |
662 } elseif (reset($stk) == SERVICES_JSON_IN_OBJ) { |
662 } elseif (reset($stk) == SERVICES_JSON_IN_OBJ) { |
663 // we are in an object, so figure |
663 // we are in an object, so figure |
664 // out the property name and set an |
664 // out the property name and set an |
665 // element in an associative array, |
665 // element in an associative array, |
666 // for now |
666 // for now |
667 $parts = array(); |
667 $parts = array(); |
668 |
668 |
669 if (preg_match('/^\s*(["\'].*[^\\\]["\'])\s*:\s*(\S.*),?$/Uis', $slice, $parts)) { |
669 if (preg_match('/^\s*(["\'].*[^\\\]["\'])\s*:\s*(\S.*),?$/Uis', $slice, $parts)) { |
670 // "name":value pair |
670 // "name":value pair |
671 $key = $this->decode($parts[1]); |
671 $key = $this->decode($parts[1]); |
672 $val = $this->decode($parts[2]); |
672 $val = $this->decode($parts[2]); |
673 |
673 |
674 if ($this->use & SERVICES_JSON_LOOSE_TYPE) { |
674 if ($this->use & SERVICES_JSON_LOOSE_TYPE) { |
675 $obj[$key] = $val; |
675 $obj[$key] = $val; |
676 } else { |
676 } else { |
677 $obj->$key = $val; |
677 $obj->$key = $val; |
678 } |
678 } |
679 } elseif (preg_match('/^\s*(\w+)\s*:\s*(\S.*),?$/Uis', $slice, $parts)) { |
679 } elseif (preg_match('/^\s*(\w+)\s*:\s*(\S.*),?$/Uis', $slice, $parts)) { |
680 // name:value pair, where name is unquoted |
680 // name:value pair, where name is unquoted |
681 $key = $parts[1]; |
681 $key = $parts[1]; |
682 $val = $this->decode($parts[2]); |
682 $val = $this->decode($parts[2]); |
683 |
683 |
684 if ($this->use & SERVICES_JSON_LOOSE_TYPE) { |
684 if ($this->use & SERVICES_JSON_LOOSE_TYPE) { |
685 $obj[$key] = $val; |
685 $obj[$key] = $val; |
686 } else { |
686 } else { |
687 $obj->$key = $val; |
687 $obj->$key = $val; |
688 } |
688 } |
689 } |
689 } |
690 |
690 |
691 } |
691 } |
692 |
692 |
693 } elseif ((($chrs{$c} == '"') || ($chrs{$c} == "'")) && ($top['what'] != SERVICES_JSON_IN_STR)) { |
693 } elseif ((($chrs{$c} == '"') || ($chrs{$c} == "'")) && ($top['what'] != SERVICES_JSON_IN_STR)) { |
694 // found a quote, and we are not inside a string |
694 // found a quote, and we are not inside a string |
695 array_push($stk, array('what' => SERVICES_JSON_IN_STR, 'where' => $c, 'delim' => $chrs{$c})); |
695 array_push($stk, array('what' => SERVICES_JSON_IN_STR, 'where' => $c, 'delim' => $chrs{$c})); |
696 //print("Found start of string at {$c}\n"); |
696 //print("Found start of string at {$c}\n"); |
697 |
697 |
698 } elseif (($chrs{$c} == $top['delim']) && |
698 } elseif (($chrs{$c} == $top['delim']) && |
699 ($top['what'] == SERVICES_JSON_IN_STR) && |
699 ($top['what'] == SERVICES_JSON_IN_STR) && |
700 ((strlen(substr($chrs, 0, $c)) - strlen(rtrim(substr($chrs, 0, $c), '\\'))) % 2 != 1)) { |
700 ((strlen(substr($chrs, 0, $c)) - strlen(rtrim(substr($chrs, 0, $c), '\\'))) % 2 != 1)) { |
701 // found a quote, we're in a string, and it's not escaped |
701 // found a quote, we're in a string, and it's not escaped |
702 // we know that it's not escaped becase there is _not_ an |
702 // we know that it's not escaped becase there is _not_ an |
703 // odd number of backslashes at the end of the string so far |
703 // odd number of backslashes at the end of the string so far |
704 array_pop($stk); |
704 array_pop($stk); |
705 //print("Found end of string at {$c}: ".substr($chrs, $top['where'], (1 + 1 + $c - $top['where']))."\n"); |
705 //print("Found end of string at {$c}: ".substr($chrs, $top['where'], (1 + 1 + $c - $top['where']))."\n"); |
706 |
706 |
707 } elseif (($chrs{$c} == '[') && |
707 } elseif (($chrs{$c} == '[') && |
708 in_array($top['what'], array(SERVICES_JSON_SLICE, SERVICES_JSON_IN_ARR, SERVICES_JSON_IN_OBJ))) { |
708 in_array($top['what'], array(SERVICES_JSON_SLICE, SERVICES_JSON_IN_ARR, SERVICES_JSON_IN_OBJ))) { |
709 // found a left-bracket, and we are in an array, object, or slice |
709 // found a left-bracket, and we are in an array, object, or slice |
710 array_push($stk, array('what' => SERVICES_JSON_IN_ARR, 'where' => $c, 'delim' => false)); |
710 array_push($stk, array('what' => SERVICES_JSON_IN_ARR, 'where' => $c, 'delim' => false)); |
711 //print("Found start of array at {$c}\n"); |
711 //print("Found start of array at {$c}\n"); |
712 |
712 |
713 } elseif (($chrs{$c} == ']') && ($top['what'] == SERVICES_JSON_IN_ARR)) { |
713 } elseif (($chrs{$c} == ']') && ($top['what'] == SERVICES_JSON_IN_ARR)) { |
714 // found a right-bracket, and we're in an array |
714 // found a right-bracket, and we're in an array |
715 array_pop($stk); |
715 array_pop($stk); |
716 //print("Found end of array at {$c}: ".substr($chrs, $top['where'], (1 + $c - $top['where']))."\n"); |
716 //print("Found end of array at {$c}: ".substr($chrs, $top['where'], (1 + $c - $top['where']))."\n"); |
717 |
717 |
718 } elseif (($chrs{$c} == '{') && |
718 } elseif (($chrs{$c} == '{') && |
719 in_array($top['what'], array(SERVICES_JSON_SLICE, SERVICES_JSON_IN_ARR, SERVICES_JSON_IN_OBJ))) { |
719 in_array($top['what'], array(SERVICES_JSON_SLICE, SERVICES_JSON_IN_ARR, SERVICES_JSON_IN_OBJ))) { |
720 // found a left-brace, and we are in an array, object, or slice |
720 // found a left-brace, and we are in an array, object, or slice |
721 array_push($stk, array('what' => SERVICES_JSON_IN_OBJ, 'where' => $c, 'delim' => false)); |
721 array_push($stk, array('what' => SERVICES_JSON_IN_OBJ, 'where' => $c, 'delim' => false)); |
722 //print("Found start of object at {$c}\n"); |
722 //print("Found start of object at {$c}\n"); |
723 |
723 |
724 } elseif (($chrs{$c} == '}') && ($top['what'] == SERVICES_JSON_IN_OBJ)) { |
724 } elseif (($chrs{$c} == '}') && ($top['what'] == SERVICES_JSON_IN_OBJ)) { |
725 // found a right-brace, and we're in an object |
725 // found a right-brace, and we're in an object |
726 array_pop($stk); |
726 array_pop($stk); |
727 //print("Found end of object at {$c}: ".substr($chrs, $top['where'], (1 + $c - $top['where']))."\n"); |
727 //print("Found end of object at {$c}: ".substr($chrs, $top['where'], (1 + $c - $top['where']))."\n"); |
728 |
728 |
729 } elseif (($substr_chrs_c_2 == '/*') && |
729 } elseif (($substr_chrs_c_2 == '/*') && |
730 in_array($top['what'], array(SERVICES_JSON_SLICE, SERVICES_JSON_IN_ARR, SERVICES_JSON_IN_OBJ))) { |
730 in_array($top['what'], array(SERVICES_JSON_SLICE, SERVICES_JSON_IN_ARR, SERVICES_JSON_IN_OBJ))) { |
731 // found a comment start, and we are in an array, object, or slice |
731 // found a comment start, and we are in an array, object, or slice |
732 array_push($stk, array('what' => SERVICES_JSON_IN_CMT, 'where' => $c, 'delim' => false)); |
732 array_push($stk, array('what' => SERVICES_JSON_IN_CMT, 'where' => $c, 'delim' => false)); |
733 $c++; |
733 $c++; |
734 //print("Found start of comment at {$c}\n"); |
734 //print("Found start of comment at {$c}\n"); |
735 |
735 |
736 } elseif (($substr_chrs_c_2 == '*/') && ($top['what'] == SERVICES_JSON_IN_CMT)) { |
736 } elseif (($substr_chrs_c_2 == '*/') && ($top['what'] == SERVICES_JSON_IN_CMT)) { |
737 // found a comment end, and we're in one now |
737 // found a comment end, and we're in one now |
738 array_pop($stk); |
738 array_pop($stk); |
739 $c++; |
739 $c++; |
740 |
740 |
741 for ($i = $top['where']; $i <= $c; ++$i) |
741 for ($i = $top['where']; $i <= $c; ++$i) |
742 $chrs = substr_replace($chrs, ' ', $i, 1); |
742 $chrs = substr_replace($chrs, ' ', $i, 1); |
743 |
743 |
744 //print("Found end of comment at {$c}: ".substr($chrs, $top['where'], (1 + $c - $top['where']))."\n"); |
744 //print("Found end of comment at {$c}: ".substr($chrs, $top['where'], (1 + $c - $top['where']))."\n"); |
745 |
745 |
746 } |
746 } |
747 |
747 |
748 } |
748 } |
749 |
749 |
750 if (reset($stk) == SERVICES_JSON_IN_ARR) { |
750 if (reset($stk) == SERVICES_JSON_IN_ARR) { |
751 return $arr; |
751 return $arr; |
752 |
752 |
753 } elseif (reset($stk) == SERVICES_JSON_IN_OBJ) { |
753 } elseif (reset($stk) == SERVICES_JSON_IN_OBJ) { |
754 return $obj; |
754 return $obj; |
755 |
755 |
756 } |
756 } |
757 |
757 |
758 } |
758 } |
759 } |
759 } |
760 } |
760 } |
761 |
761 |
762 /** |
762 /** |
763 * @todo Ultimately, this should just call PEAR::isError() |
763 * @todo Ultimately, this should just call PEAR::isError() |
764 */ |
764 */ |
765 function isError($data, $code = null) |
765 function isError($data, $code = null) |
766 { |
766 { |
767 if (class_exists('pear')) { |
767 if (class_exists('pear')) { |
768 return PEAR::isError($data, $code); |
768 return PEAR::isError($data, $code); |
769 } elseif (is_object($data) && (get_class($data) == 'services_json_error' || |
769 } elseif (is_object($data) && (get_class($data) == 'services_json_error' || |
770 is_subclass_of($data, 'services_json_error'))) { |
770 is_subclass_of($data, 'services_json_error'))) { |
771 return true; |
771 return true; |
772 } |
772 } |
773 |
773 |
774 return false; |
774 return false; |
775 } |
775 } |
776 } |
776 } |
777 |
777 |
778 if (class_exists('PEAR_Error')) { |
778 if (class_exists('PEAR_Error')) { |
779 |
779 |
780 class Services_JSON_Error extends PEAR_Error |
780 class Services_JSON_Error extends PEAR_Error |
781 { |
781 { |
782 function Services_JSON_Error($message = 'unknown error', $code = null, |
782 function Services_JSON_Error($message = 'unknown error', $code = null, |
783 $mode = null, $options = null, $userinfo = null) |
783 $mode = null, $options = null, $userinfo = null) |
784 { |
784 { |
785 parent::PEAR_Error($message, $code, $mode, $options, $userinfo); |
785 parent::PEAR_Error($message, $code, $mode, $options, $userinfo); |
786 } |
786 } |
787 } |
787 } |
788 |
788 |
789 } else { |
789 } else { |
790 |
790 |
791 /** |
791 /** |
792 * @todo Ultimately, this class shall be descended from PEAR_Error |
792 * @todo Ultimately, this class shall be descended from PEAR_Error |
793 */ |
793 */ |
794 class Services_JSON_Error |
794 class Services_JSON_Error |
795 { |
795 { |
796 function Services_JSON_Error($message = 'unknown error', $code = null, |
796 function Services_JSON_Error($message = 'unknown error', $code = null, |
797 $mode = null, $options = null, $userinfo = null) |
797 $mode = null, $options = null, $userinfo = null) |
798 { |
798 { |
799 |
799 |
800 } |
800 } |
801 } |
801 } |
802 |
802 |
803 } |
803 } |
804 |
804 |
805 ?> |
805 ?> |