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