4
0

JSON.php 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933
  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 305040 2010-11-02 23:19:03Z 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. * Behavior switch for Services_JSON::decode()
  87. */
  88. define('SERVICES_JSON_USE_TO_JSON', 64);
  89. /**
  90. * Converts to and from JSON format.
  91. *
  92. * Brief example of use:
  93. *
  94. * <code>
  95. * // create a new instance of Services_JSON
  96. * $json = new Services_JSON();
  97. *
  98. * // convert a complexe value to JSON notation, and send it to the browser
  99. * $value = array('foo', 'bar', array(1, 2, 'baz'), array(3, array(4)));
  100. * $output = $json->encode($value);
  101. *
  102. * print($output);
  103. * // prints: ["foo","bar",[1,2,"baz"],[3,[4]]]
  104. *
  105. * // accept incoming POST data, assumed to be in JSON notation
  106. * $input = file_get_contents('php://input', 1000000);
  107. * $value = $json->decode($input);
  108. * </code>
  109. */
  110. class Services_JSON
  111. {
  112. /**
  113. * constructs a new JSON instance
  114. *
  115. * @param int $use object behavior flags; combine with boolean-OR
  116. *
  117. * possible values:
  118. * - SERVICES_JSON_LOOSE_TYPE: loose typing.
  119. * "{...}" syntax creates associative arrays
  120. * instead of objects in decode().
  121. * - SERVICES_JSON_SUPPRESS_ERRORS: error suppression.
  122. * Values which can't be encoded (e.g. resources)
  123. * appear as NULL instead of throwing errors.
  124. * By default, a deeply-nested resource will
  125. * bubble up with an error, so all return values
  126. * from encode() should be checked with isError()
  127. * - SERVICES_JSON_USE_TO_JSON: call toJSON when serializing objects
  128. * It serializes the return value from the toJSON call rather
  129. * than the object it'self, toJSON can return associative arrays,
  130. * strings or numbers, if you return an object, make sure it does
  131. * not have a toJSON method, otherwise an error will occur.
  132. */
  133. function Services_JSON($use = 0)
  134. {
  135. $this->use = $use;
  136. $this->_mb_strlen = function_exists('mb_strlen');
  137. $this->_mb_convert_encoding = function_exists('mb_convert_encoding');
  138. $this->_mb_substr = function_exists('mb_substr');
  139. }
  140. // private - cache the mbstring lookup results..
  141. var $_mb_strlen = false;
  142. var $_mb_substr = false;
  143. var $_mb_convert_encoding = false;
  144. /**
  145. * convert a string from one UTF-16 char to one UTF-8 char
  146. *
  147. * Normally should be handled by mb_convert_encoding, but
  148. * provides a slower PHP-only method for installations
  149. * that lack the multibye string extension.
  150. *
  151. * @param string $utf16 UTF-16 character
  152. * @return string UTF-8 character
  153. * @access private
  154. */
  155. function utf162utf8($utf16)
  156. {
  157. // oh please oh please oh please oh please oh please
  158. if($this->_mb_convert_encoding) {
  159. return mb_convert_encoding($utf16, 'UTF-8', 'UTF-16');
  160. }
  161. $bytes = (ord($utf16{0}) << 8) | ord($utf16{1});
  162. switch(true) {
  163. case ((0x7F & $bytes) == $bytes):
  164. // this case should never be reached, because we are in ASCII range
  165. // see: http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
  166. return chr(0x7F & $bytes);
  167. case (0x07FF & $bytes) == $bytes:
  168. // return a 2-byte UTF-8 character
  169. // see: http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
  170. return chr(0xC0 | (($bytes >> 6) & 0x1F))
  171. . chr(0x80 | ($bytes & 0x3F));
  172. case (0xFFFF & $bytes) == $bytes:
  173. // return a 3-byte UTF-8 character
  174. // see: http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
  175. return chr(0xE0 | (($bytes >> 12) & 0x0F))
  176. . chr(0x80 | (($bytes >> 6) & 0x3F))
  177. . chr(0x80 | ($bytes & 0x3F));
  178. }
  179. // ignoring UTF-32 for now, sorry
  180. return '';
  181. }
  182. /**
  183. * convert a string from one UTF-8 char to one UTF-16 char
  184. *
  185. * Normally should be handled by mb_convert_encoding, but
  186. * provides a slower PHP-only method for installations
  187. * that lack the multibye string extension.
  188. *
  189. * @param string $utf8 UTF-8 character
  190. * @return string UTF-16 character
  191. * @access private
  192. */
  193. function utf82utf16($utf8)
  194. {
  195. // oh please oh please oh please oh please oh please
  196. if($this->_mb_convert_encoding) {
  197. return mb_convert_encoding($utf8, 'UTF-16', 'UTF-8');
  198. }
  199. switch($this->strlen8($utf8)) {
  200. case 1:
  201. // this case should never be reached, because we are in ASCII range
  202. // see: http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
  203. return $utf8;
  204. case 2:
  205. // return a UTF-16 character from a 2-byte UTF-8 char
  206. // see: http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
  207. return chr(0x07 & (ord($utf8{0}) >> 2))
  208. . chr((0xC0 & (ord($utf8{0}) << 6))
  209. | (0x3F & ord($utf8{1})));
  210. case 3:
  211. // return a UTF-16 character from a 3-byte UTF-8 char
  212. // see: http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
  213. return chr((0xF0 & (ord($utf8{0}) << 4))
  214. | (0x0F & (ord($utf8{1}) >> 2)))
  215. . chr((0xC0 & (ord($utf8{1}) << 6))
  216. | (0x7F & ord($utf8{2})));
  217. }
  218. // ignoring UTF-32 for now, sorry
  219. return '';
  220. }
  221. /**
  222. * encodes an arbitrary variable into JSON format (and sends JSON Header)
  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 encode($var)
  233. {
  234. header('Content-type: application/json');
  235. return $this->encodeUnsafe($var);
  236. }
  237. /**
  238. * encodes an arbitrary variable into JSON format without JSON Header - warning - may allow XSS!!!!)
  239. *
  240. * @param mixed $var any number, boolean, string, array, or object to be encoded.
  241. * see argument 1 to Services_JSON() above for array-parsing behavior.
  242. * if var is a strng, note that encode() always expects it
  243. * to be in ASCII or UTF-8 format!
  244. *
  245. * @return mixed JSON string representation of input var or an error if a problem occurs
  246. * @access public
  247. */
  248. function encodeUnsafe($var)
  249. {
  250. // see bug #16908 - regarding numeric locale printing
  251. $lc = setlocale(LC_NUMERIC, 0);
  252. setlocale(LC_NUMERIC, 'C');
  253. $ret = $this->_encode($var);
  254. setlocale(LC_NUMERIC, $lc);
  255. return $ret;
  256. }
  257. /**
  258. * PRIVATE CODE that does the work of encodes an arbitrary variable into JSON format
  259. *
  260. * @param mixed $var any number, boolean, string, array, or object to be encoded.
  261. * see argument 1 to Services_JSON() above for array-parsing behavior.
  262. * if var is a strng, note that encode() always expects it
  263. * to be in ASCII or UTF-8 format!
  264. *
  265. * @return mixed JSON string representation of input var or an error if a problem occurs
  266. * @access public
  267. */
  268. function _encode($var)
  269. {
  270. switch (gettype($var)) {
  271. case 'boolean':
  272. return $var ? 'true' : 'false';
  273. case 'NULL':
  274. return 'null';
  275. case 'integer':
  276. return (int) $var;
  277. case 'double':
  278. case 'float':
  279. return (float) $var;
  280. case 'string':
  281. // STRINGS ARE EXPECTED TO BE IN ASCII OR UTF-8 FORMAT
  282. $ascii = '';
  283. $strlen_var = $this->strlen8($var);
  284. /*
  285. * Iterate over every character in the string,
  286. * escaping with a slash or encoding to UTF-8 where necessary
  287. */
  288. for ($c = 0; $c < $strlen_var; ++$c) {
  289. $ord_var_c = ord($var{$c});
  290. switch (true) {
  291. case $ord_var_c == 0x08:
  292. $ascii .= '\b';
  293. break;
  294. case $ord_var_c == 0x09:
  295. $ascii .= '\t';
  296. break;
  297. case $ord_var_c == 0x0A:
  298. $ascii .= '\n';
  299. break;
  300. case $ord_var_c == 0x0C:
  301. $ascii .= '\f';
  302. break;
  303. case $ord_var_c == 0x0D:
  304. $ascii .= '\r';
  305. break;
  306. case $ord_var_c == 0x22:
  307. case $ord_var_c == 0x2F:
  308. case $ord_var_c == 0x5C:
  309. // double quote, slash, slosh
  310. $ascii .= '\\'.$var{$c};
  311. break;
  312. case (($ord_var_c >= 0x20) && ($ord_var_c <= 0x7F)):
  313. // characters U-00000000 - U-0000007F (same as ASCII)
  314. $ascii .= $var{$c};
  315. break;
  316. case (($ord_var_c & 0xE0) == 0xC0):
  317. // characters U-00000080 - U-000007FF, mask 110XXXXX
  318. // see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
  319. if ($c+1 >= $strlen_var) {
  320. $c += 1;
  321. $ascii .= '?';
  322. break;
  323. }
  324. $char = pack('C*', $ord_var_c, ord($var{$c + 1}));
  325. $c += 1;
  326. $utf16 = $this->utf82utf16($char);
  327. $ascii .= sprintf('\u%04s', bin2hex($utf16));
  328. break;
  329. case (($ord_var_c & 0xF0) == 0xE0):
  330. if ($c+2 >= $strlen_var) {
  331. $c += 2;
  332. $ascii .= '?';
  333. break;
  334. }
  335. // characters U-00000800 - U-0000FFFF, mask 1110XXXX
  336. // see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
  337. $char = pack('C*', $ord_var_c,
  338. @ord($var{$c + 1}),
  339. @ord($var{$c + 2}));
  340. $c += 2;
  341. $utf16 = $this->utf82utf16($char);
  342. $ascii .= sprintf('\u%04s', bin2hex($utf16));
  343. break;
  344. case (($ord_var_c & 0xF8) == 0xF0):
  345. if ($c+3 >= $strlen_var) {
  346. $c += 3;
  347. $ascii .= '?';
  348. break;
  349. }
  350. // characters U-00010000 - U-001FFFFF, mask 11110XXX
  351. // see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
  352. $char = pack('C*', $ord_var_c,
  353. ord($var{$c + 1}),
  354. ord($var{$c + 2}),
  355. ord($var{$c + 3}));
  356. $c += 3;
  357. $utf16 = $this->utf82utf16($char);
  358. $ascii .= sprintf('\u%04s', bin2hex($utf16));
  359. break;
  360. case (($ord_var_c & 0xFC) == 0xF8):
  361. // characters U-00200000 - U-03FFFFFF, mask 111110XX
  362. // see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
  363. if ($c+4 >= $strlen_var) {
  364. $c += 4;
  365. $ascii .= '?';
  366. break;
  367. }
  368. $char = pack('C*', $ord_var_c,
  369. ord($var{$c + 1}),
  370. ord($var{$c + 2}),
  371. ord($var{$c + 3}),
  372. ord($var{$c + 4}));
  373. $c += 4;
  374. $utf16 = $this->utf82utf16($char);
  375. $ascii .= sprintf('\u%04s', bin2hex($utf16));
  376. break;
  377. case (($ord_var_c & 0xFE) == 0xFC):
  378. if ($c+5 >= $strlen_var) {
  379. $c += 5;
  380. $ascii .= '?';
  381. break;
  382. }
  383. // characters U-04000000 - U-7FFFFFFF, mask 1111110X
  384. // see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
  385. $char = pack('C*', $ord_var_c,
  386. ord($var{$c + 1}),
  387. ord($var{$c + 2}),
  388. ord($var{$c + 3}),
  389. ord($var{$c + 4}),
  390. ord($var{$c + 5}));
  391. $c += 5;
  392. $utf16 = $this->utf82utf16($char);
  393. $ascii .= sprintf('\u%04s', bin2hex($utf16));
  394. break;
  395. }
  396. }
  397. return '"'.$ascii.'"';
  398. case 'array':
  399. /*
  400. * As per JSON spec if any array key is not an integer
  401. * we must treat the the whole array as an object. We
  402. * also try to catch a sparsely populated associative
  403. * array with numeric keys here because some JS engines
  404. * will create an array with empty indexes up to
  405. * max_index which can cause memory issues and because
  406. * the keys, which may be relevant, will be remapped
  407. * otherwise.
  408. *
  409. * As per the ECMA and JSON specification an object may
  410. * have any string as a property. Unfortunately due to
  411. * a hole in the ECMA specification if the key is a
  412. * ECMA reserved word or starts with a digit the
  413. * parameter is only accessible using ECMAScript's
  414. * bracket notation.
  415. */
  416. // treat as a JSON object
  417. if (is_array($var) && count($var) && (array_keys($var) !== range(0, sizeof($var) - 1))) {
  418. $properties = array_map(array($this, 'name_value'),
  419. array_keys($var),
  420. array_values($var));
  421. foreach($properties as $property) {
  422. if(Services_JSON::isError($property)) {
  423. return $property;
  424. }
  425. }
  426. return '{' . join(',', $properties) . '}';
  427. }
  428. // treat it like a regular array
  429. $elements = array_map(array($this, '_encode'), $var);
  430. foreach($elements as $element) {
  431. if(Services_JSON::isError($element)) {
  432. return $element;
  433. }
  434. }
  435. return '[' . join(',', $elements) . ']';
  436. case 'object':
  437. // support toJSON methods.
  438. if (($this->use & SERVICES_JSON_USE_TO_JSON) && method_exists($var, 'toJSON')) {
  439. // this may end up allowing unlimited recursion
  440. // so we check the return value to make sure it's not got the same method.
  441. $recode = $var->toJSON();
  442. if (method_exists($recode, 'toJSON')) {
  443. return ($this->use & SERVICES_JSON_SUPPRESS_ERRORS)
  444. ? 'null'
  445. : new Services_JSON_Error(class_name($var).
  446. " toJSON returned an object with a toJSON method.");
  447. }
  448. return $this->_encode( $recode );
  449. }
  450. $vars = get_object_vars($var);
  451. $properties = array_map(array($this, 'name_value'),
  452. array_keys($vars),
  453. array_values($vars));
  454. foreach($properties as $property) {
  455. if(Services_JSON::isError($property)) {
  456. return $property;
  457. }
  458. }
  459. return '{' . join(',', $properties) . '}';
  460. default:
  461. return ($this->use & SERVICES_JSON_SUPPRESS_ERRORS)
  462. ? 'null'
  463. : new Services_JSON_Error(gettype($var)." can not be encoded as JSON string");
  464. }
  465. }
  466. /**
  467. * array-walking function for use in generating JSON-formatted name-value pairs
  468. *
  469. * @param string $name name of key to use
  470. * @param mixed $value reference to an array element to be encoded
  471. *
  472. * @return string JSON-formatted name-value pair, like '"name":value'
  473. * @access private
  474. */
  475. function name_value($name, $value)
  476. {
  477. $encoded_value = $this->_encode($value);
  478. if(Services_JSON::isError($encoded_value)) {
  479. return $encoded_value;
  480. }
  481. return $this->_encode(strval($name)) . ':' . $encoded_value;
  482. }
  483. /**
  484. * reduce a string by removing leading and trailing comments and whitespace
  485. *
  486. * @param $str string string value to strip of comments and whitespace
  487. *
  488. * @return string string value stripped of comments and whitespace
  489. * @access private
  490. */
  491. function reduce_string($str)
  492. {
  493. $str = preg_replace(array(
  494. // eliminate single line comments in '// ...' form
  495. '#^\s*//(.+)$#m',
  496. // eliminate multi-line comments in '/* ... */' form, at start of string
  497. '#^\s*/\*(.+)\*/#Us',
  498. // eliminate multi-line comments in '/* ... */' form, at end of string
  499. '#/\*(.+)\*/\s*$#Us'
  500. ), '', $str);
  501. // eliminate extraneous space
  502. return trim($str);
  503. }
  504. /**
  505. * decodes a JSON string into appropriate variable
  506. *
  507. * @param string $str JSON-formatted string
  508. *
  509. * @return mixed number, boolean, string, array, or object
  510. * corresponding to given JSON input string.
  511. * See argument 1 to Services_JSON() above for object-output behavior.
  512. * Note that decode() always returns strings
  513. * in ASCII or UTF-8 format!
  514. * @access public
  515. */
  516. function decode($str)
  517. {
  518. $str = $this->reduce_string($str);
  519. switch (strtolower($str)) {
  520. case 'true':
  521. return true;
  522. case 'false':
  523. return false;
  524. case 'null':
  525. return null;
  526. default:
  527. $m = array();
  528. if (is_numeric($str)) {
  529. // Lookie-loo, it's a number
  530. // This would work on its own, but I'm trying to be
  531. // good about returning integers where appropriate:
  532. // return (float)$str;
  533. // Return float or int, as appropriate
  534. return ((float)$str == (integer)$str)
  535. ? (integer)$str
  536. : (float)$str;
  537. } elseif (preg_match('/^("|\').*(\1)$/s', $str, $m) && $m[1] == $m[2]) {
  538. // STRINGS RETURNED IN UTF-8 FORMAT
  539. $delim = $this->substr8($str, 0, 1);
  540. $chrs = $this->substr8($str, 1, -1);
  541. $utf8 = '';
  542. $strlen_chrs = $this->strlen8($chrs);
  543. for ($c = 0; $c < $strlen_chrs; ++$c) {
  544. $substr_chrs_c_2 = $this->substr8($chrs, $c, 2);
  545. $ord_chrs_c = ord($chrs{$c});
  546. switch (true) {
  547. case $substr_chrs_c_2 == '\b':
  548. $utf8 .= chr(0x08);
  549. ++$c;
  550. break;
  551. case $substr_chrs_c_2 == '\t':
  552. $utf8 .= chr(0x09);
  553. ++$c;
  554. break;
  555. case $substr_chrs_c_2 == '\n':
  556. $utf8 .= chr(0x0A);
  557. ++$c;
  558. break;
  559. case $substr_chrs_c_2 == '\f':
  560. $utf8 .= chr(0x0C);
  561. ++$c;
  562. break;
  563. case $substr_chrs_c_2 == '\r':
  564. $utf8 .= chr(0x0D);
  565. ++$c;
  566. break;
  567. case $substr_chrs_c_2 == '\\"':
  568. case $substr_chrs_c_2 == '\\\'':
  569. case $substr_chrs_c_2 == '\\\\':
  570. case $substr_chrs_c_2 == '\\/':
  571. if (($delim == '"' && $substr_chrs_c_2 != '\\\'') ||
  572. ($delim == "'" && $substr_chrs_c_2 != '\\"')) {
  573. $utf8 .= $chrs{++$c};
  574. }
  575. break;
  576. case preg_match('/\\\u[0-9A-F]{4}/i', $this->substr8($chrs, $c, 6)):
  577. // single, escaped unicode character
  578. $utf16 = chr(hexdec($this->substr8($chrs, ($c + 2), 2)))
  579. . chr(hexdec($this->substr8($chrs, ($c + 4), 2)));
  580. $utf8 .= $this->utf162utf8($utf16);
  581. $c += 5;
  582. break;
  583. case ($ord_chrs_c >= 0x20) && ($ord_chrs_c <= 0x7F):
  584. $utf8 .= $chrs{$c};
  585. break;
  586. case ($ord_chrs_c & 0xE0) == 0xC0:
  587. // characters U-00000080 - U-000007FF, mask 110XXXXX
  588. //see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
  589. $utf8 .= $this->substr8($chrs, $c, 2);
  590. ++$c;
  591. break;
  592. case ($ord_chrs_c & 0xF0) == 0xE0:
  593. // characters U-00000800 - U-0000FFFF, mask 1110XXXX
  594. // see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
  595. $utf8 .= $this->substr8($chrs, $c, 3);
  596. $c += 2;
  597. break;
  598. case ($ord_chrs_c & 0xF8) == 0xF0:
  599. // characters U-00010000 - U-001FFFFF, mask 11110XXX
  600. // see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
  601. $utf8 .= $this->substr8($chrs, $c, 4);
  602. $c += 3;
  603. break;
  604. case ($ord_chrs_c & 0xFC) == 0xF8:
  605. // characters U-00200000 - U-03FFFFFF, mask 111110XX
  606. // see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
  607. $utf8 .= $this->substr8($chrs, $c, 5);
  608. $c += 4;
  609. break;
  610. case ($ord_chrs_c & 0xFE) == 0xFC:
  611. // characters U-04000000 - U-7FFFFFFF, mask 1111110X
  612. // see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
  613. $utf8 .= $this->substr8($chrs, $c, 6);
  614. $c += 5;
  615. break;
  616. }
  617. }
  618. return $utf8;
  619. } elseif (preg_match('/^\[.*\]$/s', $str) || preg_match('/^\{.*\}$/s', $str)) {
  620. // array, or object notation
  621. if ($str{0} == '[') {
  622. $stk = array(SERVICES_JSON_IN_ARR);
  623. $arr = array();
  624. } else {
  625. if ($this->use & SERVICES_JSON_LOOSE_TYPE) {
  626. $stk = array(SERVICES_JSON_IN_OBJ);
  627. $obj = array();
  628. } else {
  629. $stk = array(SERVICES_JSON_IN_OBJ);
  630. $obj = new stdClass();
  631. }
  632. }
  633. array_push($stk, array('what' => SERVICES_JSON_SLICE,
  634. 'where' => 0,
  635. 'delim' => false));
  636. $chrs = $this->substr8($str, 1, -1);
  637. $chrs = $this->reduce_string($chrs);
  638. if ($chrs == '') {
  639. if (reset($stk) == SERVICES_JSON_IN_ARR) {
  640. return $arr;
  641. } else {
  642. return $obj;
  643. }
  644. }
  645. //print("\nparsing {$chrs}\n");
  646. $strlen_chrs = $this->strlen8($chrs);
  647. for ($c = 0; $c <= $strlen_chrs; ++$c) {
  648. $top = end($stk);
  649. $substr_chrs_c_2 = $this->substr8($chrs, $c, 2);
  650. if (($c == $strlen_chrs) || (($chrs{$c} == ',') && ($top['what'] == SERVICES_JSON_SLICE))) {
  651. // found a comma that is not inside a string, array, etc.,
  652. // OR we've reached the end of the character list
  653. $slice = $this->substr8($chrs, $top['where'], ($c - $top['where']));
  654. array_push($stk, array('what' => SERVICES_JSON_SLICE, 'where' => ($c + 1), 'delim' => false));
  655. //print("Found split at {$c}: ".$this->substr8($chrs, $top['where'], (1 + $c - $top['where']))."\n");
  656. if (reset($stk) == SERVICES_JSON_IN_ARR) {
  657. // we are in an array, so just push an element onto the stack
  658. array_push($arr, $this->decode($slice));
  659. } elseif (reset($stk) == SERVICES_JSON_IN_OBJ) {
  660. // we are in an object, so figure
  661. // out the property name and set an
  662. // element in an associative array,
  663. // for now
  664. $parts = array();
  665. if (preg_match('/^\s*(["\'].*[^\\\]["\'])\s*:/Uis', $slice, $parts)) {
  666. // "name":value pair
  667. $key = $this->decode($parts[1]);
  668. $val = $this->decode(trim(substr($slice, strlen($parts[0])), ", \t\n\r\0\x0B"));
  669. if ($this->use & SERVICES_JSON_LOOSE_TYPE) {
  670. $obj[$key] = $val;
  671. } else {
  672. $obj->$key = $val;
  673. }
  674. } elseif (preg_match('/^\s*(\w+)\s*:/Uis', $slice, $parts)) {
  675. // name:value pair, where name is unquoted
  676. $key = $parts[1];
  677. $val = $this->decode(trim(substr($slice, strlen($parts[0])), ", \t\n\r\0\x0B"));
  678. if ($this->use & SERVICES_JSON_LOOSE_TYPE) {
  679. $obj[$key] = $val;
  680. } else {
  681. $obj->$key = $val;
  682. }
  683. }
  684. }
  685. } elseif ((($chrs{$c} == '"') || ($chrs{$c} == "'")) && ($top['what'] != SERVICES_JSON_IN_STR)) {
  686. // found a quote, and we are not inside a string
  687. array_push($stk, array('what' => SERVICES_JSON_IN_STR, 'where' => $c, 'delim' => $chrs{$c}));
  688. //print("Found start of string at {$c}\n");
  689. } elseif (($chrs{$c} == $top['delim']) &&
  690. ($top['what'] == SERVICES_JSON_IN_STR) &&
  691. (($this->strlen8($this->substr8($chrs, 0, $c)) - $this->strlen8(rtrim($this->substr8($chrs, 0, $c), '\\'))) % 2 != 1)) {
  692. // found a quote, we're in a string, and it's not escaped
  693. // we know that it's not escaped becase there is _not_ an
  694. // odd number of backslashes at the end of the string so far
  695. array_pop($stk);
  696. //print("Found end of string at {$c}: ".$this->substr8($chrs, $top['where'], (1 + 1 + $c - $top['where']))."\n");
  697. } elseif (($chrs{$c} == '[') &&
  698. in_array($top['what'], array(SERVICES_JSON_SLICE, SERVICES_JSON_IN_ARR, SERVICES_JSON_IN_OBJ))) {
  699. // found a left-bracket, and we are in an array, object, or slice
  700. array_push($stk, array('what' => SERVICES_JSON_IN_ARR, 'where' => $c, 'delim' => false));
  701. //print("Found start of array at {$c}\n");
  702. } elseif (($chrs{$c} == ']') && ($top['what'] == SERVICES_JSON_IN_ARR)) {
  703. // found a right-bracket, and we're in an array
  704. array_pop($stk);
  705. //print("Found end of array at {$c}: ".$this->substr8($chrs, $top['where'], (1 + $c - $top['where']))."\n");
  706. } elseif (($chrs{$c} == '{') &&
  707. in_array($top['what'], array(SERVICES_JSON_SLICE, SERVICES_JSON_IN_ARR, SERVICES_JSON_IN_OBJ))) {
  708. // found a left-brace, and we are in an array, object, or slice
  709. array_push($stk, array('what' => SERVICES_JSON_IN_OBJ, 'where' => $c, 'delim' => false));
  710. //print("Found start of object at {$c}\n");
  711. } elseif (($chrs{$c} == '}') && ($top['what'] == SERVICES_JSON_IN_OBJ)) {
  712. // found a right-brace, and we're in an object
  713. array_pop($stk);
  714. //print("Found end of object at {$c}: ".$this->substr8($chrs, $top['where'], (1 + $c - $top['where']))."\n");
  715. } elseif (($substr_chrs_c_2 == '/*') &&
  716. in_array($top['what'], array(SERVICES_JSON_SLICE, SERVICES_JSON_IN_ARR, SERVICES_JSON_IN_OBJ))) {
  717. // found a comment start, and we are in an array, object, or slice
  718. array_push($stk, array('what' => SERVICES_JSON_IN_CMT, 'where' => $c, 'delim' => false));
  719. $c++;
  720. //print("Found start of comment at {$c}\n");
  721. } elseif (($substr_chrs_c_2 == '*/') && ($top['what'] == SERVICES_JSON_IN_CMT)) {
  722. // found a comment end, and we're in one now
  723. array_pop($stk);
  724. $c++;
  725. for ($i = $top['where']; $i <= $c; ++$i)
  726. $chrs = substr_replace($chrs, ' ', $i, 1);
  727. //print("Found end of comment at {$c}: ".$this->substr8($chrs, $top['where'], (1 + $c - $top['where']))."\n");
  728. }
  729. }
  730. if (reset($stk) == SERVICES_JSON_IN_ARR) {
  731. return $arr;
  732. } elseif (reset($stk) == SERVICES_JSON_IN_OBJ) {
  733. return $obj;
  734. }
  735. }
  736. }
  737. }
  738. /**
  739. * @todo Ultimately, this should just call PEAR::isError()
  740. */
  741. function isError($data, $code = null)
  742. {
  743. if (class_exists('pear')) {
  744. return PEAR::isError($data, $code);
  745. } elseif (is_object($data) && (get_class($data) == 'services_json_error' ||
  746. is_subclass_of($data, 'services_json_error'))) {
  747. return true;
  748. }
  749. return false;
  750. }
  751. /**
  752. * Calculates length of string in bytes
  753. * @param string
  754. * @return integer length
  755. */
  756. function strlen8( $str )
  757. {
  758. if ( $this->_mb_strlen ) {
  759. return mb_strlen( $str, "8bit" );
  760. }
  761. return strlen( $str );
  762. }
  763. /**
  764. * Returns part of a string, interpreting $start and $length as number of bytes.
  765. * @param string
  766. * @param integer start
  767. * @param integer length
  768. * @return integer length
  769. */
  770. function substr8( $string, $start, $length=false )
  771. {
  772. if ( $length === false ) {
  773. $length = $this->strlen8( $string ) - $start;
  774. }
  775. if ( $this->_mb_substr ) {
  776. return mb_substr( $string, $start, $length, "8bit" );
  777. }
  778. return substr( $string, $start, $length );
  779. }
  780. }
  781. if (class_exists('PEAR_Error')) {
  782. class Services_JSON_Error extends PEAR_Error
  783. {
  784. function Services_JSON_Error($message = 'unknown error', $code = null,
  785. $mode = null, $options = null, $userinfo = null)
  786. {
  787. parent::PEAR_Error($message, $code, $mode, $options, $userinfo);
  788. }
  789. }
  790. } else {
  791. /**
  792. * @todo Ultimately, this class shall be descended from PEAR_Error
  793. */
  794. class Services_JSON_Error
  795. {
  796. function Services_JSON_Error($message = 'unknown error', $code = null,
  797. $mode = null, $options = null, $userinfo = null)
  798. {
  799. }
  800. }
  801. }