JSON.php 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806
  1. <?php
  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,v 1.31 2006/06/28 05:54:17 migurski Exp $
  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
  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. switch (gettype($var)) {
  219. case 'boolean':
  220. return $var ? 'true' : 'false';
  221. case 'NULL':
  222. return 'null';
  223. case 'integer':
  224. return (int) $var;
  225. case 'double':
  226. case 'float':
  227. return (float) $var;
  228. case 'string':
  229. // STRINGS ARE EXPECTED TO BE IN ASCII OR UTF-8 FORMAT
  230. $ascii = '';
  231. $strlen_var = strlen($var);
  232. /*
  233. * Iterate over every character in the string,
  234. * escaping with a slash or encoding to UTF-8 where necessary
  235. */
  236. for ($c = 0; $c < $strlen_var; ++$c) {
  237. $ord_var_c = ord($var{$c});
  238. switch (true) {
  239. case $ord_var_c == 0x08:
  240. $ascii .= '\b';
  241. break;
  242. case $ord_var_c == 0x09:
  243. $ascii .= '\t';
  244. break;
  245. case $ord_var_c == 0x0A:
  246. $ascii .= '\n';
  247. break;
  248. case $ord_var_c == 0x0C:
  249. $ascii .= '\f';
  250. break;
  251. case $ord_var_c == 0x0D:
  252. $ascii .= '\r';
  253. break;
  254. case $ord_var_c == 0x22:
  255. case $ord_var_c == 0x2F:
  256. case $ord_var_c == 0x5C:
  257. // double quote, slash, slosh
  258. $ascii .= '\\'.$var{$c};
  259. break;
  260. case (($ord_var_c >= 0x20) && ($ord_var_c <= 0x7F)):
  261. // characters U-00000000 - U-0000007F (same as ASCII)
  262. $ascii .= $var{$c};
  263. break;
  264. case (($ord_var_c & 0xE0) == 0xC0):
  265. // characters U-00000080 - U-000007FF, mask 110XXXXX
  266. // see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
  267. $char = pack('C*', $ord_var_c, ord($var{$c + 1}));
  268. $c += 1;
  269. $utf16 = $this->utf82utf16($char);
  270. $ascii .= sprintf('\u%04s', bin2hex($utf16));
  271. break;
  272. case (($ord_var_c & 0xF0) == 0xE0):
  273. // characters U-00000800 - U-0000FFFF, mask 1110XXXX
  274. // see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
  275. $char = pack('C*', $ord_var_c,
  276. ord($var{$c + 1}),
  277. ord($var{$c + 2}));
  278. $c += 2;
  279. $utf16 = $this->utf82utf16($char);
  280. $ascii .= sprintf('\u%04s', bin2hex($utf16));
  281. break;
  282. case (($ord_var_c & 0xF8) == 0xF0):
  283. // characters U-00010000 - U-001FFFFF, mask 11110XXX
  284. // see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
  285. $char = pack('C*', $ord_var_c,
  286. ord($var{$c + 1}),
  287. ord($var{$c + 2}),
  288. ord($var{$c + 3}));
  289. $c += 3;
  290. $utf16 = $this->utf82utf16($char);
  291. $ascii .= sprintf('\u%04s', bin2hex($utf16));
  292. break;
  293. case (($ord_var_c & 0xFC) == 0xF8):
  294. // characters U-00200000 - U-03FFFFFF, mask 111110XX
  295. // see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
  296. $char = pack('C*', $ord_var_c,
  297. ord($var{$c + 1}),
  298. ord($var{$c + 2}),
  299. ord($var{$c + 3}),
  300. ord($var{$c + 4}));
  301. $c += 4;
  302. $utf16 = $this->utf82utf16($char);
  303. $ascii .= sprintf('\u%04s', bin2hex($utf16));
  304. break;
  305. case (($ord_var_c & 0xFE) == 0xFC):
  306. // characters U-04000000 - U-7FFFFFFF, mask 1111110X
  307. // see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
  308. $char = pack('C*', $ord_var_c,
  309. ord($var{$c + 1}),
  310. ord($var{$c + 2}),
  311. ord($var{$c + 3}),
  312. ord($var{$c + 4}),
  313. ord($var{$c + 5}));
  314. $c += 5;
  315. $utf16 = $this->utf82utf16($char);
  316. $ascii .= sprintf('\u%04s', bin2hex($utf16));
  317. break;
  318. }
  319. }
  320. return '"'.$ascii.'"';
  321. case 'array':
  322. /*
  323. * As per JSON spec if any array key is not an integer
  324. * we must treat the the whole array as an object. We
  325. * also try to catch a sparsely populated associative
  326. * array with numeric keys here because some JS engines
  327. * will create an array with empty indexes up to
  328. * max_index which can cause memory issues and because
  329. * the keys, which may be relevant, will be remapped
  330. * otherwise.
  331. *
  332. * As per the ECMA and JSON specification an object may
  333. * have any string as a property. Unfortunately due to
  334. * a hole in the ECMA specification if the key is a
  335. * ECMA reserved word or starts with a digit the
  336. * parameter is only accessible using ECMAScript's
  337. * bracket notation.
  338. */
  339. // treat as a JSON object
  340. if (is_array($var) && count($var) && (array_keys($var) !== range(0, sizeof($var) - 1))) {
  341. $properties = array_map(array($this, 'name_value'),
  342. array_keys($var),
  343. array_values($var));
  344. foreach($properties as $property) {
  345. if(Services_JSON::isError($property)) {
  346. return $property;
  347. }
  348. }
  349. return '{' . join(',', $properties) . '}';
  350. }
  351. // treat it like a regular array
  352. $elements = array_map(array($this, 'encode'), $var);
  353. foreach($elements as $element) {
  354. if(Services_JSON::isError($element)) {
  355. return $element;
  356. }
  357. }
  358. return '[' . join(',', $elements) . ']';
  359. case 'object':
  360. $vars = get_object_vars($var);
  361. $properties = array_map(array($this, 'name_value'),
  362. array_keys($vars),
  363. array_values($vars));
  364. foreach($properties as $property) {
  365. if(Services_JSON::isError($property)) {
  366. return $property;
  367. }
  368. }
  369. return '{' . join(',', $properties) . '}';
  370. default:
  371. return ($this->use & SERVICES_JSON_SUPPRESS_ERRORS)
  372. ? 'null'
  373. : new Services_JSON_Error(gettype($var)." can not be encoded as JSON string");
  374. }
  375. }
  376. /**
  377. * array-walking function for use in generating JSON-formatted name-value pairs
  378. *
  379. * @param string $name name of key to use
  380. * @param mixed $value reference to an array element to be encoded
  381. *
  382. * @return string JSON-formatted name-value pair, like '"name":value'
  383. * @access private
  384. */
  385. function name_value($name, $value)
  386. {
  387. $encoded_value = $this->encode($value);
  388. if(Services_JSON::isError($encoded_value)) {
  389. return $encoded_value;
  390. }
  391. return $this->encode(strval($name)) . ':' . $encoded_value;
  392. }
  393. /**
  394. * reduce a string by removing leading and trailing comments and whitespace
  395. *
  396. * @param $str string string value to strip of comments and whitespace
  397. *
  398. * @return string string value stripped of comments and whitespace
  399. * @access private
  400. */
  401. function reduce_string($str)
  402. {
  403. $str = preg_replace(array(
  404. // eliminate single line comments in '// ...' form
  405. '#^\s*//(.+)$#m',
  406. // eliminate multi-line comments in '/* ... */' form, at start of string
  407. '#^\s*/\*(.+)\*/#Us',
  408. // eliminate multi-line comments in '/* ... */' form, at end of string
  409. '#/\*(.+)\*/\s*$#Us'
  410. ), '', $str);
  411. // eliminate extraneous space
  412. return trim($str);
  413. }
  414. /**
  415. * decodes a JSON string into appropriate variable
  416. *
  417. * @param string $str JSON-formatted string
  418. *
  419. * @return mixed number, boolean, string, array, or object
  420. * corresponding to given JSON input string.
  421. * See argument 1 to Services_JSON() above for object-output behavior.
  422. * Note that decode() always returns strings
  423. * in ASCII or UTF-8 format!
  424. * @access public
  425. */
  426. function decode($str)
  427. {
  428. $str = $this->reduce_string($str);
  429. switch (strtolower($str)) {
  430. case 'true':
  431. return true;
  432. case 'false':
  433. return false;
  434. case 'null':
  435. return null;
  436. default:
  437. $m = array();
  438. if (is_numeric($str)) {
  439. // Lookie-loo, it's a number
  440. // This would work on its own, but I'm trying to be
  441. // good about returning integers where appropriate:
  442. // return (float)$str;
  443. // Return float or int, as appropriate
  444. return ((float)$str == (integer)$str)
  445. ? (integer)$str
  446. : (float)$str;
  447. } elseif (preg_match('/^("|\').*(\1)$/s', $str, $m) && $m[1] == $m[2]) {
  448. // STRINGS RETURNED IN UTF-8 FORMAT
  449. $delim = substr($str, 0, 1);
  450. $chrs = substr($str, 1, -1);
  451. $utf8 = '';
  452. $strlen_chrs = strlen($chrs);
  453. for ($c = 0; $c < $strlen_chrs; ++$c) {
  454. $substr_chrs_c_2 = substr($chrs, $c, 2);
  455. $ord_chrs_c = ord($chrs{$c});
  456. switch (true) {
  457. case $substr_chrs_c_2 == '\b':
  458. $utf8 .= chr(0x08);
  459. ++$c;
  460. break;
  461. case $substr_chrs_c_2 == '\t':
  462. $utf8 .= chr(0x09);
  463. ++$c;
  464. break;
  465. case $substr_chrs_c_2 == '\n':
  466. $utf8 .= chr(0x0A);
  467. ++$c;
  468. break;
  469. case $substr_chrs_c_2 == '\f':
  470. $utf8 .= chr(0x0C);
  471. ++$c;
  472. break;
  473. case $substr_chrs_c_2 == '\r':
  474. $utf8 .= chr(0x0D);
  475. ++$c;
  476. break;
  477. case $substr_chrs_c_2 == '\\"':
  478. case $substr_chrs_c_2 == '\\\'':
  479. case $substr_chrs_c_2 == '\\\\':
  480. case $substr_chrs_c_2 == '\\/':
  481. if (($delim == '"' && $substr_chrs_c_2 != '\\\'') ||
  482. ($delim == "'" && $substr_chrs_c_2 != '\\"')) {
  483. $utf8 .= $chrs{++$c};
  484. }
  485. break;
  486. case preg_match('/\\\u[0-9A-F]{4}/i', substr($chrs, $c, 6)):
  487. // single, escaped unicode character
  488. $utf16 = chr(hexdec(substr($chrs, ($c + 2), 2)))
  489. . chr(hexdec(substr($chrs, ($c + 4), 2)));
  490. $utf8 .= $this->utf162utf8($utf16);
  491. $c += 5;
  492. break;
  493. case ($ord_chrs_c >= 0x20) && ($ord_chrs_c <= 0x7F):
  494. $utf8 .= $chrs{$c};
  495. break;
  496. case ($ord_chrs_c & 0xE0) == 0xC0:
  497. // characters U-00000080 - U-000007FF, mask 110XXXXX
  498. //see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
  499. $utf8 .= substr($chrs, $c, 2);
  500. ++$c;
  501. break;
  502. case ($ord_chrs_c & 0xF0) == 0xE0:
  503. // characters U-00000800 - U-0000FFFF, mask 1110XXXX
  504. // see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
  505. $utf8 .= substr($chrs, $c, 3);
  506. $c += 2;
  507. break;
  508. case ($ord_chrs_c & 0xF8) == 0xF0:
  509. // characters U-00010000 - U-001FFFFF, mask 11110XXX
  510. // see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
  511. $utf8 .= substr($chrs, $c, 4);
  512. $c += 3;
  513. break;
  514. case ($ord_chrs_c & 0xFC) == 0xF8:
  515. // characters U-00200000 - U-03FFFFFF, mask 111110XX
  516. // see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
  517. $utf8 .= substr($chrs, $c, 5);
  518. $c += 4;
  519. break;
  520. case ($ord_chrs_c & 0xFE) == 0xFC:
  521. // characters U-04000000 - U-7FFFFFFF, mask 1111110X
  522. // see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
  523. $utf8 .= substr($chrs, $c, 6);
  524. $c += 5;
  525. break;
  526. }
  527. }
  528. return $utf8;
  529. } elseif (preg_match('/^\[.*\]$/s', $str) || preg_match('/^\{.*\}$/s', $str)) {
  530. // array, or object notation
  531. if ($str{0} == '[') {
  532. $stk = array(SERVICES_JSON_IN_ARR);
  533. $arr = array();
  534. } else {
  535. if ($this->use & SERVICES_JSON_LOOSE_TYPE) {
  536. $stk = array(SERVICES_JSON_IN_OBJ);
  537. $obj = array();
  538. } else {
  539. $stk = array(SERVICES_JSON_IN_OBJ);
  540. $obj = new stdClass();
  541. }
  542. }
  543. array_push($stk, array('what' => SERVICES_JSON_SLICE,
  544. 'where' => 0,
  545. 'delim' => false));
  546. $chrs = substr($str, 1, -1);
  547. $chrs = $this->reduce_string($chrs);
  548. if ($chrs == '') {
  549. if (reset($stk) == SERVICES_JSON_IN_ARR) {
  550. return $arr;
  551. } else {
  552. return $obj;
  553. }
  554. }
  555. //print("\nparsing {$chrs}\n");
  556. $strlen_chrs = strlen($chrs);
  557. for ($c = 0; $c <= $strlen_chrs; ++$c) {
  558. $top = end($stk);
  559. $substr_chrs_c_2 = substr($chrs, $c, 2);
  560. if (($c == $strlen_chrs) || (($chrs{$c} == ',') && ($top['what'] == SERVICES_JSON_SLICE))) {
  561. // found a comma that is not inside a string, array, etc.,
  562. // OR we've reached the end of the character list
  563. $slice = substr($chrs, $top['where'], ($c - $top['where']));
  564. array_push($stk, array('what' => SERVICES_JSON_SLICE, 'where' => ($c + 1), 'delim' => false));
  565. //print("Found split at {$c}: ".substr($chrs, $top['where'], (1 + $c - $top['where']))."\n");
  566. if (reset($stk) == SERVICES_JSON_IN_ARR) {
  567. // we are in an array, so just push an element onto the stack
  568. array_push($arr, $this->decode($slice));
  569. } elseif (reset($stk) == SERVICES_JSON_IN_OBJ) {
  570. // we are in an object, so figure
  571. // out the property name and set an
  572. // element in an associative array,
  573. // for now
  574. $parts = array();
  575. if (preg_match('/^\s*(["\'].*[^\\\]["\'])\s*:\s*(\S.*),?$/Uis', $slice, $parts)) {
  576. // "name":value pair
  577. $key = $this->decode($parts[1]);
  578. $val = $this->decode($parts[2]);
  579. if ($this->use & SERVICES_JSON_LOOSE_TYPE) {
  580. $obj[$key] = $val;
  581. } else {
  582. $obj->$key = $val;
  583. }
  584. } elseif (preg_match('/^\s*(\w+)\s*:\s*(\S.*),?$/Uis', $slice, $parts)) {
  585. // name:value pair, where name is unquoted
  586. $key = $parts[1];
  587. $val = $this->decode($parts[2]);
  588. if ($this->use & SERVICES_JSON_LOOSE_TYPE) {
  589. $obj[$key] = $val;
  590. } else {
  591. $obj->$key = $val;
  592. }
  593. }
  594. }
  595. } elseif ((($chrs{$c} == '"') || ($chrs{$c} == "'")) && ($top['what'] != SERVICES_JSON_IN_STR)) {
  596. // found a quote, and we are not inside a string
  597. array_push($stk, array('what' => SERVICES_JSON_IN_STR, 'where' => $c, 'delim' => $chrs{$c}));
  598. //print("Found start of string at {$c}\n");
  599. } elseif (($chrs{$c} == $top['delim']) &&
  600. ($top['what'] == SERVICES_JSON_IN_STR) &&
  601. ((strlen(substr($chrs, 0, $c)) - strlen(rtrim(substr($chrs, 0, $c), '\\'))) % 2 != 1)) {
  602. // found a quote, we're in a string, and it's not escaped
  603. // we know that it's not escaped becase there is _not_ an
  604. // odd number of backslashes at the end of the string so far
  605. array_pop($stk);
  606. //print("Found end of string at {$c}: ".substr($chrs, $top['where'], (1 + 1 + $c - $top['where']))."\n");
  607. } elseif (($chrs{$c} == '[') &&
  608. in_array($top['what'], array(SERVICES_JSON_SLICE, SERVICES_JSON_IN_ARR, SERVICES_JSON_IN_OBJ))) {
  609. // found a left-bracket, and we are in an array, object, or slice
  610. array_push($stk, array('what' => SERVICES_JSON_IN_ARR, 'where' => $c, 'delim' => false));
  611. //print("Found start of array at {$c}\n");
  612. } elseif (($chrs{$c} == ']') && ($top['what'] == SERVICES_JSON_IN_ARR)) {
  613. // found a right-bracket, and we're in an array
  614. array_pop($stk);
  615. //print("Found end of array at {$c}: ".substr($chrs, $top['where'], (1 + $c - $top['where']))."\n");
  616. } elseif (($chrs{$c} == '{') &&
  617. in_array($top['what'], array(SERVICES_JSON_SLICE, SERVICES_JSON_IN_ARR, SERVICES_JSON_IN_OBJ))) {
  618. // found a left-brace, and we are in an array, object, or slice
  619. array_push($stk, array('what' => SERVICES_JSON_IN_OBJ, 'where' => $c, 'delim' => false));
  620. //print("Found start of object at {$c}\n");
  621. } elseif (($chrs{$c} == '}') && ($top['what'] == SERVICES_JSON_IN_OBJ)) {
  622. // found a right-brace, and we're in an object
  623. array_pop($stk);
  624. //print("Found end of object at {$c}: ".substr($chrs, $top['where'], (1 + $c - $top['where']))."\n");
  625. } elseif (($substr_chrs_c_2 == '/*') &&
  626. in_array($top['what'], array(SERVICES_JSON_SLICE, SERVICES_JSON_IN_ARR, SERVICES_JSON_IN_OBJ))) {
  627. // found a comment start, and we are in an array, object, or slice
  628. array_push($stk, array('what' => SERVICES_JSON_IN_CMT, 'where' => $c, 'delim' => false));
  629. $c++;
  630. //print("Found start of comment at {$c}\n");
  631. } elseif (($substr_chrs_c_2 == '*/') && ($top['what'] == SERVICES_JSON_IN_CMT)) {
  632. // found a comment end, and we're in one now
  633. array_pop($stk);
  634. $c++;
  635. for ($i = $top['where']; $i <= $c; ++$i)
  636. $chrs = substr_replace($chrs, ' ', $i, 1);
  637. //print("Found end of comment at {$c}: ".substr($chrs, $top['where'], (1 + $c - $top['where']))."\n");
  638. }
  639. }
  640. if (reset($stk) == SERVICES_JSON_IN_ARR) {
  641. return $arr;
  642. } elseif (reset($stk) == SERVICES_JSON_IN_OBJ) {
  643. return $obj;
  644. }
  645. }
  646. }
  647. }
  648. /**
  649. * @todo Ultimately, this should just call PEAR::isError()
  650. */
  651. function isError($data, $code = null)
  652. {
  653. if (class_exists('pear')) {
  654. return PEAR::isError($data, $code);
  655. } elseif (is_object($data) && (get_class($data) == 'services_json_error' ||
  656. is_subclass_of($data, 'services_json_error'))) {
  657. return true;
  658. }
  659. return false;
  660. }
  661. }
  662. if (class_exists('PEAR_Error')) {
  663. class Services_JSON_Error extends PEAR_Error
  664. {
  665. function Services_JSON_Error($message = 'unknown error', $code = null,
  666. $mode = null, $options = null, $userinfo = null)
  667. {
  668. parent::PEAR_Error($message, $code, $mode, $options, $userinfo);
  669. }
  670. }
  671. } else {
  672. /**
  673. * @todo Ultimately, this class shall be descended from PEAR_Error
  674. */
  675. class Services_JSON_Error
  676. {
  677. function Services_JSON_Error($message = 'unknown error', $code = null,
  678. $mode = null, $options = null, $userinfo = null)
  679. {
  680. }
  681. }
  682. }
  683. ?>