PageRenderTime 58ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 1ms

/lib/Services_JSON-1.0.2/JSON.php

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