3 * $Id: JSON.php 40 2007-06-18 11:43:15Z spocke $
5 * @package MCManager.utils
7 * @copyright Copyright © 2007, Moxiecode Systems AB, All rights reserved.
10 define('JSON_BOOL', 1);
11 define('JSON_INT', 2);
12 define('JSON_STR', 3);
13 define('JSON_FLOAT', 4);
14 define('JSON_NULL', 5);
15 define('JSON_START_OBJ', 6);
16 define('JSON_END_OBJ', 7);
17 define('JSON_START_ARRAY', 8);
18 define('JSON_END_ARRAY', 9);
19 define('JSON_KEY', 10);
20 define('JSON_SKIP', 11);
22 define('JSON_IN_ARRAY', 30);
23 define('JSON_IN_OBJECT', 40);
24 define('JSON_IN_BETWEEN', 50);
26 class Moxiecode_JSONReader {
27 var $_data, $_len, $_pos;
29 var $_location, $_lastLocations;
32 function Moxiecode_JSONReader($data) {
34 $this->_len = strlen($data);
36 $this->_location = JSON_IN_BETWEEN;
37 $this->_lastLocations = array();
38 $this->_needProp = false;
45 function getLocation() {
46 return $this->_location;
49 function getTokenName() {
50 switch ($this->_token) {
67 return 'JSON_START_OBJ';
70 return 'JSON_END_OBJ';
72 case JSON_START_ARRAY:
73 return 'JSON_START_ARRAY';
76 return 'JSON_END_ARRAY';
89 function readToken() {
95 $this->_lastLocation[] = $this->_location;
96 $this->_location = JSON_IN_ARRAY;
97 $this->_token = JSON_START_ARRAY;
103 $this->_location = array_pop($this->_lastLocation);
104 $this->_token = JSON_END_ARRAY;
105 $this->_value = null;
108 if ($this->_location == JSON_IN_OBJECT)
109 $this->_needProp = true;
114 $this->_lastLocation[] = $this->_location;
115 $this->_location = JSON_IN_OBJECT;
116 $this->_needProp = true;
117 $this->_token = JSON_START_OBJ;
118 $this->_value = null;
123 $this->_location = array_pop($this->_lastLocation);
124 $this->_token = JSON_END_OBJ;
125 $this->_value = null;
128 if ($this->_location == JSON_IN_OBJECT)
129 $this->_needProp = true;
136 return $this->_readString($chr);
140 return $this->_readNull();
145 return $this->_readBool($chr);
149 if (is_numeric($chr) || $chr == '-' || $chr == '.')
150 return $this->_readNumber($chr);
159 function _readBool($chr) {
160 $this->_token = JSON_BOOL;
161 $this->_value = $chr == 't';
164 $this->skip(3); // rue
166 $this->skip(4); // alse
170 if ($this->_location == JSON_IN_OBJECT && !$this->_needProp)
171 $this->_needProp = true;
176 function _readNull() {
177 $this->_token = JSON_NULL;
178 $this->_value = null;
180 $this->skip(3); // ull
183 if ($this->_location == JSON_IN_OBJECT && !$this->_needProp)
184 $this->_needProp = true;
189 function _readString($quote) {
191 $this->_token = JSON_STR;
194 while (($chr = $this->peek()) != -1) {
201 $chr = $this->read();
224 $output .= $this->_int2utf8(hexdec($this->read(4)));
239 $chr = $this->read();
240 if ($chr != -1 && $chr != $quote)
246 $output .= $this->read();
255 $this->_value = $output;
258 if ($this->_needProp) {
259 $this->_token = JSON_KEY;
260 $this->_needProp = false;
264 if ($this->_location == JSON_IN_OBJECT && !$this->_needProp)
265 $this->_needProp = true;
270 function _int2utf8($int) {
281 return chr(0xC0 | (($int >> 6) & 0x1F)) . chr(0x80 | ($int & 0x3F));
283 case ($int & 0xFFFF):
284 return chr(0xE0 | (($int >> 12) & 0x0F)) . chr(0x80 | (($int >> 6) & 0x3F)) . chr (0x80 | ($int & 0x3F));
286 case ($int & 0x1FFFFF):
287 return chr(0xF0 | ($int >> 18)) . chr(0x80 | (($int >> 12) & 0x3F)) . chr(0x80 | (($int >> 6) & 0x3F)) . chr(0x80 | ($int & 0x3F));
291 function _readNumber($start) {
295 $this->_token = JSON_INT;
298 while (($chr = $this->peek()) != -1) {
299 if (is_numeric($chr) || $chr == '-' || $chr == '.') {
303 $value .= $this->read();
311 $this->_token = JSON_FLOAT;
312 $this->_value = floatval($value);
314 $this->_value = intval($value);
316 if ($this->_location == JSON_IN_OBJECT && !$this->_needProp)
317 $this->_needProp = true;
322 function readAway() {
323 while (($chr = $this->peek()) != null) {
324 if ($chr != ':' && $chr != ',' && $chr != ' ')
331 function read($len = 1) {
332 if ($this->_pos < $this->_len) {
334 $str = substr($this->_data, $this->_pos + 1, $len);
339 return $this->_data[++$this->_pos];
345 function skip($len) {
350 if ($this->_pos < $this->_len)
351 return $this->_data[$this->_pos + 1];
358 * This class handles JSON stuff.
360 * @package MCManager.utils
362 class Moxiecode_JSON {
363 function Moxiecode_JSON() {
366 function decode($input) {
367 $reader = new Moxiecode_JSONReader($input);
369 return $this->readValue($reader);
372 function readValue(&$reader) {
373 $this->data = array();
374 $this->parents = array();
375 $this->cur =& $this->data;
377 $loc = JSON_IN_ARRAY;
379 while ($reader->readToken()) {
380 switch ($reader->getToken()) {
386 switch ($reader->getLocation()) {
388 $this->cur[$key] = $reader->getValue();
392 $this->cur[] = $reader->getValue();
396 return $reader->getValue();
401 $key = $reader->getValue();
405 case JSON_START_ARRAY:
406 if ($loc == JSON_IN_OBJECT)
407 $this->addArray($key);
409 $this->addArray(null);
413 $loc = $reader->getLocation();
418 $loc = $reader->getLocation();
420 if (count($this->parents) > 0) {
421 $this->cur =& $this->parents[count($this->parents) - 1];
422 array_pop($this->parents);
428 return $this->data[0];
431 // This method was needed since PHP is crapy and doesn't have pointers/references
432 function addArray($key) {
433 $this->parents[] =& $this->cur;
437 $this->cur[$key] =& $ar;
444 function getDelim($index, &$reader) {
445 switch ($reader->getLocation()) {
456 function encode($input) {
457 switch (gettype($input)) {
459 return $input ? 'true' : 'false';
466 return (float) $input;
472 return $this->encodeString($input);
475 return $this->_encodeArray($input);
478 return $this->_encodeArray(get_object_vars($input));
484 function encodeString($input) {
485 // Needs to be escaped
486 if (preg_match('/[^a-zA-Z0-9]/', $input)) {
489 for ($i=0; $i<strlen($input); $i++) {
490 switch ($input[$i]) {
524 $byte = ord($input[$i]);
526 if (($byte & 0xE0) == 0xC0) {
527 $char = pack('C*', $byte, ord($input[$i + 1]));
529 $output .= sprintf('\u%04s', bin2hex($this->_utf82utf16($char)));
530 } if (($byte & 0xF0) == 0xE0) {
531 $char = pack('C*', $byte, ord($input[$i + 1]), ord($input[$i + 2]));
533 $output .= sprintf('\u%04s', bin2hex($this->_utf82utf16($char)));
534 } if (($byte & 0xF8) == 0xF0) {
535 $char = pack('C*', $byte, ord($input[$i + 1]), ord($input[$i + 2], ord($input[$i + 3])));
537 $output .= sprintf('\u%04s', bin2hex($this->_utf82utf16($char)));
538 } if (($byte & 0xFC) == 0xF8) {
539 $char = pack('C*', $byte, ord($input[$i + 1]), ord($input[$i + 2], ord($input[$i + 3]), ord($input[$i + 4])));
541 $output .= sprintf('\u%04s', bin2hex($this->_utf82utf16($char)));
542 } if (($byte & 0xFE) == 0xFC) {
543 $char = pack('C*', $byte, ord($input[$i + 1]), ord($input[$i + 2], ord($input[$i + 3]), ord($input[$i + 4]), ord($input[$i + 5])));
545 $output .= sprintf('\u%04s', bin2hex($this->_utf82utf16($char)));
546 } else if ($byte < 128)
547 $output .= $input[$i];
551 return '"' . $output . '"';
554 return '"' . $input . '"';
557 function _utf82utf16($utf8) {
558 if (function_exists('mb_convert_encoding'))
559 return mb_convert_encoding($utf8, 'UTF-16', 'UTF-8');
561 switch (strlen($utf8)) {
566 return chr(0x07 & (ord($utf8[0]) >> 2)) . chr((0xC0 & (ord($utf8[0]) << 6)) | (0x3F & ord($utf8[1])));
569 return chr((0xF0 & (ord($utf8[0]) << 4)) | (0x0F & (ord($utf8[1]) >> 2))) . chr((0xC0 & (ord($utf8[1]) << 6)) | (0x7F & ord($utf8[2])));
575 function _encodeArray($input) {
579 $keys = array_keys($input);
580 for ($i=0; $i<count($keys); $i++) {
581 if (!is_int($keys[$i])) {
582 $output .= $this->encodeString($keys[$i]) . ':' . $this->encode($input[$keys[$i]]);
585 $output .= $this->encode($input[$keys[$i]]);
587 if ($i != count($keys) - 1)
591 return $isIndexed ? '[' . $output . ']' : '{' . $output . '}';