a9d494202b1dde4ff965ee14876eb57453ee9150.svn-base 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724
  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. * @license http://www.opensource.org/licenses/bsd-license.php
  54. * @link http://pear.php.net/pepr/pepr-proposal-show.php?id=198
  55. */
  56. /**
  57. * Marker constant for Services_JSON::decode(), used to flag stack state
  58. */
  59. define('SERVICES_JSON_SLICE', 1);
  60. /**
  61. * Marker constant for Services_JSON::decode(), used to flag stack state
  62. */
  63. define('SERVICES_JSON_IN_STR', 2);
  64. /**
  65. * Marker constant for Services_JSON::decode(), used to flag stack state
  66. */
  67. define('SERVICES_JSON_IN_ARR', 4);
  68. /**
  69. * Marker constant for Services_JSON::decode(), used to flag stack state
  70. */
  71. define('SERVICES_JSON_IN_OBJ', 8);
  72. /**
  73. * Marker constant for Services_JSON::decode(), used to flag stack state
  74. */
  75. define('SERVICES_JSON_IN_CMT', 16);
  76. /**
  77. * Behavior switch for Services_JSON::decode()
  78. */
  79. define('SERVICES_JSON_LOOSE_TYPE', 10);
  80. /**
  81. * Behavior switch for Services_JSON::decode()
  82. */
  83. define('SERVICES_JSON_STRICT_TYPE', 11);
  84. /**
  85. * Converts to and from JSON format.
  86. *
  87. * Brief example of use:
  88. *
  89. * <code>
  90. * // create a new instance of Services_JSON
  91. * $json = new Services_JSON();
  92. *
  93. * // convert a complexe value to JSON notation, and send it to the browser
  94. * $value = array('foo', 'bar', array(1, 2, 'baz'), array(3, array(4)));
  95. * $output = $json->encode($value);
  96. *
  97. * print($output);
  98. * // prints: ["foo","bar",[1,2,"baz"],[3,[4]]]
  99. *
  100. * // accept incoming POST data, assumed to be in JSON notation
  101. * $input = file_get_contents('php://input', 1000000);
  102. * $value = $json->decode($input);
  103. * </code>
  104. */
  105. class Services_JSON
  106. {
  107. /**
  108. * constructs a new JSON instance
  109. *
  110. * @param int $use object behavior: when encoding or decoding,
  111. * be loose or strict about object/array usage
  112. *
  113. * possible values:
  114. * - SERVICES_JSON_STRICT_TYPE: strict typing, default.
  115. * "{...}" syntax creates objects in decode().
  116. * - SERVICES_JSON_LOOSE_TYPE: loose typing.
  117. * "{...}" syntax creates associative arrays in decode().
  118. */
  119. function Services_JSON($use = SERVICES_JSON_STRICT_TYPE)
  120. {
  121. $this->use = $use;
  122. }
  123. /**
  124. * convert a string from one UTF-16 char to one UTF-8 char
  125. *
  126. * Normally should be handled by mb_convert_encoding, but
  127. * provides a slower PHP-only method for installations
  128. * that lack the multibye string extension.
  129. *
  130. * @param string $utf16 UTF-16 character
  131. * @return string UTF-8 character
  132. * @access private
  133. */
  134. function utf162utf8($utf16)
  135. {
  136. // oh please oh please oh please oh please oh please
  137. if(function_exists('mb_convert_encoding'))
  138. return mb_convert_encoding($utf16, 'UTF-8', 'UTF-16');
  139. $bytes = (ord($utf16{0}) << 8) | ord($utf16{1});
  140. switch(true) {
  141. case ((0x7F & $bytes) == $bytes):
  142. // this case should never be reached, because we are in ASCII range
  143. // see: http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
  144. return chr(0x7F & $bytes);
  145. case (0x07FF & $bytes) == $bytes:
  146. // return a 2-byte UTF-8 character
  147. // see: http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
  148. return chr(0xC0 | (($bytes >> 6) & 0x1F))
  149. . chr(0x80 | ($bytes & 0x3F));
  150. case (0xFFFF & $bytes) == $bytes:
  151. // return a 3-byte UTF-8 character
  152. // see: http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
  153. return chr(0xE0 | (($bytes >> 12) & 0x0F))
  154. . chr(0x80 | (($bytes >> 6) & 0x3F))
  155. . chr(0x80 | ($bytes & 0x3F));
  156. }
  157. // ignoring UTF-32 for now, sorry
  158. return '';
  159. }
  160. /**
  161. * convert a string from one UTF-8 char to one UTF-16 char
  162. *
  163. * Normally should be handled by mb_convert_encoding, but
  164. * provides a slower PHP-only method for installations
  165. * that lack the multibye string extension.
  166. *
  167. * @param string $utf8 UTF-8 character
  168. * @return string UTF-16 character
  169. * @access private
  170. */
  171. function utf82utf16($utf8)
  172. {
  173. // oh please oh please oh please oh please oh please
  174. if(function_exists('mb_convert_encoding'))
  175. return mb_convert_encoding($utf8, 'UTF-16', 'UTF-8');
  176. switch(strlen($utf8)) {
  177. case 1:
  178. // this case should never be reached, because we are in ASCII range
  179. // see: http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
  180. return $ut8;
  181. case 2:
  182. // return a UTF-16 character from a 2-byte UTF-8 char
  183. // see: http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
  184. return chr(0x07 & (ord($utf8{0}) >> 2))
  185. . chr((0xC0 & (ord($utf8{0}) << 6))
  186. | (0x3F & ord($utf8{1})));
  187. case 3:
  188. // return a UTF-16 character from a 3-byte UTF-8 char
  189. // see: http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
  190. return chr((0xF0 & (ord($utf8{0}) << 4))
  191. | (0x0F & (ord($utf8{1}) >> 2)))
  192. . chr((0xC0 & (ord($utf8{1}) << 6))
  193. | (0x7F & ord($utf8{2})));
  194. }
  195. // ignoring UTF-32 for now, sorry
  196. return '';
  197. }
  198. /**
  199. * encodes an arbitrary variable into JSON format
  200. *
  201. * @param mixed $var any number, boolean, string, array, or object to be encoded.
  202. * see argument 1 to Services_JSON() above for array-parsing behavior.
  203. * if var is a strng, note that encode() always expects it
  204. * to be in ASCII or UTF-8 format!
  205. *
  206. * @return string JSON string representation of input var
  207. * @access public
  208. */
  209. function encode($var)
  210. {
  211. switch (gettype($var)) {
  212. case 'boolean':
  213. return $var ? 'true' : 'false';
  214. case 'NULL':
  215. return 'null';
  216. case 'integer':
  217. return (int) $var;
  218. case 'double':
  219. case 'float':
  220. return (float) $var;
  221. case 'string':
  222. // STRINGS ARE EXPECTED TO BE IN ASCII OR UTF-8 FORMAT
  223. $ascii = '';
  224. $strlen_var = strlen($var);
  225. /*
  226. * Iterate over every character in the string,
  227. * escaping with a slash or encoding to UTF-8 where necessary
  228. */
  229. for ($c = 0; $c < $strlen_var; ++$c) {
  230. $ord_var_c = ord($var{$c});
  231. switch (true) {
  232. case $ord_var_c == 0x08:
  233. $ascii .= '\b';
  234. break;
  235. case $ord_var_c == 0x09:
  236. $ascii .= '\t';
  237. break;
  238. case $ord_var_c == 0x0A:
  239. $ascii .= '\n';
  240. break;
  241. case $ord_var_c == 0x0C:
  242. $ascii .= '\f';
  243. break;
  244. case $ord_var_c == 0x0D:
  245. $ascii .= '\r';
  246. break;
  247. case $ord_var_c == 0x22:
  248. case $ord_var_c == 0x2F:
  249. case $ord_var_c == 0x5C:
  250. // double quote, slash, slosh
  251. $ascii .= '\\'.$var{$c};
  252. break;
  253. case (($ord_var_c >= 0x20) && ($ord_var_c <= 0x7F)):
  254. // characters U-00000000 - U-0000007F (same as ASCII)
  255. $ascii .= $var{$c};
  256. break;
  257. case (($ord_var_c & 0xE0) == 0xC0):
  258. // characters U-00000080 - U-000007FF, mask 110XXXXX
  259. // see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
  260. $char = pack('C*', $ord_var_c, ord($var{$c + 1}));
  261. $c += 1;
  262. $utf16 = $this->utf82utf16($char);
  263. $ascii .= sprintf('\u%04s', bin2hex($utf16));
  264. break;
  265. case (($ord_var_c & 0xF0) == 0xE0):
  266. // characters U-00000800 - U-0000FFFF, mask 1110XXXX
  267. // see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
  268. $char = pack('C*', $ord_var_c,
  269. ord($var{$c + 1}),
  270. ord($var{$c + 2}));
  271. $c += 2;
  272. $utf16 = $this->utf82utf16($char);
  273. $ascii .= sprintf('\u%04s', bin2hex($utf16));
  274. break;
  275. case (($ord_var_c & 0xF8) == 0xF0):
  276. // characters U-00010000 - U-001FFFFF, mask 11110XXX
  277. // see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
  278. $char = pack('C*', $ord_var_c,
  279. ord($var{$c + 1}),
  280. ord($var{$c + 2}),
  281. ord($var{$c + 3}));
  282. $c += 3;
  283. $utf16 = $this->utf82utf16($char);
  284. $ascii .= sprintf('\u%04s', bin2hex($utf16));
  285. break;
  286. case (($ord_var_c & 0xFC) == 0xF8):
  287. // characters U-00200000 - U-03FFFFFF, mask 111110XX
  288. // see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
  289. $char = pack('C*', $ord_var_c,
  290. ord($var{$c + 1}),
  291. ord($var{$c + 2}),
  292. ord($var{$c + 3}),
  293. ord($var{$c + 4}));
  294. $c += 4;
  295. $utf16 = $this->utf82utf16($char);
  296. $ascii .= sprintf('\u%04s', bin2hex($utf16));
  297. break;
  298. case (($ord_var_c & 0xFE) == 0xFC):
  299. // characters U-04000000 - U-7FFFFFFF, mask 1111110X
  300. // see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
  301. $char = pack('C*', $ord_var_c,
  302. ord($var{$c + 1}),
  303. ord($var{$c + 2}),
  304. ord($var{$c + 3}),
  305. ord($var{$c + 4}),
  306. ord($var{$c + 5}));
  307. $c += 5;
  308. $utf16 = $this->utf82utf16($char);
  309. $ascii .= sprintf('\u%04s', bin2hex($utf16));
  310. break;
  311. }
  312. }
  313. return '"'.$ascii.'"';
  314. case 'array':
  315. /*
  316. * As per JSON spec if any array key is not an integer
  317. * we must treat the the whole array as an object. We
  318. * also try to catch a sparsely populated associative
  319. * array with numeric keys here because some JS engines
  320. * will create an array with empty indexes up to
  321. * max_index which can cause memory issues and because
  322. * the keys, which may be relevant, will be remapped
  323. * otherwise.
  324. *
  325. * As per the ECMA and JSON specification an object may
  326. * have any string as a property. Unfortunately due to
  327. * a hole in the ECMA specification if the key is a
  328. * ECMA reserved word or starts with a digit the
  329. * parameter is only accessible using ECMAScript's
  330. * bracket notation.
  331. */
  332. // treat as a JSON object
  333. if (is_array($var) && count($var) && (array_keys($var) !== range(0, sizeof($var) - 1))) {
  334. return '{' .
  335. join(',', array_map(array($this, 'name_value'),
  336. array_keys($var),
  337. array_values($var)))
  338. . '}';
  339. }
  340. // treat it like a regular array
  341. return '[' . join(',', array_map(array($this, 'encode'), $var)) . ']';
  342. case 'object':
  343. $vars = get_object_vars($var);
  344. return '{' .
  345. join(',', array_map(array($this, 'name_value'),
  346. array_keys($vars),
  347. array_values($vars)))
  348. . '}';
  349. default:
  350. return '';
  351. }
  352. }
  353. /**
  354. * array-walking function for use in generating JSON-formatted name-value pairs
  355. *
  356. * @param string $name name of key to use
  357. * @param mixed $value reference to an array element to be encoded
  358. *
  359. * @return string JSON-formatted name-value pair, like '"name":value'
  360. * @access private
  361. */
  362. function name_value($name, $value)
  363. {
  364. return $this->encode(strval($name)) . ':' . $this->encode($value);
  365. }
  366. /**
  367. * reduce a string by removing leading and trailing comments and whitespace
  368. *
  369. * @param $str string string value to strip of comments and whitespace
  370. *
  371. * @return string string value stripped of comments and whitespace
  372. * @access private
  373. */
  374. function reduce_string($str)
  375. {
  376. $str = preg_replace(array(
  377. // eliminate single line comments in '// ...' form
  378. '#^\s*//(.+)$#m',
  379. // eliminate multi-line comments in '/* ... */' form, at start of string
  380. '#^\s*/\*(.+)\*/#Us',
  381. // eliminate multi-line comments in '/* ... */' form, at end of string
  382. '#/\*(.+)\*/\s*$#Us'
  383. ), '', $str);
  384. // eliminate extraneous space
  385. return trim($str);
  386. }
  387. /**
  388. * decodes a JSON string into appropriate variable
  389. *
  390. * @param string $str JSON-formatted string
  391. *
  392. * @return mixed number, boolean, string, array, or object
  393. * corresponding to given JSON input string.
  394. * See argument 1 to Services_JSON() above for object-output behavior.
  395. * Note that decode() always returns strings
  396. * in ASCII or UTF-8 format!
  397. * @access public
  398. */
  399. function decode($str)
  400. {
  401. $str = $this->reduce_string($str);
  402. switch (strtolower($str)) {
  403. case 'true':
  404. return true;
  405. case 'false':
  406. return false;
  407. case 'null':
  408. return null;
  409. default:
  410. if (is_numeric($str)) {
  411. // Lookie-loo, it's a number
  412. // This would work on its own, but I'm trying to be
  413. // good about returning integers where appropriate:
  414. // return (float)$str;
  415. // Return float or int, as appropriate
  416. return ((float)$str == (integer)$str)
  417. ? (integer)$str
  418. : (float)$str;
  419. } elseif (preg_match('/^("|\').*(\1)$/s', $str, $m) && $m[1] == $m[2]) {
  420. // STRINGS RETURNED IN UTF-8 FORMAT
  421. $delim = substr($str, 0, 1);
  422. $chrs = substr($str, 1, -1);
  423. $utf8 = '';
  424. $strlen_chrs = strlen($chrs);
  425. for ($c = 0; $c < $strlen_chrs; ++$c) {
  426. $substr_chrs_c_2 = substr($chrs, $c, 2);
  427. $ord_chrs_c = ord($chrs{$c});
  428. switch (true) {
  429. case $substr_chrs_c_2 == '\b':
  430. $utf8 .= chr(0x08);
  431. ++$c;
  432. break;
  433. case $substr_chrs_c_2 == '\t':
  434. $utf8 .= chr(0x09);
  435. ++$c;
  436. break;
  437. case $substr_chrs_c_2 == '\n':
  438. $utf8 .= chr(0x0A);
  439. ++$c;
  440. break;
  441. case $substr_chrs_c_2 == '\f':
  442. $utf8 .= chr(0x0C);
  443. ++$c;
  444. break;
  445. case $substr_chrs_c_2 == '\r':
  446. $utf8 .= chr(0x0D);
  447. ++$c;
  448. break;
  449. case $substr_chrs_c_2 == '\\"':
  450. case $substr_chrs_c_2 == '\\\'':
  451. case $substr_chrs_c_2 == '\\\\':
  452. case $substr_chrs_c_2 == '\\/':
  453. if (($delim == '"' && $substr_chrs_c_2 != '\\\'') ||
  454. ($delim == "'" && $substr_chrs_c_2 != '\\"')) {
  455. $utf8 .= $chrs{++$c};
  456. }
  457. break;
  458. case preg_match('/\\\u[0-9A-F]{4}/i', substr($chrs, $c, 6)):
  459. // single, escaped unicode character
  460. $utf16 = chr(hexdec(substr($chrs, ($c + 2), 2)))
  461. . chr(hexdec(substr($chrs, ($c + 4), 2)));
  462. $utf8 .= $this->utf162utf8($utf16);
  463. $c += 5;
  464. break;
  465. case ($ord_chrs_c >= 0x20) && ($ord_chrs_c <= 0x7F):
  466. $utf8 .= $chrs{$c};
  467. break;
  468. case ($ord_chrs_c & 0xE0) == 0xC0:
  469. // characters U-00000080 - U-000007FF, mask 110XXXXX
  470. //see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
  471. $utf8 .= substr($chrs, $c, 2);
  472. ++$c;
  473. break;
  474. case ($ord_chrs_c & 0xF0) == 0xE0:
  475. // characters U-00000800 - U-0000FFFF, mask 1110XXXX
  476. // see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
  477. $utf8 .= substr($chrs, $c, 3);
  478. $c += 2;
  479. break;
  480. case ($ord_chrs_c & 0xF8) == 0xF0:
  481. // characters U-00010000 - U-001FFFFF, mask 11110XXX
  482. // see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
  483. $utf8 .= substr($chrs, $c, 4);
  484. $c += 3;
  485. break;
  486. case ($ord_chrs_c & 0xFC) == 0xF8:
  487. // characters U-00200000 - U-03FFFFFF, mask 111110XX
  488. // see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
  489. $utf8 .= substr($chrs, $c, 5);
  490. $c += 4;
  491. break;
  492. case ($ord_chrs_c & 0xFE) == 0xFC:
  493. // characters U-04000000 - U-7FFFFFFF, mask 1111110X
  494. // see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
  495. $utf8 .= substr($chrs, $c, 6);
  496. $c += 5;
  497. break;
  498. }
  499. }
  500. return $utf8;
  501. } elseif (preg_match('/^\[.*\]$/s', $str) || preg_match('/^\{.*\}$/s', $str)) {
  502. // array, or object notation
  503. if ($str{0} == '[') {
  504. $stk = array(SERVICES_JSON_IN_ARR);
  505. $arr = array();
  506. } else {
  507. if ($this->use == SERVICES_JSON_LOOSE_TYPE) {
  508. $stk = array(SERVICES_JSON_IN_OBJ);
  509. $obj = array();
  510. } else {
  511. $stk = array(SERVICES_JSON_IN_OBJ);
  512. $obj = new stdClass();
  513. }
  514. }
  515. array_push($stk, array('what' => SERVICES_JSON_SLICE,
  516. 'where' => 0,
  517. 'delim' => false));
  518. $chrs = substr($str, 1, -1);
  519. $chrs = $this->reduce_string($chrs);
  520. if ($chrs == '') {
  521. if (reset($stk) == SERVICES_JSON_IN_ARR) {
  522. return $arr;
  523. } else {
  524. return $obj;
  525. }
  526. }
  527. //print("\nparsing {$chrs}\n");
  528. $strlen_chrs = strlen($chrs);
  529. for ($c = 0; $c <= $strlen_chrs; ++$c) {
  530. $top = end($stk);
  531. $substr_chrs_c_2 = substr($chrs, $c, 2);
  532. if (($c == $strlen_chrs) || (($chrs{$c} == ',') && ($top['what'] == SERVICES_JSON_SLICE))) {
  533. // found a comma that is not inside a string, array, etc.,
  534. // OR we've reached the end of the character list
  535. $slice = substr($chrs, $top['where'], ($c - $top['where']));
  536. array_push($stk, array('what' => SERVICES_JSON_SLICE, 'where' => ($c + 1), 'delim' => false));
  537. //print("Found split at {$c}: ".substr($chrs, $top['where'], (1 + $c - $top['where']))."\n");
  538. if (reset($stk) == SERVICES_JSON_IN_ARR) {
  539. // we are in an array, so just push an element onto the stack
  540. array_push($arr, $this->decode($slice));
  541. } elseif (reset($stk) == SERVICES_JSON_IN_OBJ) {
  542. // we are in an object, so figure
  543. // out the property name and set an
  544. // element in an associative array,
  545. // for now
  546. if (preg_match('/^\s*(["\'].*[^\\\]["\'])\s*:\s*(\S.*),?$/Uis', $slice, $parts)) {
  547. // "name":value pair
  548. $key = $this->decode($parts[1]);
  549. $val = $this->decode($parts[2]);
  550. if ($this->use == SERVICES_JSON_LOOSE_TYPE) {
  551. $obj[$key] = $val;
  552. } else {
  553. $obj->$key = $val;
  554. }
  555. } elseif (preg_match('/^\s*(\w+)\s*:\s*(\S.*),?$/Uis', $slice, $parts)) {
  556. // name:value pair, where name is unquoted
  557. $key = $parts[1];
  558. $val = $this->decode($parts[2]);
  559. if ($this->use == SERVICES_JSON_LOOSE_TYPE) {
  560. $obj[$key] = $val;
  561. } else {
  562. $obj->$key = $val;
  563. }
  564. }
  565. }
  566. } elseif ((($chrs{$c} == '"') || ($chrs{$c} == "'")) && ($top['what'] != SERVICES_JSON_IN_STR)) {
  567. // found a quote, and we are not inside a string
  568. array_push($stk, array('what' => SERVICES_JSON_IN_STR, 'where' => $c, 'delim' => $chrs{$c}));
  569. //print("Found start of string at {$c}\n");
  570. } elseif (($chrs{$c} == $top['delim']) &&
  571. ($top['what'] == SERVICES_JSON_IN_STR) &&
  572. (($chrs{$c - 1} != '\\') ||
  573. ($chrs{$c - 1} == '\\' && $chrs{$c - 2} == '\\'))) {
  574. // found a quote, we're in a string, and it's not escaped
  575. array_pop($stk);
  576. //print("Found end of string at {$c}: ".substr($chrs, $top['where'], (1 + 1 + $c - $top['where']))."\n");
  577. } elseif (($chrs{$c} == '[') &&
  578. in_array($top['what'], array(SERVICES_JSON_SLICE, SERVICES_JSON_IN_ARR, SERVICES_JSON_IN_OBJ))) {
  579. // found a left-bracket, and we are in an array, object, or slice
  580. array_push($stk, array('what' => SERVICES_JSON_IN_ARR, 'where' => $c, 'delim' => false));
  581. //print("Found start of array at {$c}\n");
  582. } elseif (($chrs{$c} == ']') && ($top['what'] == SERVICES_JSON_IN_ARR)) {
  583. // found a right-bracket, and we're in an array
  584. array_pop($stk);
  585. //print("Found end of array at {$c}: ".substr($chrs, $top['where'], (1 + $c - $top['where']))."\n");
  586. } elseif (($chrs{$c} == '{') &&
  587. in_array($top['what'], array(SERVICES_JSON_SLICE, SERVICES_JSON_IN_ARR, SERVICES_JSON_IN_OBJ))) {
  588. // found a left-brace, and we are in an array, object, or slice
  589. array_push($stk, array('what' => SERVICES_JSON_IN_OBJ, 'where' => $c, 'delim' => false));
  590. //print("Found start of object at {$c}\n");
  591. } elseif (($chrs{$c} == '}') && ($top['what'] == SERVICES_JSON_IN_OBJ)) {
  592. // found a right-brace, and we're in an object
  593. array_pop($stk);
  594. //print("Found end of object at {$c}: ".substr($chrs, $top['where'], (1 + $c - $top['where']))."\n");
  595. } elseif (($substr_chrs_c_2 == '/*') &&
  596. in_array($top['what'], array(SERVICES_JSON_SLICE, SERVICES_JSON_IN_ARR, SERVICES_JSON_IN_OBJ))) {
  597. // found a comment start, and we are in an array, object, or slice
  598. array_push($stk, array('what' => SERVICES_JSON_IN_CMT, 'where' => $c, 'delim' => false));
  599. $c++;
  600. //print("Found start of comment at {$c}\n");
  601. } elseif (($substr_chrs_c_2 == '*/') && ($top['what'] == SERVICES_JSON_IN_CMT)) {
  602. // found a comment end, and we're in one now
  603. array_pop($stk);
  604. $c++;
  605. for ($i = $top['where']; $i <= $c; ++$i)
  606. $chrs = substr_replace($chrs, ' ', $i, 1);
  607. //print("Found end of comment at {$c}: ".substr($chrs, $top['where'], (1 + $c - $top['where']))."\n");
  608. }
  609. }
  610. if (reset($stk) == SERVICES_JSON_IN_ARR) {
  611. return $arr;
  612. } elseif (reset($stk) == SERVICES_JSON_IN_OBJ) {
  613. return $obj;
  614. }
  615. }
  616. }
  617. }
  618. }
  619. ?>