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