[ Index ]
 

Code source de DokuWiki 2006-11-06

Accédez au Source d'autres logiciels libresSoutenez Angelica Josefina !

title

Body

[fermer]

/inc/ -> JSON.php (source)

   1  <?php
   2  /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
   3  
   4  /**
   5   * Converts to and from JSON format.
   6   *
   7   * JSON (JavaScript Object Notation) is a lightweight data-interchange
   8   * format. It is easy for humans to read and write. It is easy for machines
   9   * to parse and generate. It is based on a subset of the JavaScript
  10   * Programming Language, Standard ECMA-262 3rd Edition - December 1999.
  11   * This feature can also be found in  Python. JSON is a text format that is
  12   * completely language independent but uses conventions that are familiar
  13   * to programmers of the C-family of languages, including C, C++, C#, Java,
  14   * JavaScript, Perl, TCL, and many others. These properties make JSON an
  15   * ideal data-interchange language.
  16   *
  17   * This package provides a simple encoder and decoder for JSON notation. It
  18   * is intended for use with client-side Javascript applications that make
  19   * use of HTTPRequest to perform server communication functions - data can
  20   * be encoded into JSON notation for use in a client-side javascript, or
  21   * decoded from incoming Javascript requests. JSON format is native to
  22   * Javascript, and can be directly eval()'ed with no further parsing
  23   * overhead
  24   *
  25   * All strings should be in ASCII or UTF-8 format!
  26   *
  27   * PHP versions 4 and 5
  28   *
  29   * LICENSE: Redistribution and use in source and binary forms, with or
  30   * without modification, are permitted provided that the following
  31   * conditions are met: Redistributions of source code must retain the
  32   * above copyright notice, this list of conditions and the following
  33   * disclaimer. Redistributions in binary form must reproduce the above
  34   * copyright notice, this list of conditions and the following disclaimer
  35   * in the documentation and/or other materials provided with the
  36   * distribution.
  37   *
  38   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
  39   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  40   * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
  41   * NO EVENT SHALL CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  42   * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  43   * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  44   * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  45   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
  46   * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
  47   * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  48   * DAMAGE.
  49   *
  50   * @category
  51   * @package
  52   * @author      Michal Migurski <mike-json@teczno.com>
  53   * @author      Matt Knapp <mdknapp[at]gmail[dot]com>
  54   * @author      Brett Stimmerman <brettstimmerman[at]gmail[dot]com>
  55   * @copyright   2005 Michal Migurski
  56   * @license     http://www.freebsd.org/copyright/freebsd-license.html
  57   * @link        http://pear.php.net/pepr/pepr-proposal-show.php?id=198
  58   */
  59  
  60  // for DokuWiki
  61  if(!defined('DOKU_INC')) define('DOKU_INC',realpath(dirname(__FILE__).'/../').'/');
  62  require_once (DOKU_INC.'inc/utf8.php');
  63  
  64  /**
  65   * Marker constant for JSON::decode(), used to flag stack state
  66   */
  67  define('JSON_SLICE',   1);
  68  
  69  /**
  70   * Marker constant for JSON::decode(), used to flag stack state
  71   */
  72  define('JSON_IN_STR',  2);
  73  
  74  /**
  75   * Marker constant for JSON::decode(), used to flag stack state
  76   */
  77  define('JSON_IN_ARR',  4);
  78  
  79  /**
  80   * Marker constant for JSON::decode(), used to flag stack state
  81   */
  82  define('JSON_IN_OBJ',  8);
  83  
  84  /**
  85   * Marker constant for JSON::decode(), used to flag stack state
  86   */
  87  define('JSON_IN_CMT', 16);
  88  
  89  /**
  90   * Behavior switch for JSON::decode()
  91   */
  92  define('JSON_LOOSE_TYPE', 10);
  93  
  94  /**
  95   * Behavior switch for JSON::decode()
  96   */
  97  define('JSON_STRICT_TYPE', 11);
  98  
  99  /**
 100   * Converts to and from JSON format.
 101   *
 102   * @category
 103   * @package
 104   * @author     Michal Migurski <mike-json@teczno.com>
 105   * @author     Matt Knapp <mdknapp[at]gmail[dot]com>
 106   * @author     Brett Stimmerman <brettstimmerman[at]gmail[dot]com>
 107   * @copyright  2005 Michal Migurski
 108   * @license    http://www.php.net/license/3_0.txt  PHP License 3.0
 109   * @version
 110   * @link
 111   * @see
 112   * @since
 113   * @deprecated
 114   */
 115  class JSON
 116  {
 117     /**
 118      * constructs a new JSON instance
 119      *
 120      * @param    int     $use    object behavior: when encoding or decoding,
 121      *                           be loose or strict about object/array usage
 122      *
 123      *                           possible values:
 124      *                              JSON_STRICT_TYPE - strict typing, default
 125      *                                                 "{...}" syntax creates objects in decode.
 126      *                               JSON_LOOSE_TYPE - loose typing
 127      *                                                 "{...}" syntax creates associative arrays in decode.
 128      */
 129      function JSON($use=JSON_STRICT_TYPE)
 130      {
 131          $this->use = $use;
 132      }
 133  
 134     /**
 135      * encodes an arbitrary variable into JSON format
 136      *
 137      * @param    mixed   $var    any number, boolean, string, array, or object to be encoded.
 138      *                           see argument 1 to JSON() above for array-parsing behavior.
 139      *                           if var is a strng, note that encode() always expects it
 140      *                           to be in ASCII or UTF-8 format!
 141      *
 142      * @return   string  JSON string representation of input var
 143      * @access   public
 144      */
 145      function encode($var)
 146      {
 147          switch (gettype($var)) {
 148              case 'boolean':
 149                  return $var ? 'true' : 'false';
 150  
 151              case 'NULL':
 152                  return 'null';
 153  
 154              case 'integer':
 155                  return sprintf('%d', $var);
 156  
 157              case 'double':
 158              case 'float':
 159                  return sprintf('%f', $var);
 160  
 161              case 'string':
 162                  // STRINGS ARE EXPECTED TO BE IN ASCII OR UTF-8 FORMAT
 163                  $ascii = '';
 164                  $strlen_var = strlen($var);
 165  
 166                 /*
 167                  * Iterate over every character in the string,
 168                  * escaping with a slash or encoding to UTF-8 where necessary
 169                  */
 170                  for ($c = 0; $c < $strlen_var; ++$c) {
 171  
 172                      $ord_var_c = ord($var{$c});
 173  
 174                      switch ($ord_var_c) {
 175                          case 0x08:  $ascii .= '\b';  break;
 176                          case 0x09:  $ascii .= '\t';  break;
 177                          case 0x0A:  $ascii .= '\n';  break;
 178                          case 0x0C:  $ascii .= '\f';  break;
 179                          case 0x0D:  $ascii .= '\r';  break;
 180  
 181                          case 0x22:
 182                          case 0x2F:
 183                          case 0x5C:
 184                              // double quote, slash, slosh
 185                              $ascii .= '\\'.$var{$c};
 186                              break;
 187  
 188                          case (($ord_var_c >= 0x20) && ($ord_var_c <= 0x7F)):
 189                              // characters U-00000000 - U-0000007F (same as ASCII)
 190                              $ascii .= $var{$c};
 191                              break;
 192  
 193                          case (($ord_var_c & 0xE0) == 0xC0):
 194                              // characters U-00000080 - U-000007FF, mask 110XXXXX
 195                              // see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
 196                              $char = pack('C*', $ord_var_c, ord($var{$c+1}));
 197                              $c+=1;
 198                              //$utf16 = mb_convert_encoding($char, 'UTF-16', 'UTF-8');
 199                              $utf16 = utf8_to_utf16be($char);
 200                              $ascii .= sprintf('\u%04s', bin2hex($utf16));
 201                              break;
 202  
 203                          case (($ord_var_c & 0xF0) == 0xE0):
 204                              // characters U-00000800 - U-0000FFFF, mask 1110XXXX
 205                              // see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
 206                              $char = pack('C*', $ord_var_c,
 207                                           ord($var{$c+1}),
 208                                           ord($var{$c+2}));
 209                              $c+=2;
 210                              //$utf16 = mb_convert_encoding($char, 'UTF-16', 'UTF-8');
 211                              $utf16 = utf8_to_utf16be($char);
 212                              $ascii .= sprintf('\u%04s', bin2hex($utf16));
 213                              break;
 214  
 215                          case (($ord_var_c & 0xF8) == 0xF0):
 216                              // characters U-00010000 - U-001FFFFF, mask 11110XXX
 217                              // see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
 218                              $char = pack('C*', $ord_var_c,
 219                                           ord($var{$c+1}),
 220                                           ord($var{$c+2}),
 221                                           ord($var{$c+3}));
 222                              $c+=3;
 223                              //$utf16 = mb_convert_encoding($char, 'UTF-16', 'UTF-8');
 224                              $utf16 = utf8_to_utf16be($char);
 225                              $ascii .= sprintf('\u%04s', bin2hex($utf16));
 226                              break;
 227  
 228                          case (($ord_var_c & 0xFC) == 0xF8):
 229                              // characters U-00200000 - U-03FFFFFF, mask 111110XX
 230                              // see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
 231                              $char = pack('C*', $ord_var_c,
 232                                           ord($var{$c+1}),
 233                                           ord($var{$c+2}),
 234                                           ord($var{$c+3}),
 235                                           ord($var{$c+4}));
 236                              $c+=4;
 237                              //$utf16 = mb_convert_encoding($char, 'UTF-16', 'UTF-8');
 238                              $utf16 = utf8_to_utf16be($char);
 239                              $ascii .= sprintf('\u%04s', bin2hex($utf16));
 240                              break;
 241  
 242                          case (($ord_var_c & 0xFE) == 0xFC):
 243                              // characters U-04000000 - U-7FFFFFFF, mask 1111110X
 244                              // see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
 245                              $char = pack('C*', $ord_var_c,
 246                                           ord($var{$c+1}),
 247                                           ord($var{$c+2}),
 248                                           ord($var{$c+3}),
 249                                           ord($var{$c+4}),
 250                                           ord($var{$c+5}));
 251                              $c+=5;
 252                              //$utf16 = mb_convert_encoding($char, 'UTF-16', 'UTF-8');
 253                              $utf16 = utf8_to_utf16be($char);
 254                              $ascii .= sprintf('\u%04s', bin2hex($utf16));
 255                              break;
 256                      }
 257                  }
 258  
 259                  return '"'.$ascii.'"';
 260  
 261              case 'array':
 262                 /*
 263                  * As per JSON spec if any array key is not an integer
 264                  * we must treat the the whole array as an object. We
 265                  * also try to catch a sparsely populated associative
 266                  * array with numeric keys here because some JS engines
 267                  * will create an array with empty indexes up to
 268                  * max_index which can cause memory issues and because
 269                  * the keys, which may be relevant, will be remapped
 270                  * otherwise.
 271                  *
 272                  * As per the ECMA and JSON specification an object may
 273                  * have any string as a property. Unfortunately due to
 274                  * a hole in the ECMA specification if the key is a
 275                  * ECMA reserved word or starts with a digit the
 276                  * parameter is only accessible using ECMAScript's
 277                  * bracket notation.
 278                  */
 279  
 280                  // treat as a JSON object
 281                  if (is_array($var) && count($var) && (array_keys($var) !== range(0, sizeof($var) - 1))) {
 282                      return sprintf('{%s}', join(',', array_map(array($this, 'name_value'),
 283                                                                 array_keys($var),
 284                                                                 array_values($var))));
 285                  }
 286  
 287                  // treat it like a regular array
 288                  return sprintf('[%s]', join(',', array_map(array($this, 'encode'), $var)));
 289  
 290              case 'object':
 291                  $vars = get_object_vars($var);
 292                  return sprintf('{%s}', join(',', array_map(array($this, 'name_value'),
 293                                                             array_keys($vars),
 294                                                             array_values($vars))));
 295  
 296              default:
 297                  return '';
 298          }
 299      }
 300  
 301     /**
 302      * encodes an arbitrary variable into JSON format, alias for encode()
 303      */
 304      function enc($var)
 305      {
 306          return $this->encode($var);
 307      }
 308  
 309     /** function name_value
 310      * array-walking function for use in generating JSON-formatted name-value pairs
 311      *
 312      * @param    string  $name   name of key to use
 313      * @param    mixed   $value  reference to an array element to be encoded
 314      *
 315      * @return   string  JSON-formatted name-value pair, like '"name":value'
 316      * @access   private
 317      */
 318      function name_value($name, $value)
 319      {
 320          return (sprintf("%s:%s", $this->encode(strval($name)), $this->encode($value)));
 321      }
 322  
 323     /**
 324      * reduce a string by removing leading and trailing comments and whitespace
 325      *
 326      * @param    $str    string      string value to strip of comments and whitespace
 327      *
 328      * @return   string  string value stripped of comments and whitespace
 329      * @access   private
 330      */
 331      function reduce_string($str)
 332      {
 333          $str = preg_replace(array(
 334  
 335                  // eliminate single line comments in '// ...' form
 336                  '#^\s*//(.+)$#m',
 337  
 338                  // eliminate multi-line comments in '/* ... */' form, at start of string
 339                  '#^\s*/\*(.+)\*/#Us',
 340  
 341                  // eliminate multi-line comments in '/* ... */' form, at end of string
 342                  '#/\*(.+)\*/\s*$#Us'
 343  
 344              ), '', $str);
 345  
 346          // eliminate extraneous space
 347          return trim($str);
 348      }
 349  
 350     /**
 351      * decodes a JSON string into appropriate variable
 352      *
 353      * @param    string  $str    JSON-formatted string
 354      *
 355      * @return   mixed   number, boolean, string, array, or object
 356      *                   corresponding to given JSON input string.
 357      *                   See argument 1 to JSON() above for object-output behavior.
 358      *                   Note that decode() always returns strings
 359      *                   in ASCII or UTF-8 format!
 360      * @access   public
 361      */
 362      function decode($str)
 363      {
 364          $str = $this->reduce_string($str);
 365  
 366          switch (strtolower($str)) {
 367              case 'true':
 368                  return true;
 369  
 370              case 'false':
 371                  return false;
 372  
 373              case 'null':
 374                  return null;
 375  
 376              default:
 377                  if (is_numeric($str)) {
 378                      // Lookie-loo, it's a number
 379  
 380                      // This would work on its own, but I'm trying to be
 381                      // good about returning integers where appropriate:
 382                      // return (float)$str;
 383  
 384                      // Return float or int, as appropriate
 385                      return ((float)$str == (integer)$str)
 386                          ? (integer)$str
 387                          : (float)$str;
 388  
 389                  } elseif (preg_match('/^("|\').+("|\')$/s', $str, $m) && $m[1] == $m[2]) {
 390                      // STRINGS RETURNED IN UTF-8 FORMAT
 391                      $delim = substr($str, 0, 1);
 392                      $chrs = substr($str, 1, -1);
 393                      $utf8 = '';
 394                      $strlen_chrs = strlen($chrs);
 395  
 396                      for ($c = 0; $c < $strlen_chrs; ++$c) {
 397  
 398                          $substr_chrs_c_2 = substr($chrs, $c, 2);
 399                          $ord_chrs_c = ord($chrs{$c});
 400  
 401                          switch ($substr_chrs_c_2) {
 402                              case '\b':  $utf8 .= chr(0x08);  $c+=1;  break;
 403                              case '\t':  $utf8 .= chr(0x09);  $c+=1;  break;
 404                              case '\n':  $utf8 .= chr(0x0A);  $c+=1;  break;
 405                              case '\f':  $utf8 .= chr(0x0C);  $c+=1;  break;
 406                              case '\r':  $utf8 .= chr(0x0D);  $c+=1;  break;
 407  
 408                              case '\\"':
 409                              case '\\\'':
 410                              case '\\\\':
 411                              case '\\/':
 412                                  if (($delim == '"' && $substr_chrs_c_2 != '\\\'') ||
 413                                     ($delim == "'" && $substr_chrs_c_2 != '\\"')) {
 414                                      $utf8 .= $chrs{++$c};
 415                                  }
 416                                  break;
 417  
 418                              default:
 419                                  if (preg_match('/\\\u[0-9A-F]{4}/i', substr($chrs, $c, 6))) {
 420                                      // single, escaped unicode character
 421                                      $utf16 = chr(hexdec(substr($chrs, ($c+2), 2)))
 422                                             . chr(hexdec(substr($chrs, ($c+4), 2)));
 423                                      //$utf8 .= mb_convert_encoding($utf16, 'UTF-8', 'UTF-16');
 424                                      $utf8 .= utf16be_to_utf8($utf16);
 425                                      $c+=5;
 426  
 427                                  } elseif(($ord_chrs_c >= 0x20) && ($ord_chrs_c <= 0x7F)) {
 428                                      $utf8 .= $chrs{$c};
 429  
 430                                  } elseif(($ord_chrs_c & 0xE0) == 0xC0) {
 431                                      // characters U-00000080 - U-000007FF, mask 110XXXXX
 432                                      //see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
 433                                      $utf8 .= substr($chrs, $c, 2); $c += 1;
 434  
 435                                  } elseif(($ord_chrs_c & 0xF0) == 0xE0) {
 436                                      // characters U-00000800 - U-0000FFFF, mask 1110XXXX
 437                                      // see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
 438                                      $utf8 .= substr($chrs, $c, 3); $c += 2;
 439  
 440                                  } elseif(($ord_chrs_c & 0xF8) == 0xF0) {
 441                                      // characters U-00010000 - U-001FFFFF, mask 11110XXX
 442                                      // see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
 443                                      $utf8 .= substr($chrs, $c, 4); $c += 3;
 444  
 445                                  } elseif(($ord_chrs_c & 0xFC) == 0xF8) {
 446                                      // characters U-00200000 - U-03FFFFFF, mask 111110XX
 447                                      // see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
 448                                      $utf8 .= substr($chrs, $c, 5); $c += 4;
 449  
 450                                  } elseif(($ord_chrs_c & 0xFE) == 0xFC) {
 451                                      // characters U-04000000 - U-7FFFFFFF, mask 1111110X
 452                                      // see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
 453                                      $utf8 .= substr($chrs, $c, 6); $c += 5;
 454  
 455                                  }
 456                                  break;
 457  
 458                          }
 459  
 460                      }
 461  
 462                      return $utf8;
 463  
 464                  } elseif (preg_match('/^\[.*\]$/s', $str) || preg_match('/^\{.*\}$/s', $str)) {
 465                      // array, or object notation
 466  
 467                      if ($str{0} == '[') {
 468                          $stk = array(JSON_IN_ARR);
 469                          $arr = array();
 470                      } else {
 471                          if ($this->use == JSON_LOOSE_TYPE) {
 472                              $stk = array(JSON_IN_OBJ);
 473                              $obj = array();
 474                          } else {
 475                              $stk = array(JSON_IN_OBJ);
 476                              $obj = new stdClass();
 477                          }
 478                      }
 479  
 480                      array_push($stk, array('what'  => JSON_SLICE,
 481                                             'where' => 0,
 482                                             'delim' => false));
 483  
 484                      $chrs = substr($str, 1, -1);
 485                      $chrs = $this->reduce_string($chrs);
 486  
 487                      if ($chrs == '') {
 488                          if (reset($stk) == JSON_IN_ARR) {
 489                              return $arr;
 490  
 491                          } else {
 492                              return $obj;
 493  
 494                          }
 495                      }
 496  
 497                      //print("\nparsing {$chrs}\n");
 498  
 499                      $strlen_chrs = strlen($chrs);
 500  
 501                      for ($c = 0; $c <= $strlen_chrs; ++$c) {
 502  
 503                          $top = end($stk);
 504                          $substr_chrs_c_2 = substr($chrs, $c, 2);
 505  
 506                          if (($c == $strlen_chrs) || (($chrs{$c} == ',') && ($top['what'] == JSON_SLICE))) {
 507                              // found a comma that is not inside a string, array, etc.,
 508                              // OR we've reached the end of the character list
 509                              $slice = substr($chrs, $top['where'], ($c - $top['where']));
 510                              array_push($stk, array('what' => JSON_SLICE, 'where' => ($c + 1), 'delim' => false));
 511                              //print("Found split at {$c}: ".substr($chrs, $top['where'], (1 + $c - $top['where']))."\n");
 512  
 513                              if (reset($stk) == JSON_IN_ARR) {
 514                                  // we are in an array, so just push an element onto the stack
 515                                  array_push($arr, $this->decode($slice));
 516  
 517                              } elseif (reset($stk) == JSON_IN_OBJ) {
 518                                  // we are in an object, so figure
 519                                  // out the property name and set an
 520                                  // element in an associative array,
 521                                  // for now
 522                                  if (preg_match('/^\s*(["\'].*[^\\\]["\'])\s*:\s*(\S.*),?$/Uis', $slice, $parts)) {
 523                                      // "name":value pair
 524                                      $key = $this->decode($parts[1]);
 525                                      $val = $this->decode($parts[2]);
 526  
 527                                      if ($this->use == JSON_LOOSE_TYPE) {
 528                                          $obj[$key] = $val;
 529                                      } else {
 530                                          $obj->$key = $val;
 531                                      }
 532                                  } elseif (preg_match('/^\s*(\w+)\s*:\s*(\S.*),?$/Uis', $slice, $parts)) {
 533                                      // name:value pair, where name is unquoted
 534                                      $key = $parts[1];
 535                                      $val = $this->decode($parts[2]);
 536  
 537                                      if ($this->use == JSON_LOOSE_TYPE) {
 538                                          $obj[$key] = $val;
 539                                      } else {
 540                                          $obj->$key = $val;
 541                                      }
 542                                  }
 543  
 544                              }
 545  
 546                          } elseif ((($chrs{$c} == '"') || ($chrs{$c} == "'")) &&
 547                                   in_array($top['what'], array(JSON_SLICE, JSON_IN_ARR, JSON_IN_OBJ))) {
 548                              // found a quote, and we are not inside a string
 549                              array_push($stk, array('what' => JSON_IN_STR, 'where' => $c, 'delim' => $chrs{$c}));
 550                              //print("Found start of string at {$c}\n");
 551  
 552                          } elseif (($chrs{$c} == $top['delim']) &&
 553                                   ($top['what'] == JSON_IN_STR) &&
 554                                   (($chrs{$c - 1} != "\\") ||
 555                                   ($chrs{$c - 1} == "\\" && $chrs{$c - 2} == "\\"))) {
 556                              // found a quote, we're in a string, and it's not escaped
 557                              array_pop($stk);
 558                              //print("Found end of string at {$c}: ".substr($chrs, $top['where'], (1 + 1 + $c - $top['where']))."\n");
 559  
 560                          } elseif (($chrs{$c} == '[') &&
 561                                   in_array($top['what'], array(JSON_SLICE, JSON_IN_ARR, JSON_IN_OBJ))) {
 562                              // found a left-bracket, and we are in an array, object, or slice
 563                              array_push($stk, array('what' => JSON_IN_ARR, 'where' => $c, 'delim' => false));
 564                              //print("Found start of array at {$c}\n");
 565  
 566                          } elseif (($chrs{$c} == ']') && ($top['what'] == JSON_IN_ARR)) {
 567                              // found a right-bracket, and we're in an array
 568                              array_pop($stk);
 569                              //print("Found end of array at {$c}: ".substr($chrs, $top['where'], (1 + $c - $top['where']))."\n");
 570  
 571                          } elseif (($chrs{$c} == '{') &&
 572                                   in_array($top['what'], array(JSON_SLICE, JSON_IN_ARR, JSON_IN_OBJ))) {
 573                              // found a left-brace, and we are in an array, object, or slice
 574                              array_push($stk, array('what' => JSON_IN_OBJ, 'where' => $c, 'delim' => false));
 575                              //print("Found start of object at {$c}\n");
 576  
 577                          } elseif (($chrs{$c} == '}') && ($top['what'] == JSON_IN_OBJ)) {
 578                              // found a right-brace, and we're in an object
 579                              array_pop($stk);
 580                              //print("Found end of object at {$c}: ".substr($chrs, $top['where'], (1 + $c - $top['where']))."\n");
 581  
 582                          } elseif (($substr_chrs_c_2 == '/*') &&
 583                                   in_array($top['what'], array(JSON_SLICE, JSON_IN_ARR, JSON_IN_OBJ))) {
 584                              // found a comment start, and we are in an array, object, or slice
 585                              array_push($stk, array('what' => JSON_IN_CMT, 'where' => $c, 'delim' => false));
 586                              $c++;
 587                              //print("Found start of comment at {$c}\n");
 588  
 589                          } elseif (($substr_chrs_c_2 == '*/') && ($top['what'] == JSON_IN_CMT)) {
 590                              // found a comment end, and we're in one now
 591                              array_pop($stk);
 592                              $c++;
 593  
 594                              for ($i = $top['where']; $i <= $c; ++$i)
 595                                  $chrs = substr_replace($chrs, ' ', $i, 1);
 596  
 597                              //print("Found end of comment at {$c}: ".substr($chrs, $top['where'], (1 + $c - $top['where']))."\n");
 598  
 599                          }
 600  
 601                      }
 602  
 603                      if (reset($stk) == JSON_IN_ARR) {
 604                          return $arr;
 605  
 606                      } elseif (reset($stk) == JSON_IN_OBJ) {
 607                          return $obj;
 608  
 609                      }
 610  
 611                  }
 612          }
 613      }
 614  
 615     /**
 616      * decodes a JSON string into appropriate variable; alias for decode()
 617      */
 618      function dec($var)
 619      {
 620          return $this->decode($var);
 621      }
 622  
 623  }
 624  
 625  ?>


Généré le : Tue Apr 3 20:47:31 2007 par Balluche grâce à PHPXref 0.7