-
Notifications
You must be signed in to change notification settings - Fork 78
/
fJSON.php
739 lines (642 loc) · 17 KB
/
fJSON.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
<?php
/**
* Provides encoding and decoding for JSON
*
* This class is a compatibility class for the
* [http://php.net/json json extension] on servers with PHP 5.0 or 5.1, or
* servers with the json extension compiled out.
*
* This class will handle JSON values that are not contained in an array or
* object - such values are not valid according to the JSON spec, but the
* functionality is included for compatiblity with the json extension.
*
* @copyright Copyright (c) 2008-2010 Will Bond
* @author Will Bond [wb] <[email protected]>
* @license http://flourishlib.com/license
*
* @package Flourish
* @link http://flourishlib.com/fJSON
*
* @version 1.0.0b6
* @changes 1.0.0b6 Removed `e` flag from preg_replace() calls [wb, 2010-06-08]
* @changes 1.0.0b5 Added the ::output() method [wb, 2010-03-15]
* @changes 1.0.0b4 Fixed a bug with ::decode() where JSON objects could lose all but the first key: value pair [wb, 2009-05-06]
* @changes 1.0.0b3 Updated the class to be consistent with PHP 5.2.9+ for encoding and decoding invalid data [wb, 2009-05-04]
* @changes 1.0.0b2 Changed @ error suppression operator to `error_reporting()` calls [wb, 2009-01-26]
* @changes 1.0.0b The initial implementation [wb, 2008-07-12]
*/
class fJSON
{
// The following constants allow for nice looking callbacks to static methods
const decode = 'fJSON::decode';
const encode = 'fJSON::encode';
const output = 'fJSON::output';
const sendHeader = 'fJSON::sendHeader';
/**
* An abstract representation of [
*
* @internal
*
* @var integer
*/
const J_ARRAY_OPEN = 0;
/**
* An abstract representation of , in a JSON array
*
* @internal
*
* @var integer
*/
const J_ARRAY_COMMA = 1;
/**
* An abstract representation of ]
*
* @internal
*
* @var integer
*/
const J_ARRAY_CLOSE = 2;
/**
* An abstract representation of {
*
* @internal
*
* @var integer
*/
const J_OBJ_OPEN = 3;
/**
* An abstract representation of a JSON object key
*
* @internal
*
* @var integer
*/
const J_KEY = 4;
/**
* An abstract representation of :
*
* @internal
*
* @var integer
*/
const J_COLON = 5;
/**
* An abstract representation of , in a JSON object
*
* @internal
*
* @var integer
*/
const J_OBJ_COMMA = 6;
/**
* An abstract representation of }
*
* @internal
*
* @var integer
*/
const J_OBJ_CLOSE = 7;
/**
* An abstract representation of an integer
*
* @internal
*
* @var integer
*/
const J_INTEGER = 8;
/**
* An abstract representation of a floating value
*
* @internal
*
* @var integer
*/
const J_FLOAT = 9;
/**
* An abstract representation of a boolean true
*
* @internal
*
* @var integer
*/
const J_TRUE = 10;
/**
* An abstract representation of a boolean false
*
* @internal
*
* @var integer
*/
const J_FALSE = 11;
/**
* An abstract representation of null
*
* @internal
*
* @var integer
*/
const J_NULL = 12;
/**
* An abstract representation of a string
*
* @internal
*
* @var integer
*/
const J_STRING = 13;
/**
* An array of special characters in JSON strings
*
* @var array
*/
static private $control_character_map = array(
'"' => '\"', '\\' => '\\\\', '/' => '\/', "\x8" => '\b',
"\xC" => '\f', "\n" => '\n', "\r" => '\r', "\t" => '\t'
);
/**
* An array of what values are allowed after other values
*
* @internal
*
* @var array
*/
static private $next_values = array(
self::J_ARRAY_OPEN => array(
self::J_ARRAY_OPEN => TRUE,
self::J_ARRAY_CLOSE => TRUE,
self::J_OBJ_OPEN => TRUE,
self::J_INTEGER => TRUE,
self::J_FLOAT => TRUE,
self::J_TRUE => TRUE,
self::J_FALSE => TRUE,
self::J_NULL => TRUE,
self::J_STRING => TRUE
),
self::J_ARRAY_COMMA => array(
self::J_ARRAY_OPEN => TRUE,
self::J_OBJ_OPEN => TRUE,
self::J_INTEGER => TRUE,
self::J_FLOAT => TRUE,
self::J_TRUE => TRUE,
self::J_FALSE => TRUE,
self::J_NULL => TRUE,
self::J_STRING => TRUE
),
self::J_ARRAY_CLOSE => array(
self::J_ARRAY_CLOSE => TRUE,
self::J_OBJ_CLOSE => TRUE,
self::J_ARRAY_COMMA => TRUE,
self::J_OBJ_COMMA => TRUE
),
self::J_OBJ_OPEN => array(
self::J_OBJ_CLOSE => TRUE,
self::J_KEY => TRUE
),
self::J_KEY => array(
self::J_COLON => TRUE
),
self::J_OBJ_COMMA => array(
self::J_KEY => TRUE
),
self::J_COLON => array(
self::J_ARRAY_OPEN => TRUE,
self::J_OBJ_OPEN => TRUE,
self::J_INTEGER => TRUE,
self::J_FLOAT => TRUE,
self::J_TRUE => TRUE,
self::J_FALSE => TRUE,
self::J_NULL => TRUE,
self::J_STRING => TRUE
),
self::J_OBJ_CLOSE => array(
self::J_ARRAY_CLOSE => TRUE,
self::J_OBJ_CLOSE => TRUE,
self::J_ARRAY_COMMA => TRUE,
self::J_OBJ_COMMA => TRUE
),
self::J_INTEGER => array(
self::J_ARRAY_CLOSE => TRUE,
self::J_OBJ_CLOSE => TRUE,
self::J_ARRAY_COMMA => TRUE,
self::J_OBJ_COMMA => TRUE
),
self::J_FLOAT => array(
self::J_ARRAY_CLOSE => TRUE,
self::J_OBJ_CLOSE => TRUE,
self::J_ARRAY_COMMA => TRUE,
self::J_OBJ_COMMA => TRUE
),
self::J_TRUE => array(
self::J_ARRAY_CLOSE => TRUE,
self::J_OBJ_CLOSE => TRUE,
self::J_ARRAY_COMMA => TRUE,
self::J_OBJ_COMMA => TRUE
),
self::J_FALSE => array(
self::J_ARRAY_CLOSE => TRUE,
self::J_OBJ_CLOSE => TRUE,
self::J_ARRAY_COMMA => TRUE,
self::J_OBJ_COMMA => TRUE
),
self::J_NULL => array(
self::J_ARRAY_CLOSE => TRUE,
self::J_OBJ_CLOSE => TRUE,
self::J_ARRAY_COMMA => TRUE,
self::J_OBJ_COMMA => TRUE
),
self::J_STRING => array(
self::J_ARRAY_CLOSE => TRUE,
self::J_OBJ_CLOSE => TRUE,
self::J_ARRAY_COMMA => TRUE,
self::J_OBJ_COMMA => TRUE
)
);
/**
* Decodes a JSON string into native PHP data types
*
* This function is very strict about the format of JSON. If the string is
* not a valid JSON string, `NULL` will be returned.
*
* @param string $json This should be the name of a related class
* @param boolean $assoc If this is TRUE, JSON objects will be represented as an assocative array instead of a `stdClass` object
* @return array|stdClass A PHP equivalent of the JSON string
*/
static public function decode($json, $assoc=FALSE)
{
if (!is_string($json) && !is_numeric($json)) {
return NULL;
}
$json = trim($json);
if ($json === '') {
return NULL;
}
// If the json is an array or object, we can rely on the php function
if (function_exists('json_decode') && ($json[0] == '[' || $json[0] == '{' || version_compare(PHP_VERSION, '5.2.9', '>='))) {
return json_decode($json, $assoc);
}
preg_match_all('~\[| # Array begin
\]| # Array end
{| # Object begin
}| # Object end
-?(?:0|[1-9]\d*) # Float
(?:\.\d*(?:[eE][+\-]?\d+)?|
(?:[eE][+\-]?\d+))|
-?(?:0|[1-9]\d*)| # Integer
true| # True
false| # False
null| # Null
,| # Member separator for arrays and objects
:| # Value separator for objects
"(?:(?:(?!\\\\u)[^\\\\"\n\b\f\r\t]+)| # String
\\\\\\\\|
\\\\/|
\\\\"|
\\\\b|
\\\\f|
\\\\n|
\\\\r|
\\\\t|
\\\\u[0-9a-fA-F]{4})*"|
\s+ # Whitespace
~x', $json, $matches);
$matched_length = 0;
$stack = array();
$last = NULL;
$last_key = NULL;
$output = NULL;
$container = NULL;
if (sizeof($matches) == 1 && strlen($matches[0][0]) == strlen($json)) {
$match = $matches[0][0];
$stack = array();
$type = self::getElementType($stack, self::J_ARRAY_OPEN, $match);
$element = self::scalarize($type, $match);
if ($match !== $element) {
return $element;
}
}
if ($json[0] != '[' && $json[0] != '{') {
return NULL;
}
foreach ($matches[0] as $match) {
if ($matched_length == 0) {
if ($match == '[') {
$output = array();
$last = self::J_ARRAY_OPEN;
} else {
$output = ($assoc) ? array() : new stdClass();
$last = self::J_OBJ_OPEN;
}
$stack[] = array($last, &$output);
$container =& $output;
$matched_length = 1;
continue;
}
$matched_length += strlen($match);
// Whitespace can be skipped over
if (ctype_space($match)) {
continue;
}
$type = self::getElementType($stack, $last, $match);
// An invalid sequence will cause parsing to stop
if (!isset(self::$next_values[$last][$type])) {
break;
}
// Decode the data values
$match = self::scalarize($type, $match);
// If the element is not a value, record some info and continue
if ($type == self::J_COLON ||
$type == self::J_OBJ_COMMA ||
$type == self::J_ARRAY_COMMA ||
$type == self::J_KEY) {
$last = $type;
if ($type == self::J_KEY) {
$last_key = $match;
}
continue;
}
// This flag is used to indicate if an array or object is being added and thus
// if the container reference needs to be changed to the current match
$ref_match = FALSE;
// Closing an object or array
if ($type == self::J_OBJ_CLOSE || $type == self::J_ARRAY_CLOSE) {
array_pop($stack);
if (sizeof($stack) == 0) {
break;
}
$new_container = end($stack);
$container =& $new_container[1];
$last = $type;
continue;
}
// Opening a new object or array requires some references to keep
// track of what the current container is
if ($type == self::J_OBJ_OPEN) {
$match = ($assoc) ? array() : new stdClass();
$ref_match = TRUE;
}
if ($type == self::J_ARRAY_OPEN) {
$match = array();
$ref_match = TRUE;
}
if ($ref_match) {
$stack[] = array($type, &$match);
$stack_end = end($stack);
}
// Here we assign the value. This code is kind of crazy because
// we have to keep track of the current container by references
// so we can traverse back down the stack as we move out of
// nested arrays and objects
if ($last == self::J_COLON && !$assoc) {
if ($last_key == '') {
$last_key = '_empty_';
}
if ($ref_match) {
$container->$last_key =& $stack_end[1];
$container =& $stack_end[1];
} else {
$container->$last_key = $match;
}
} elseif ($last == self::J_COLON) {
if ($ref_match) {
$container[$last_key] =& $stack_end[1];
$container =& $stack_end[1];
} else {
$container[$last_key] = $match;
}
} else {
if ($ref_match) {
$container[] =& $stack_end[1];
$container =& $stack_end[1];
} else {
$container[] = $match;
}
}
if ($last == self::J_COLON) {
$last_key = NULL;
}
$last = $type;
unset($match);
}
if ($matched_length != strlen($json) || sizeof($stack) > 0) {
return NULL;
}
return $output;
}
/**
* Encodes a PHP value into a JSON string
*
* @param mixed $value The PHP value to encode
* @return string The JSON string that is equivalent to the PHP value
*/
static public function encode($value)
{
if (is_resource($value)) {
return 'null';
}
if (function_exists('json_encode')) {
return json_encode($value);
}
if (is_int($value)) {
return (string) $value;
}
if (is_float($value)) {
return (string) $value;
}
if (is_bool($value)) {
return ($value) ? 'true' : 'false';
}
if (is_null($value)) {
return 'null';
}
if (is_string($value)) {
if (!preg_match('#^.*$#usD', $value)) {
return 'null';
}
$char_array = fUTF8::explode($value);
$output = '"';
foreach ($char_array as $char) {
if (isset(self::$control_character_map[$char])) {
$output .= self::$control_character_map[$char];
} elseif (strlen($char) < 2) {
$output .= $char;
} else {
$output .= '\u' . substr(strtolower(fUTF8::ord($char)), 2);
}
}
$output .= '"';
return $output;
}
// Detect if an array is associative, which would mean it needs to be encoded as an object
$is_assoc_array = FALSE;
if (is_array($value) && $value) {
$looking_for = 0;
foreach ($value as $key => $val) {
if (!is_numeric($key) || $key != $looking_for) {
$is_assoc_array = TRUE;
break;
}
$looking_for++;
}
}
if (is_object($value) || $is_assoc_array) {
$output = '{';
$members = array();
foreach ($value as $key => $val) {
$members[] = self::encode((string) $key) . ':' . self::encode($val);
}
$output .= join(',', $members);
$output .= '}';
return $output;
}
if (is_array($value)) {
$output = '[';
$members = array();
foreach ($value as $key => $val) {
$members[] = self::encode($val);
}
$output .= join(',', $members);
$output .= ']';
return $output;
}
}
/**
* Determines the type of a parser JSON element
*
* @param array &$stack The stack of arrays/objects being parsed
* @param integer $last The type of the last element parsed
* @param string $element The element being detected
* @return integer The element type
*/
static private function getElementType(&$stack, $last, $element)
{
if ($element == '[') {
return self::J_ARRAY_OPEN;
}
if ($element == ']') {
return self::J_ARRAY_CLOSE;
}
if ($element == '{') {
return self::J_OBJ_OPEN;
}
if ($element == '}') {
return self::J_OBJ_CLOSE;
}
if (ctype_digit($element)) {
return self::J_INTEGER;
}
if (is_numeric($element)) {
return self::J_FLOAT;
}
if ($element == 'true') {
return self::J_TRUE;
}
if ($element == 'false') {
return self::J_FALSE;
}
if ($element == 'null') {
return self::J_NULL;
}
$last_stack = end($stack);
if ($element == ',' && $last_stack[0] == self::J_ARRAY_OPEN) {
return self::J_ARRAY_COMMA;
}
if ($element == ',') {
return self::J_OBJ_COMMA;
}
if ($element == ':') {
return self::J_COLON;
}
if ($last == self::J_OBJ_OPEN || $last == self::J_OBJ_COMMA) {
return self::J_KEY;
}
return self::J_STRING;
}
/**
* Created a unicode code point from a JS escaped unicode character
*
* @param array $match A regex match containing the 4 digit code referenced by the key `1`
* @return string The U+{digits} unicode code point
*/
static private function makeUnicodeCodePoint($match)
{
return fUTF8::chr("U+" . $match[1]);
}
/**
* Sets the proper `Content-Type` header and outputs the value, encoded as JSON
*
* @param mixed $value The PHP value to output as JSON
* @return void
*/
static public function output($value)
{
self::sendHeader();
echo self::encode($value);
}
/**
* Decodes a scalar value
*
* @param integer $type The type of the element
* @param string $element The element to be converted to a scalar
* @return mixed The scalar value or the original string of the element
*/
static private function scalarize($type, $element)
{
if ($type == self::J_INTEGER) {
$element = (integer) $element;
}
if ($type == self::J_FLOAT) {
$element = (float) $element;
}
if ($type == self::J_FALSE) {
$element = FALSE;
}
if ($type == self::J_TRUE) {
$element = TRUE;
}
if ($type == self::J_NULL) {
$element = NULL;
}
if ($type == self::J_STRING || $type == self::J_KEY) {
$element = substr($element, 1, -1);
$element = strtr($element, array_flip(self::$control_character_map));
$element = preg_replace_callback('#\\\\u([0-9a-fA-F]{4})#', array('self', 'makeUnicodeCodePoint'), $element);
}
return $element;
}
/**
* Sets the proper `Content-Type` header for UTF-8 encoded JSON
*
* @return void
*/
static public function sendHeader()
{
header('Content-Type: application/json; charset=utf-8');
}
/**
* Forces use as a static class
*
* @return fJSON
*/
private function __construct() { }
}
/**
* Copyright (c) 2008-2010 Will Bond <[email protected]>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/