2 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
4 // this is very debatable... - It should really be in encode
8 //header('Content-type: application/json');
10 * Converts to and from JSON format.
12 * JSON (JavaScript Object Notation) is a lightweight data-interchange
13 * format. It is easy for humans to read and write. It is easy for machines
14 * to parse and generate. It is based on a subset of the JavaScript
15 * Programming Language, Standard ECMA-262 3rd Edition - December 1999.
16 * This feature can also be found in Python. JSON is a text format that is
17 * completely language independent but uses conventions that are familiar
18 * to programmers of the C-family of languages, including C, C++, C#, Java,
19 * JavaScript, Perl, TCL, and many others. These properties make JSON an
20 * ideal data-interchange language.
22 * This package provides a simple encoder and decoder for JSON notation. It
23 * is intended for use with client-side Javascript applications that make
24 * use of HTTPRequest to perform server communication functions - data can
25 * be encoded into JSON notation for use in a client-side javascript, or
26 * decoded from incoming Javascript requests. JSON format is native to
27 * Javascript, and can be directly eval()'ed with no further parsing
30 * All strings should be in ASCII or UTF-8 format!
32 * LICENSE: Redistribution and use in source and binary forms, with or
33 * without modification, are permitted provided that the following
34 * conditions are met: Redistributions of source code must retain the
35 * above copyright notice, this list of conditions and the following
36 * disclaimer. Redistributions in binary form must reproduce the above
37 * copyright notice, this list of conditions and the following disclaimer
38 * in the documentation and/or other materials provided with the
41 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
42 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
43 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
44 * NO EVENT SHALL CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
45 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
46 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
47 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
48 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
49 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
50 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
54 * @package Services_JSON
55 * @author Michal Migurski <mike-json@teczno.com>
56 * @author Matt Knapp <mdknapp[at]gmail[dot]com>
57 * @author Brett Stimmerman <brettstimmerman[at]gmail[dot]com>
58 * @copyright 2005 Michal Migurski
59 * @version CVS: $Id: JSON.php,v 1.31 2006/06/28 05:54:17 migurski Exp $
60 * @license http://www.opensource.org/licenses/bsd-license.php
61 * @link http://pear.php.net/pepr/pepr-proposal-show.php?id=198
65 * Marker constant for Services_JSON::decode(), used to flag stack state
67 define('SERVICES_JSON_SLICE', 1);
70 * Marker constant for Services_JSON::decode(), used to flag stack state
72 define('SERVICES_JSON_IN_STR', 2);
75 * Marker constant for Services_JSON::decode(), used to flag stack state
77 define('SERVICES_JSON_IN_ARR', 3);
80 * Marker constant for Services_JSON::decode(), used to flag stack state
82 define('SERVICES_JSON_IN_OBJ', 4);
85 * Marker constant for Services_JSON::decode(), used to flag stack state
87 define('SERVICES_JSON_IN_CMT', 5);
90 * Behavior switch for Services_JSON::decode()
92 define('SERVICES_JSON_LOOSE_TYPE', 16);
95 * Behavior switch for Services_JSON::decode()
97 define('SERVICES_JSON_SUPPRESS_ERRORS', 32);
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()
138 function Services_JSON($use = 0)
144 * convert a string from one UTF-16 char to one UTF-8 char
146 * Normally should be handled by mb_convert_encoding, but
147 * provides a slower PHP-only method for installations
148 * that lack the multibye string extension.
150 * @param string $utf16 UTF-16 character
151 * @return string UTF-8 character
154 function utf162utf8($utf16)
156 // oh please oh please oh please oh please oh please
157 if(function_exists('mb_convert_encoding')) {
158 return mb_convert_encoding($utf16, 'UTF-8', 'UTF-16');
161 $bytes = (ord($utf16{0}) << 8) | ord($utf16{1});
164 case ((0x7F & $bytes) == $bytes):
165 // this case should never be reached, because we are in ASCII range
166 // see: http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
167 return chr(0x7F & $bytes);
169 case (0x07FF & $bytes) == $bytes:
170 // return a 2-byte UTF-8 character
171 // see: http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
172 return chr(0xC0 | (($bytes >> 6) & 0x1F))
173 . chr(0x80 | ($bytes & 0x3F));
175 case (0xFFFF & $bytes) == $bytes:
176 // return a 3-byte UTF-8 character
177 // see: http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
178 return chr(0xE0 | (($bytes >> 12) & 0x0F))
179 . chr(0x80 | (($bytes >> 6) & 0x3F))
180 . chr(0x80 | ($bytes & 0x3F));
183 // ignoring UTF-32 for now, sorry
188 * convert a string from one UTF-8 char to one UTF-16 char
190 * Normally should be handled by mb_convert_encoding, but
191 * provides a slower PHP-only method for installations
192 * that lack the multibye string extension.
194 * @param string $utf8 UTF-8 character
195 * @return string UTF-16 character
198 function utf82utf16($utf8)
200 // oh please oh please oh please oh please oh please
201 if(function_exists('mb_convert_encoding')) {
202 return mb_convert_encoding($utf8, 'UTF-16', 'UTF-8');
205 switch(strlen($utf8)) {
207 // this case should never be reached, because we are in ASCII range
208 // see: http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
212 // return a UTF-16 character from a 2-byte UTF-8 char
213 // see: http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
214 return chr(0x07 & (ord($utf8{0}) >> 2))
215 . chr((0xC0 & (ord($utf8{0}) << 6))
216 | (0x3F & ord($utf8{1})));
219 // return a UTF-16 character from a 3-byte UTF-8 char
220 // see: http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
221 return chr((0xF0 & (ord($utf8{0}) << 4))
222 | (0x0F & (ord($utf8{1}) >> 2)))
223 . chr((0xC0 & (ord($utf8{1}) << 6))
224 | (0x7F & ord($utf8{2})));
227 // ignoring UTF-32 for now, sorry
232 * encodes an arbitrary variable into JSON format (and sends JSON Header)
234 * @param mixed $var any number, boolean, string, array, or object to be encoded.
235 * see argument 1 to Services_JSON() above for array-parsing behavior.
236 * if var is a strng, note that encode() always expects it
237 * to be in ASCII or UTF-8 format!
239 * @return mixed JSON string representation of input var or an error if a problem occurs
242 function encode($var)
244 header('Content-type: application/x-javascript');
245 return $this->_encode($var);
248 * encodes an arbitrary variable into JSON format without JSON Header - warning - may allow CSS!!!!)
250 * @param mixed $var any number, boolean, string, array, or object to be encoded.
251 * see argument 1 to Services_JSON() above for array-parsing behavior.
252 * if var is a strng, note that encode() always expects it
253 * to be in ASCII or UTF-8 format!
255 * @return mixed JSON string representation of input var or an error if a problem occurs
258 function encodeUnsafe($var)
260 return $this->_encode($var);
263 * PRIVATE CODE that does the work of encodes an arbitrary variable into JSON format
265 * @param mixed $var any number, boolean, string, array, or object to be encoded.
266 * see argument 1 to Services_JSON() above for array-parsing behavior.
267 * if var is a strng, note that encode() always expects it
268 * to be in ASCII or UTF-8 format!
270 * @return mixed JSON string representation of input var or an error if a problem occurs
273 function _encode($var)
276 switch (gettype($var)) {
278 return $var ? 'true' : 'false';
291 // STRINGS ARE EXPECTED TO BE IN ASCII OR UTF-8 FORMAT
293 $strlen_var = strlen($var);
296 * Iterate over every character in the string,
297 * escaping with a slash or encoding to UTF-8 where necessary
299 for ($c = 0; $c < $strlen_var; ++$c) {
301 $ord_var_c = ord($var{$c});
304 case $ord_var_c == 0x08:
307 case $ord_var_c == 0x09:
310 case $ord_var_c == 0x0A:
313 case $ord_var_c == 0x0C:
316 case $ord_var_c == 0x0D:
320 case $ord_var_c == 0x22:
321 case $ord_var_c == 0x2F:
322 case $ord_var_c == 0x5C:
323 // double quote, slash, slosh
324 $ascii .= '\\'.$var{$c};
327 case (($ord_var_c >= 0x20) && ($ord_var_c <= 0x7F)):
328 // characters U-00000000 - U-0000007F (same as ASCII)
332 case (($ord_var_c & 0xE0) == 0xC0):
333 // characters U-00000080 - U-000007FF, mask 110XXXXX
334 // see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
335 if ($c+1 >= $strlen_var) {
341 $char = pack('C*', $ord_var_c, ord($var{$c + 1}));
343 $utf16 = $this->utf82utf16($char);
344 $ascii .= sprintf('\u%04s', bin2hex($utf16));
347 case (($ord_var_c & 0xF0) == 0xE0):
348 if ($c+2 >= $strlen_var) {
353 // characters U-00000800 - U-0000FFFF, mask 1110XXXX
354 // see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
355 $char = pack('C*', $ord_var_c,
359 $utf16 = $this->utf82utf16($char);
360 $ascii .= sprintf('\u%04s', bin2hex($utf16));
363 case (($ord_var_c & 0xF8) == 0xF0):
364 if ($c+3 >= $strlen_var) {
369 // characters U-00010000 - U-001FFFFF, mask 11110XXX
370 // see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
371 $char = pack('C*', $ord_var_c,
376 $utf16 = $this->utf82utf16($char);
377 $ascii .= sprintf('\u%04s', bin2hex($utf16));
380 case (($ord_var_c & 0xFC) == 0xF8):
381 // characters U-00200000 - U-03FFFFFF, mask 111110XX
382 // see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
383 if ($c+4 >= $strlen_var) {
388 $char = pack('C*', $ord_var_c,
394 $utf16 = $this->utf82utf16($char);
395 $ascii .= sprintf('\u%04s', bin2hex($utf16));
398 case (($ord_var_c & 0xFE) == 0xFC):
399 if ($c+5 >= $strlen_var) {
404 // characters U-04000000 - U-7FFFFFFF, mask 1111110X
405 // see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
406 $char = pack('C*', $ord_var_c,
413 $utf16 = $this->utf82utf16($char);
414 $ascii .= sprintf('\u%04s', bin2hex($utf16));
418 return (strpos($ascii, "'") > -1) ? '"'.$ascii.'"' : "'".$ascii."'";
422 * As per JSON spec if any array key is not an integer
423 * we must treat the the whole array as an object. We
424 * also try to catch a sparsely populated associative
425 * array with numeric keys here because some JS engines
426 * will create an array with empty indexes up to
427 * max_index which can cause memory issues and because
428 * the keys, which may be relevant, will be remapped
431 * As per the ECMA and JSON specification an object may
432 * have any string as a property. Unfortunately due to
433 * a hole in the ECMA specification if the key is a
434 * ECMA reserved word or starts with a digit the
435 * parameter is only accessible using ECMAScript's
439 // treat as a JSON object
440 if (is_array($var) && count($var) && (array_keys($var) !== range(0, sizeof($var) - 1))) {
441 $properties = array_map(array($this, 'name_value'),
445 foreach($properties as $property) {
446 if(Services_JSON::isError($property)) {
451 return '{' . join(',', $properties) . '}';
454 // treat it like a regular array
455 $elements = array_map(array($this, '_encode'), $var);
457 foreach($elements as $element) {
458 if(Services_JSON::isError($element)) {
463 return '[' . join(',', $elements) . ']';
466 $vars = get_object_vars($var);
468 $properties = array_map(array($this, 'name_value'),
470 array_values($vars));
472 foreach($properties as $property) {
473 if(Services_JSON::isError($property)) {
478 return '{' . join(',', $properties) . '}';
481 return ($this->use & SERVICES_JSON_SUPPRESS_ERRORS)
483 : new Services_JSON_Error(gettype($var)." can not be encoded as JSON string");
488 * array-walking function for use in generating JSON-formatted name-value pairs
490 * @param string $name name of key to use
491 * @param mixed $value reference to an array element to be encoded
493 * @return string JSON-formatted name-value pair, like '"name":value'
496 function name_value($name, $value)
498 $encoded_value = $this->_encode($value);
500 if(Services_JSON::isError($encoded_value)) {
501 return $encoded_value;
504 return $this->_encode(strval($name)) . ':' . $encoded_value;
508 * reduce a string by removing leading and trailing comments and whitespace
510 * @param $str string string value to strip of comments and whitespace
512 * @return string string value stripped of comments and whitespace
515 function reduce_string($str)
517 $str = preg_replace(array(
519 // eliminate single line comments in '// ...' form
522 // eliminate multi-line comments in '/* ... */' form, at start of string
523 '#^\s*/\*(.+)\*/#Us',
525 // eliminate multi-line comments in '/* ... */' form, at end of string
530 // eliminate extraneous space
535 * decodes a JSON string into appropriate variable
537 * @param string $str JSON-formatted string
539 * @return mixed number, boolean, string, array, or object
540 * corresponding to given JSON input string.
541 * See argument 1 to Services_JSON() above for object-output behavior.
542 * Note that decode() always returns strings
543 * in ASCII or UTF-8 format!
546 function decode($str)
548 $str = $this->reduce_string($str);
550 switch (strtolower($str)) {
563 if (is_numeric($str)) {
564 // Lookie-loo, it's a number
566 // This would work on its own, but I'm trying to be
567 // good about returning integers where appropriate:
568 // return (float)$str;
570 // Return float or int, as appropriate
571 return ((float)$str == (integer)$str)
575 } elseif (preg_match('/^("|\').*(\1)$/s', $str, $m) && $m[1] == $m[2]) {
576 // STRINGS RETURNED IN UTF-8 FORMAT
577 $delim = substr($str, 0, 1);
578 $chrs = substr($str, 1, -1);
580 $strlen_chrs = strlen($chrs);
582 for ($c = 0; $c < $strlen_chrs; ++$c) {
584 $substr_chrs_c_2 = substr($chrs, $c, 2);
585 $ord_chrs_c = ord($chrs{$c});
588 case $substr_chrs_c_2 == '\b':
592 case $substr_chrs_c_2 == '\t':
596 case $substr_chrs_c_2 == '\n':
600 case $substr_chrs_c_2 == '\f':
604 case $substr_chrs_c_2 == '\r':
609 case $substr_chrs_c_2 == '\\"':
610 case $substr_chrs_c_2 == '\\\'':
611 case $substr_chrs_c_2 == '\\\\':
612 case $substr_chrs_c_2 == '\\/':
613 if (($delim == '"' && $substr_chrs_c_2 != '\\\'') ||
614 ($delim == "'" && $substr_chrs_c_2 != '\\"')) {
615 $utf8 .= $chrs{++$c};
619 case preg_match('/\\\u[0-9A-F]{4}/i', substr($chrs, $c, 6)):
620 // single, escaped unicode character
621 $utf16 = chr(hexdec(substr($chrs, ($c + 2), 2)))
622 . chr(hexdec(substr($chrs, ($c + 4), 2)));
623 $utf8 .= $this->utf162utf8($utf16);
627 case ($ord_chrs_c >= 0x20) && ($ord_chrs_c <= 0x7F):
631 case ($ord_chrs_c & 0xE0) == 0xC0:
632 // characters U-00000080 - U-000007FF, mask 110XXXXX
633 //see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
634 $utf8 .= substr($chrs, $c, 2);
638 case ($ord_chrs_c & 0xF0) == 0xE0:
639 // characters U-00000800 - U-0000FFFF, mask 1110XXXX
640 // see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
641 $utf8 .= substr($chrs, $c, 3);
645 case ($ord_chrs_c & 0xF8) == 0xF0:
646 // characters U-00010000 - U-001FFFFF, mask 11110XXX
647 // see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
648 $utf8 .= substr($chrs, $c, 4);
652 case ($ord_chrs_c & 0xFC) == 0xF8:
653 // characters U-00200000 - U-03FFFFFF, mask 111110XX
654 // see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
655 $utf8 .= substr($chrs, $c, 5);
659 case ($ord_chrs_c & 0xFE) == 0xFC:
660 // characters U-04000000 - U-7FFFFFFF, mask 1111110X
661 // see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
662 $utf8 .= substr($chrs, $c, 6);
672 } elseif (preg_match('/^\[.*\]$/s', $str) || preg_match('/^\{.*\}$/s', $str)) {
673 // array, or object notation
675 if ($str{0} == '[') {
676 $stk = array(SERVICES_JSON_IN_ARR);
679 if ($this->use & SERVICES_JSON_LOOSE_TYPE) {
680 $stk = array(SERVICES_JSON_IN_OBJ);
683 $stk = array(SERVICES_JSON_IN_OBJ);
684 $obj = new stdClass();
688 array_push($stk, array('what' => SERVICES_JSON_SLICE,
692 $chrs = substr($str, 1, -1);
693 $chrs = $this->reduce_string($chrs);
696 if (reset($stk) == SERVICES_JSON_IN_ARR) {
705 //print("\nparsing {$chrs}\n");
707 $strlen_chrs = strlen($chrs);
709 for ($c = 0; $c <= $strlen_chrs; ++$c) {
712 $substr_chrs_c_2 = substr($chrs, $c, 2);
714 if (($c == $strlen_chrs) || (($chrs{$c} == ',') && ($top['what'] == SERVICES_JSON_SLICE))) {
715 // found a comma that is not inside a string, array, etc.,
716 // OR we've reached the end of the character list
717 $slice = substr($chrs, $top['where'], ($c - $top['where']));
718 array_push($stk, array('what' => SERVICES_JSON_SLICE, 'where' => ($c + 1), 'delim' => false));
719 //print("Found split at {$c}: ".substr($chrs, $top['where'], (1 + $c - $top['where']))."\n");
721 if (reset($stk) == SERVICES_JSON_IN_ARR) {
722 // we are in an array, so just push an element onto the stack
723 array_push($arr, $this->decode($slice));
725 } elseif (reset($stk) == SERVICES_JSON_IN_OBJ) {
726 // we are in an object, so figure
727 // out the property name and set an
728 // element in an associative array,
732 if (preg_match('/^\s*(["\'].*[^\\\]["\'])\s*:\s*(\S.*),?$/Uis', $slice, $parts)) {
734 $key = $this->decode($parts[1]);
735 $val = $this->decode($parts[2]);
737 if ($this->use & SERVICES_JSON_LOOSE_TYPE) {
742 } elseif (preg_match('/^\s*(\w+)\s*:\s*(\S.*),?$/Uis', $slice, $parts)) {
743 // name:value pair, where name is unquoted
745 $val = $this->decode($parts[2]);
747 if ($this->use & SERVICES_JSON_LOOSE_TYPE) {
756 } elseif ((($chrs{$c} == '"') || ($chrs{$c} == "'")) && ($top['what'] != SERVICES_JSON_IN_STR)) {
757 // found a quote, and we are not inside a string
758 array_push($stk, array('what' => SERVICES_JSON_IN_STR, 'where' => $c, 'delim' => $chrs{$c}));
759 //print("Found start of string at {$c}\n");
761 } elseif (($chrs{$c} == $top['delim']) &&
762 ($top['what'] == SERVICES_JSON_IN_STR) &&
763 ((strlen(substr($chrs, 0, $c)) - strlen(rtrim(substr($chrs, 0, $c), '\\'))) % 2 != 1)) {
764 // found a quote, we're in a string, and it's not escaped
765 // we know that it's not escaped becase there is _not_ an
766 // odd number of backslashes at the end of the string so far
768 //print("Found end of string at {$c}: ".substr($chrs, $top['where'], (1 + 1 + $c - $top['where']))."\n");
770 } elseif (($chrs{$c} == '[') &&
771 in_array($top['what'], array(SERVICES_JSON_SLICE, SERVICES_JSON_IN_ARR, SERVICES_JSON_IN_OBJ))) {
772 // found a left-bracket, and we are in an array, object, or slice
773 array_push($stk, array('what' => SERVICES_JSON_IN_ARR, 'where' => $c, 'delim' => false));
774 //print("Found start of array at {$c}\n");
776 } elseif (($chrs{$c} == ']') && ($top['what'] == SERVICES_JSON_IN_ARR)) {
777 // found a right-bracket, and we're in an array
779 //print("Found end of array at {$c}: ".substr($chrs, $top['where'], (1 + $c - $top['where']))."\n");
781 } elseif (($chrs{$c} == '{') &&
782 in_array($top['what'], array(SERVICES_JSON_SLICE, SERVICES_JSON_IN_ARR, SERVICES_JSON_IN_OBJ))) {
783 // found a left-brace, and we are in an array, object, or slice
784 array_push($stk, array('what' => SERVICES_JSON_IN_OBJ, 'where' => $c, 'delim' => false));
785 //print("Found start of object at {$c}\n");
787 } elseif (($chrs{$c} == '}') && ($top['what'] == SERVICES_JSON_IN_OBJ)) {
788 // found a right-brace, and we're in an object
790 //print("Found end of object at {$c}: ".substr($chrs, $top['where'], (1 + $c - $top['where']))."\n");
792 } elseif (($substr_chrs_c_2 == '/*') &&
793 in_array($top['what'], array(SERVICES_JSON_SLICE, SERVICES_JSON_IN_ARR, SERVICES_JSON_IN_OBJ))) {
794 // found a comment start, and we are in an array, object, or slice
795 array_push($stk, array('what' => SERVICES_JSON_IN_CMT, 'where' => $c, 'delim' => false));
797 //print("Found start of comment at {$c}\n");
799 } elseif (($substr_chrs_c_2 == '*/') && ($top['what'] == SERVICES_JSON_IN_CMT)) {
800 // found a comment end, and we're in one now
804 for ($i = $top['where']; $i <= $c; ++$i)
805 $chrs = substr_replace($chrs, ' ', $i, 1);
807 //print("Found end of comment at {$c}: ".substr($chrs, $top['where'], (1 + $c - $top['where']))."\n");
813 if (reset($stk) == SERVICES_JSON_IN_ARR) {
816 } elseif (reset($stk) == SERVICES_JSON_IN_OBJ) {
826 * @todo Ultimately, this should just call PEAR::isError()
828 function isError($data, $code = null)
830 if (class_exists('pear')) {
831 return PEAR::isError($data, $code);
832 } elseif (is_object($data) && (get_class($data) == 'services_json_error' ||
833 is_subclass_of($data, 'services_json_error'))) {
841 if (class_exists('PEAR_Error')) {
843 class Services_JSON_Error extends PEAR_Error
845 function Services_JSON_Error($message = 'unknown error', $code = null,
846 $mode = null, $options = null, $userinfo = null)
848 parent::PEAR_Error($message, $code, $mode, $options, $userinfo);
855 * @todo Ultimately, this class shall be descended from PEAR_Error
857 class Services_JSON_Error
859 function Services_JSON_Error($message = 'unknown error', $code = null,
860 $mode = null, $options = null, $userinfo = null)