2 if ( ! class_exists( 'Services_JSON' ) ) :
3 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
5 * Converts to and from JSON format.
7 * JSON (JavaScript Object Notation) is a lightweight data-interchange
8 * format. It is easy for humans to read and write. It is easy for machines
9 * to parse and generate. It is based on a subset of the JavaScript
10 * Programming Language, Standard ECMA-262 3rd Edition - December 1999.
11 * This feature can also be found in Python. JSON is a text format that is
12 * completely language independent but uses conventions that are familiar
13 * to programmers of the C-family of languages, including C, C++, C#, Java,
14 * JavaScript, Perl, TCL, and many others. These properties make JSON an
15 * ideal data-interchange language.
17 * This package provides a simple encoder and decoder for JSON notation. It
18 * is intended for use with client-side Javascript applications that make
19 * use of HTTPRequest to perform server communication functions - data can
20 * be encoded into JSON notation for use in a client-side javascript, or
21 * decoded from incoming Javascript requests. JSON format is native to
22 * Javascript, and can be directly eval()'ed with no further parsing
25 * All strings should be in ASCII or UTF-8 format!
27 * LICENSE: Redistribution and use in source and binary forms, with or
28 * without modification, are permitted provided that the following
29 * conditions are met: Redistributions of source code must retain the
30 * above copyright notice, this list of conditions and the following
31 * disclaimer. Redistributions in binary form must reproduce the above
32 * copyright notice, this list of conditions and the following disclaimer
33 * in the documentation and/or other materials provided with the
36 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
37 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
38 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
39 * NO EVENT SHALL CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
40 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
41 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
42 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
43 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
44 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
45 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
49 * @package Services_JSON
50 * @author Michal Migurski <mike-json@teczno.com>
51 * @author Matt Knapp <mdknapp[at]gmail[dot]com>
52 * @author Brett Stimmerman <brettstimmerman[at]gmail[dot]com>
53 * @copyright 2005 Michal Migurski
54 * @version CVS: $Id: JSON.php 305040 2010-11-02 23:19:03Z alan_k $
55 * @license http://www.opensource.org/licenses/bsd-license.php
56 * @link http://pear.php.net/pepr/pepr-proposal-show.php?id=198
60 * Marker constant for Services_JSON::decode(), used to flag stack state
62 define('SERVICES_JSON_SLICE', 1);
65 * Marker constant for Services_JSON::decode(), used to flag stack state
67 define('SERVICES_JSON_IN_STR', 2);
70 * Marker constant for Services_JSON::decode(), used to flag stack state
72 define('SERVICES_JSON_IN_ARR', 3);
75 * Marker constant for Services_JSON::decode(), used to flag stack state
77 define('SERVICES_JSON_IN_OBJ', 4);
80 * Marker constant for Services_JSON::decode(), used to flag stack state
82 define('SERVICES_JSON_IN_CMT', 5);
85 * Behavior switch for Services_JSON::decode()
87 define('SERVICES_JSON_LOOSE_TYPE', 16);
90 * Behavior switch for Services_JSON::decode()
92 define('SERVICES_JSON_SUPPRESS_ERRORS', 32);
95 * Behavior switch for Services_JSON::decode()
97 define('SERVICES_JSON_USE_TO_JSON', 64);
100 * Converts to and from JSON format.
102 * Brief example of use:
105 * // create a new instance of Services_JSON
106 * $json = new Services_JSON();
108 * // convert a complexe value to JSON notation, and send it to the browser
109 * $value = array('foo', 'bar', array(1, 2, 'baz'), array(3, array(4)));
110 * $output = $json->encode($value);
113 * // prints: ["foo","bar",[1,2,"baz"],[3,[4]]]
115 * // accept incoming POST data, assumed to be in JSON notation
116 * $input = file_get_contents('php://input', 1000000);
117 * $value = $json->decode($input);
123 * constructs a new JSON instance
125 * @param int $use object behavior flags; combine with boolean-OR
128 * - SERVICES_JSON_LOOSE_TYPE: loose typing.
129 * "{...}" syntax creates associative arrays
130 * instead of objects in decode().
131 * - SERVICES_JSON_SUPPRESS_ERRORS: error suppression.
132 * Values which can't be encoded (e.g. resources)
133 * appear as NULL instead of throwing errors.
134 * By default, a deeply-nested resource will
135 * bubble up with an error, so all return values
136 * from encode() should be checked with isError()
137 * - SERVICES_JSON_USE_TO_JSON: call toJSON when serializing objects
138 * It serializes the return value from the toJSON call rather
139 * than the object it'self, toJSON can return associative arrays,
140 * strings or numbers, if you return an object, make sure it does
141 * not have a toJSON method, otherwise an error will occur.
143 function Services_JSON($use = 0)
146 $this->_mb_strlen = function_exists('mb_strlen');
147 $this->_mb_convert_encoding = function_exists('mb_convert_encoding');
148 $this->_mb_substr = function_exists('mb_substr');
150 // private - cache the mbstring lookup results..
151 var $_mb_strlen = false;
152 var $_mb_substr = false;
153 var $_mb_convert_encoding = false;
156 * convert a string from one UTF-16 char to one UTF-8 char
158 * Normally should be handled by mb_convert_encoding, but
159 * provides a slower PHP-only method for installations
160 * that lack the multibye string extension.
162 * @param string $utf16 UTF-16 character
163 * @return string UTF-8 character
166 function utf162utf8($utf16)
168 // oh please oh please oh please oh please oh please
169 if($this->_mb_convert_encoding) {
170 return mb_convert_encoding($utf16, 'UTF-8', 'UTF-16');
173 $bytes = (ord($utf16{0}) << 8) | ord($utf16{1});
176 case ((0x7F & $bytes) == $bytes):
177 // this case should never be reached, because we are in ASCII range
178 // see: http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
179 return chr(0x7F & $bytes);
181 case (0x07FF & $bytes) == $bytes:
182 // return a 2-byte UTF-8 character
183 // see: http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
184 return chr(0xC0 | (($bytes >> 6) & 0x1F))
185 . chr(0x80 | ($bytes & 0x3F));
187 case (0xFFFF & $bytes) == $bytes:
188 // return a 3-byte UTF-8 character
189 // see: http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
190 return chr(0xE0 | (($bytes >> 12) & 0x0F))
191 . chr(0x80 | (($bytes >> 6) & 0x3F))
192 . chr(0x80 | ($bytes & 0x3F));
195 // ignoring UTF-32 for now, sorry
200 * convert a string from one UTF-8 char to one UTF-16 char
202 * Normally should be handled by mb_convert_encoding, but
203 * provides a slower PHP-only method for installations
204 * that lack the multibye string extension.
206 * @param string $utf8 UTF-8 character
207 * @return string UTF-16 character
210 function utf82utf16($utf8)
212 // oh please oh please oh please oh please oh please
213 if($this->_mb_convert_encoding) {
214 return mb_convert_encoding($utf8, 'UTF-16', 'UTF-8');
217 switch($this->strlen8($utf8)) {
219 // this case should never be reached, because we are in ASCII range
220 // see: http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
224 // return a UTF-16 character from a 2-byte UTF-8 char
225 // see: http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
226 return chr(0x07 & (ord($utf8{0}) >> 2))
227 . chr((0xC0 & (ord($utf8{0}) << 6))
228 | (0x3F & ord($utf8{1})));
231 // return a UTF-16 character from a 3-byte UTF-8 char
232 // see: http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
233 return chr((0xF0 & (ord($utf8{0}) << 4))
234 | (0x0F & (ord($utf8{1}) >> 2)))
235 . chr((0xC0 & (ord($utf8{1}) << 6))
236 | (0x7F & ord($utf8{2})));
239 // ignoring UTF-32 for now, sorry
244 * encodes an arbitrary variable into JSON format (and sends JSON Header)
246 * @param mixed $var any number, boolean, string, array, or object to be encoded.
247 * see argument 1 to Services_JSON() above for array-parsing behavior.
248 * if var is a strng, note that encode() always expects it
249 * to be in ASCII or UTF-8 format!
251 * @return mixed JSON string representation of input var or an error if a problem occurs
254 function encode($var)
256 header('Content-type: application/json');
257 return $this->encodeUnsafe($var);
260 * encodes an arbitrary variable into JSON format without JSON Header - warning - may allow XSS!!!!)
262 * @param mixed $var any number, boolean, string, array, or object to be encoded.
263 * see argument 1 to Services_JSON() above for array-parsing behavior.
264 * if var is a strng, note that encode() always expects it
265 * to be in ASCII or UTF-8 format!
267 * @return mixed JSON string representation of input var or an error if a problem occurs
270 function encodeUnsafe($var)
272 // see bug #16908 - regarding numeric locale printing
273 $lc = setlocale(LC_NUMERIC, 0);
274 setlocale(LC_NUMERIC, 'C');
275 $ret = $this->_encode($var);
276 setlocale(LC_NUMERIC, $lc);
281 * PRIVATE CODE that does the work of encodes an arbitrary variable into JSON format
283 * @param mixed $var any number, boolean, string, array, or object to be encoded.
284 * see argument 1 to Services_JSON() above for array-parsing behavior.
285 * if var is a strng, note that encode() always expects it
286 * to be in ASCII or UTF-8 format!
288 * @return mixed JSON string representation of input var or an error if a problem occurs
291 function _encode($var)
294 switch (gettype($var)) {
296 return $var ? 'true' : 'false';
309 // STRINGS ARE EXPECTED TO BE IN ASCII OR UTF-8 FORMAT
311 $strlen_var = $this->strlen8($var);
314 * Iterate over every character in the string,
315 * escaping with a slash or encoding to UTF-8 where necessary
317 for ($c = 0; $c < $strlen_var; ++$c) {
319 $ord_var_c = ord($var{$c});
322 case $ord_var_c == 0x08:
325 case $ord_var_c == 0x09:
328 case $ord_var_c == 0x0A:
331 case $ord_var_c == 0x0C:
334 case $ord_var_c == 0x0D:
338 case $ord_var_c == 0x22:
339 case $ord_var_c == 0x2F:
340 case $ord_var_c == 0x5C:
341 // double quote, slash, slosh
342 $ascii .= '\\'.$var{$c};
345 case (($ord_var_c >= 0x20) && ($ord_var_c <= 0x7F)):
346 // characters U-00000000 - U-0000007F (same as ASCII)
350 case (($ord_var_c & 0xE0) == 0xC0):
351 // characters U-00000080 - U-000007FF, mask 110XXXXX
352 // see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
353 if ($c+1 >= $strlen_var) {
359 $char = pack('C*', $ord_var_c, ord($var{$c + 1}));
361 $utf16 = $this->utf82utf16($char);
362 $ascii .= sprintf('\u%04s', bin2hex($utf16));
365 case (($ord_var_c & 0xF0) == 0xE0):
366 if ($c+2 >= $strlen_var) {
371 // characters U-00000800 - U-0000FFFF, mask 1110XXXX
372 // see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
373 $char = pack('C*', $ord_var_c,
377 $utf16 = $this->utf82utf16($char);
378 $ascii .= sprintf('\u%04s', bin2hex($utf16));
381 case (($ord_var_c & 0xF8) == 0xF0):
382 if ($c+3 >= $strlen_var) {
387 // characters U-00010000 - U-001FFFFF, mask 11110XXX
388 // see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
389 $char = pack('C*', $ord_var_c,
394 $utf16 = $this->utf82utf16($char);
395 $ascii .= sprintf('\u%04s', bin2hex($utf16));
398 case (($ord_var_c & 0xFC) == 0xF8):
399 // characters U-00200000 - U-03FFFFFF, mask 111110XX
400 // see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
401 if ($c+4 >= $strlen_var) {
406 $char = pack('C*', $ord_var_c,
412 $utf16 = $this->utf82utf16($char);
413 $ascii .= sprintf('\u%04s', bin2hex($utf16));
416 case (($ord_var_c & 0xFE) == 0xFC):
417 if ($c+5 >= $strlen_var) {
422 // characters U-04000000 - U-7FFFFFFF, mask 1111110X
423 // see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
424 $char = pack('C*', $ord_var_c,
431 $utf16 = $this->utf82utf16($char);
432 $ascii .= sprintf('\u%04s', bin2hex($utf16));
436 return '"'.$ascii.'"';
440 * As per JSON spec if any array key is not an integer
441 * we must treat the the whole array as an object. We
442 * also try to catch a sparsely populated associative
443 * array with numeric keys here because some JS engines
444 * will create an array with empty indexes up to
445 * max_index which can cause memory issues and because
446 * the keys, which may be relevant, will be remapped
449 * As per the ECMA and JSON specification an object may
450 * have any string as a property. Unfortunately due to
451 * a hole in the ECMA specification if the key is a
452 * ECMA reserved word or starts with a digit the
453 * parameter is only accessible using ECMAScript's
457 // treat as a JSON object
458 if (is_array($var) && count($var) && (array_keys($var) !== range(0, sizeof($var) - 1))) {
459 $properties = array_map(array($this, 'name_value'),
463 foreach($properties as $property) {
464 if(Services_JSON::isError($property)) {
469 return '{' . join(',', $properties) . '}';
472 // treat it like a regular array
473 $elements = array_map(array($this, '_encode'), $var);
475 foreach($elements as $element) {
476 if(Services_JSON::isError($element)) {
481 return '[' . join(',', $elements) . ']';
485 // support toJSON methods.
486 if (($this->use & SERVICES_JSON_USE_TO_JSON) && method_exists($var, 'toJSON')) {
487 // this may end up allowing unlimited recursion
488 // so we check the return value to make sure it's not got the same method.
489 $recode = $var->toJSON();
491 if (method_exists($recode, 'toJSON')) {
493 return ($this->use & SERVICES_JSON_SUPPRESS_ERRORS)
495 : new Services_JSON_Error(get_class($var).
496 " toJSON returned an object with a toJSON method.");
500 return $this->_encode( $recode );
503 $vars = get_object_vars($var);
505 $properties = array_map(array($this, 'name_value'),
507 array_values($vars));
509 foreach($properties as $property) {
510 if(Services_JSON::isError($property)) {
515 return '{' . join(',', $properties) . '}';
518 return ($this->use & SERVICES_JSON_SUPPRESS_ERRORS)
520 : new Services_JSON_Error(gettype($var)." can not be encoded as JSON string");
525 * array-walking function for use in generating JSON-formatted name-value pairs
527 * @param string $name name of key to use
528 * @param mixed $value reference to an array element to be encoded
530 * @return string JSON-formatted name-value pair, like '"name":value'
533 function name_value($name, $value)
535 $encoded_value = $this->_encode($value);
537 if(Services_JSON::isError($encoded_value)) {
538 return $encoded_value;
541 return $this->_encode(strval($name)) . ':' . $encoded_value;
545 * reduce a string by removing leading and trailing comments and whitespace
547 * @param $str string string value to strip of comments and whitespace
549 * @return string string value stripped of comments and whitespace
552 function reduce_string($str)
554 $str = preg_replace(array(
556 // eliminate single line comments in '// ...' form
559 // eliminate multi-line comments in '/* ... */' form, at start of string
560 '#^\s*/\*(.+)\*/#Us',
562 // eliminate multi-line comments in '/* ... */' form, at end of string
567 // eliminate extraneous space
572 * decodes a JSON string into appropriate variable
574 * @param string $str JSON-formatted string
576 * @return mixed number, boolean, string, array, or object
577 * corresponding to given JSON input string.
578 * See argument 1 to Services_JSON() above for object-output behavior.
579 * Note that decode() always returns strings
580 * in ASCII or UTF-8 format!
583 function decode($str)
585 $str = $this->reduce_string($str);
587 switch (strtolower($str)) {
600 if (is_numeric($str)) {
601 // Lookie-loo, it's a number
603 // This would work on its own, but I'm trying to be
604 // good about returning integers where appropriate:
605 // return (float)$str;
607 // Return float or int, as appropriate
608 return ((float)$str == (integer)$str)
612 } elseif (preg_match('/^("|\').*(\1)$/s', $str, $m) && $m[1] == $m[2]) {
613 // STRINGS RETURNED IN UTF-8 FORMAT
614 $delim = $this->substr8($str, 0, 1);
615 $chrs = $this->substr8($str, 1, -1);
617 $strlen_chrs = $this->strlen8($chrs);
619 for ($c = 0; $c < $strlen_chrs; ++$c) {
621 $substr_chrs_c_2 = $this->substr8($chrs, $c, 2);
622 $ord_chrs_c = ord($chrs{$c});
625 case $substr_chrs_c_2 == '\b':
629 case $substr_chrs_c_2 == '\t':
633 case $substr_chrs_c_2 == '\n':
637 case $substr_chrs_c_2 == '\f':
641 case $substr_chrs_c_2 == '\r':
646 case $substr_chrs_c_2 == '\\"':
647 case $substr_chrs_c_2 == '\\\'':
648 case $substr_chrs_c_2 == '\\\\':
649 case $substr_chrs_c_2 == '\\/':
650 if (($delim == '"' && $substr_chrs_c_2 != '\\\'') ||
651 ($delim == "'" && $substr_chrs_c_2 != '\\"')) {
652 $utf8 .= $chrs{++$c};
656 case preg_match('/\\\u[0-9A-F]{4}/i', $this->substr8($chrs, $c, 6)):
657 // single, escaped unicode character
658 $utf16 = chr(hexdec($this->substr8($chrs, ($c + 2), 2)))
659 . chr(hexdec($this->substr8($chrs, ($c + 4), 2)));
660 $utf8 .= $this->utf162utf8($utf16);
664 case ($ord_chrs_c >= 0x20) && ($ord_chrs_c <= 0x7F):
668 case ($ord_chrs_c & 0xE0) == 0xC0:
669 // characters U-00000080 - U-000007FF, mask 110XXXXX
670 //see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
671 $utf8 .= $this->substr8($chrs, $c, 2);
675 case ($ord_chrs_c & 0xF0) == 0xE0:
676 // characters U-00000800 - U-0000FFFF, mask 1110XXXX
677 // see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
678 $utf8 .= $this->substr8($chrs, $c, 3);
682 case ($ord_chrs_c & 0xF8) == 0xF0:
683 // characters U-00010000 - U-001FFFFF, mask 11110XXX
684 // see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
685 $utf8 .= $this->substr8($chrs, $c, 4);
689 case ($ord_chrs_c & 0xFC) == 0xF8:
690 // characters U-00200000 - U-03FFFFFF, mask 111110XX
691 // see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
692 $utf8 .= $this->substr8($chrs, $c, 5);
696 case ($ord_chrs_c & 0xFE) == 0xFC:
697 // characters U-04000000 - U-7FFFFFFF, mask 1111110X
698 // see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
699 $utf8 .= $this->substr8($chrs, $c, 6);
709 } elseif (preg_match('/^\[.*\]$/s', $str) || preg_match('/^\{.*\}$/s', $str)) {
710 // array, or object notation
712 if ($str{0} == '[') {
713 $stk = array(SERVICES_JSON_IN_ARR);
716 if ($this->use & SERVICES_JSON_LOOSE_TYPE) {
717 $stk = array(SERVICES_JSON_IN_OBJ);
720 $stk = array(SERVICES_JSON_IN_OBJ);
721 $obj = new stdClass();
725 array_push($stk, array('what' => SERVICES_JSON_SLICE,
729 $chrs = $this->substr8($str, 1, -1);
730 $chrs = $this->reduce_string($chrs);
733 if (reset($stk) == SERVICES_JSON_IN_ARR) {
742 //print("\nparsing {$chrs}\n");
744 $strlen_chrs = $this->strlen8($chrs);
746 for ($c = 0; $c <= $strlen_chrs; ++$c) {
749 $substr_chrs_c_2 = $this->substr8($chrs, $c, 2);
751 if (($c == $strlen_chrs) || (($chrs{$c} == ',') && ($top['what'] == SERVICES_JSON_SLICE))) {
752 // found a comma that is not inside a string, array, etc.,
753 // OR we've reached the end of the character list
754 $slice = $this->substr8($chrs, $top['where'], ($c - $top['where']));
755 array_push($stk, array('what' => SERVICES_JSON_SLICE, 'where' => ($c + 1), 'delim' => false));
756 //print("Found split at {$c}: ".$this->substr8($chrs, $top['where'], (1 + $c - $top['where']))."\n");
758 if (reset($stk) == SERVICES_JSON_IN_ARR) {
759 // we are in an array, so just push an element onto the stack
760 array_push($arr, $this->decode($slice));
762 } elseif (reset($stk) == SERVICES_JSON_IN_OBJ) {
763 // we are in an object, so figure
764 // out the property name and set an
765 // element in an associative array,
769 if (preg_match('/^\s*(["\'].*[^\\\]["\'])\s*:/Uis', $slice, $parts)) {
771 $key = $this->decode($parts[1]);
772 $val = $this->decode(trim(substr($slice, strlen($parts[0])), ", \t\n\r\0\x0B"));
773 if ($this->use & SERVICES_JSON_LOOSE_TYPE) {
778 } elseif (preg_match('/^\s*(\w+)\s*:/Uis', $slice, $parts)) {
779 // name:value pair, where name is unquoted
781 $val = $this->decode(trim(substr($slice, strlen($parts[0])), ", \t\n\r\0\x0B"));
783 if ($this->use & SERVICES_JSON_LOOSE_TYPE) {
792 } elseif ((($chrs{$c} == '"') || ($chrs{$c} == "'")) && ($top['what'] != SERVICES_JSON_IN_STR)) {
793 // found a quote, and we are not inside a string
794 array_push($stk, array('what' => SERVICES_JSON_IN_STR, 'where' => $c, 'delim' => $chrs{$c}));
795 //print("Found start of string at {$c}\n");
797 } elseif (($chrs{$c} == $top['delim']) &&
798 ($top['what'] == SERVICES_JSON_IN_STR) &&
799 (($this->strlen8($this->substr8($chrs, 0, $c)) - $this->strlen8(rtrim($this->substr8($chrs, 0, $c), '\\'))) % 2 != 1)) {
800 // found a quote, we're in a string, and it's not escaped
801 // we know that it's not escaped becase there is _not_ an
802 // odd number of backslashes at the end of the string so far
804 //print("Found end of string at {$c}: ".$this->substr8($chrs, $top['where'], (1 + 1 + $c - $top['where']))."\n");
806 } elseif (($chrs{$c} == '[') &&
807 in_array($top['what'], array(SERVICES_JSON_SLICE, SERVICES_JSON_IN_ARR, SERVICES_JSON_IN_OBJ))) {
808 // found a left-bracket, and we are in an array, object, or slice
809 array_push($stk, array('what' => SERVICES_JSON_IN_ARR, 'where' => $c, 'delim' => false));
810 //print("Found start of array at {$c}\n");
812 } elseif (($chrs{$c} == ']') && ($top['what'] == SERVICES_JSON_IN_ARR)) {
813 // found a right-bracket, and we're in an array
815 //print("Found end of array at {$c}: ".$this->substr8($chrs, $top['where'], (1 + $c - $top['where']))."\n");
817 } elseif (($chrs{$c} == '{') &&
818 in_array($top['what'], array(SERVICES_JSON_SLICE, SERVICES_JSON_IN_ARR, SERVICES_JSON_IN_OBJ))) {
819 // found a left-brace, and we are in an array, object, or slice
820 array_push($stk, array('what' => SERVICES_JSON_IN_OBJ, 'where' => $c, 'delim' => false));
821 //print("Found start of object at {$c}\n");
823 } elseif (($chrs{$c} == '}') && ($top['what'] == SERVICES_JSON_IN_OBJ)) {
824 // found a right-brace, and we're in an object
826 //print("Found end of object at {$c}: ".$this->substr8($chrs, $top['where'], (1 + $c - $top['where']))."\n");
828 } elseif (($substr_chrs_c_2 == '/*') &&
829 in_array($top['what'], array(SERVICES_JSON_SLICE, SERVICES_JSON_IN_ARR, SERVICES_JSON_IN_OBJ))) {
830 // found a comment start, and we are in an array, object, or slice
831 array_push($stk, array('what' => SERVICES_JSON_IN_CMT, 'where' => $c, 'delim' => false));
833 //print("Found start of comment at {$c}\n");
835 } elseif (($substr_chrs_c_2 == '*/') && ($top['what'] == SERVICES_JSON_IN_CMT)) {
836 // found a comment end, and we're in one now
840 for ($i = $top['where']; $i <= $c; ++$i)
841 $chrs = substr_replace($chrs, ' ', $i, 1);
843 //print("Found end of comment at {$c}: ".$this->substr8($chrs, $top['where'], (1 + $c - $top['where']))."\n");
849 if (reset($stk) == SERVICES_JSON_IN_ARR) {
852 } elseif (reset($stk) == SERVICES_JSON_IN_OBJ) {
862 * @todo Ultimately, this should just call PEAR::isError()
864 function isError($data, $code = null)
866 if (class_exists('pear')) {
867 return PEAR::isError($data, $code);
868 } elseif (is_object($data) && (get_class($data) == 'services_json_error' ||
869 is_subclass_of($data, 'services_json_error'))) {
877 * Calculates length of string in bytes
879 * @return integer length
881 function strlen8( $str )
883 if ( $this->_mb_strlen ) {
884 return mb_strlen( $str, "8bit" );
886 return strlen( $str );
890 * Returns part of a string, interpreting $start and $length as number of bytes.
892 * @param integer start
893 * @param integer length
894 * @return integer length
896 function substr8( $string, $start, $length=false )
898 if ( $length === false ) {
899 $length = $this->strlen8( $string ) - $start;
901 if ( $this->_mb_substr ) {
902 return mb_substr( $string, $start, $length, "8bit" );
904 return substr( $string, $start, $length );
909 if (class_exists('PEAR_Error')) {
911 class Services_JSON_Error extends PEAR_Error
913 function Services_JSON_Error($message = 'unknown error', $code = null,
914 $mode = null, $options = null, $userinfo = null)
916 parent::PEAR_Error($message, $code, $mode, $options, $userinfo);
923 * @todo Ultimately, this class shall be descended from PEAR_Error
925 class Services_JSON_Error
927 function Services_JSON_Error($message = 'unknown error', $code = null,
928 $mode = null, $options = null, $userinfo = null)