forked from rin-nas/php-json
/
JSON.php
284 lines (271 loc) · 8.62 KB
/
JSON.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
<?php
/**
* Encode/decode a JSON data
*
* Since PHP/5.2.0 there is a feature json_encode() and json_decode(),
* but the choice of this class is preferable.
*
* JSON::encode() features
* * Not only works with UTF-8, but also with any other single-byte encodings (eg: windows-1251, koi8-r)
* * Is able to convert numbers represented a string data type into the corresponding numeric data types (optional)
* * Non-ASCII characters leave as is, and does not convert to \uXXXX.
*
* JSON::decode() features
* * Normalizes a "dirty" JSON string, coming from JavaScript (smart behavior -- only if necessary)
*
* @link http://code.google.com/p/php-json/
* @license http://creativecommons.org/licenses/by-sa/3.0/
* @author Nasibullin Rinat
* @version 2.1.2
*/
class JSON
{
#calling the methods of this class only statically!
private function __construct() {}
/**
* @link http://json.org/
* @var array
*/
protected static $escape_table = array(
"\x5c" => '\\\\', #reverse solidus
"\x22" => '\"', #quotation mark
"\x2f" => '\/', #solidus
"\x08" => '\b', #backspace
"\x0c" => '\f', #formfeed
"\x0a" => '\n', #new line
"\x0d" => '\r', #carriage return
"\x09" => '\t', #horizontal tab
);
/**
* Specially additions coming from JavaScript
* @var array
*/
protected static $spec_escape_table = array(
"\x27" => "\'", #single quotation mark
);
/**
* Converts objects to array recursive
*
* @param mixed $object
* @return mixed
*/
public static function objectToArray($object)
{
if ($object instanceof Traversable)
{
$vars = array();
foreach ($object as $k => $v) $vars[$k] = $v;
$object = $vars;
}
elseif (is_object($object)) $object = get_object_vars($object); #$object = (array)$object;
if (is_array($object)) foreach ($object as $k => $v) $object[$k] = self::objectToArray($v);
return $object;
}
/**
* Encodes to a JSON string
*
* @link http://www.json.org/
* @link http://php.net/json_encode
* @param scalar|object|array|null $a
* @param string $quote Quote char: '"', "'" or '' (empty)
* @param bool $is_convert_numeric Convert numbers represented a string data type in the corresponding numeric data types
* @param bool $_is_key Private for recursive calling
* @return string|bool Returns FALSE if error occurred
*/
public static function encode($a, $quote = '"', $is_convert_numeric = false, $_is_key = false)
{
if (! ReflectionTypeHint::isValid()) return false;
if ($a === null) return 'null';
if ($a === false) return 'false';
if ($a === true) return 'true';
if (is_scalar($a))
{
if (is_int($a))
{
$a = strval($a);
if (! $_is_key) return $a;
return $quote . $a . $quote;
}
if (is_float($a))
{
$a = str_replace(',', '.', strval($a)); #always use "." for floats
if (! $_is_key) return $a;
return $quote . $a . $quote;
}
#string:
if (! $_is_key && $is_convert_numeric && (ctype_digit($a) || preg_match('/^-?+\d+(?>\.\d+)?+(?>[eE]\d+)?$/sSX', $a))) return $a;
$escape_table = self::$escape_table + ($quote !== '"' ? self::$spec_escape_table : array());
return $quote . strtr($a, $escape_table) . $quote;
}
if (is_object($a)) $a = self::objectToArray($a);
elseif (! is_array($a))
{
trigger_error('An any type (except a resource) expected, ' . gettype($a) . ' given!', E_USER_WARNING);
return false;
}
$is_list = true;
for ($i = 0, reset($a); $i < count($a); $i++, next($a))
{
if (key($a) !== $i)
{
$is_list = false;
break;
}
}
$result = array();
if ($is_list)
{
foreach ($a as $v)
{
$s = self::encode($v, $quote, $is_convert_numeric);
if ($s === false) return false;
$result[] = $s;
}
return '[' . implode(',', $result) . ']';
}
foreach ($a as $k => $v)
{
#it is impossible put the numbers as keys of the hash, use only string (hi, IE-5.01 browser)
$k = self::encode($k, $quote !== '' ? $quote : '"', $is_convert_numeric, true);
if ($k === false) return false;
$v = self::encode($v, $quote, $is_convert_numeric);
if ($v === false) return false;
$result[] = $k . ':' . $v;
}
return '{' . implode(',', $result) . '}';
}
/**
* Decodes a JSON string
*
* @param string $s The json string being decoded
* @param bool $is_assoc When TRUE, returned objects will be converted into associative arrays
* @param int $depth User specified recursion depth
* @return object|array|null Returns an object or if the optional $is_assoc parameter is TRUE, an associative array is instead returned.
* NULL is returned if the json cannot be decoded or if the encoded data is deeper than the recursion limit.
*/
public static function decode($s, $is_assoc = false, $depth = 512)
{
if (! ReflectionTypeHint::isValid()) return null;
$ret = @json_decode($s, $is_assoc, $depth);
if (json_last_error() !== JSON_ERROR_SYNTAX) return $ret;
$s = self::normalize($s);
if (! is_string($s)) return null;
return json_decode($s, $is_assoc, $depth);
}
/**
* Converts malformed JSON to well-formed JSON
* Hint: normalizes a "dirty" JSON string, coming from JavaScript
*
* @param string|null $s The json string being normalized
* @return string|bool|null Returns FALSE if error occurred
*/
public static function normalize($s)
{
if (! ReflectionTypeHint::isValid()) return false;
if (is_null($s)) return $s;
$s = preg_replace_callback('~(?>
#comments
(\/\* .*? \*\/) #1 multi line comment
| (\/\/ [^\r\n]*+) #2 single line comment
#spaces
| (\s++) #3
#strings
| " ((?>[^"\\\\]+ |\\\\.)*) " #4
| \' ((?>[^\'\\\\]+|\\\\.)*) \' #5
#trailing commas
| (,) #6
(?> \/\* .*? \*\/ #multi line comment
| \/\/ [^\r\n]*+ #single line comment
| \s++
)*+
(?=[\]}])
#keys
| ([a-zA-Z\d_]++) #7
(?> \/\* .*? \*\/ #multi line comment
| \/\/ [^\r\n]*+ #single line comment
| \s++
)*+
(?=:)
)
~sxuSX', array('self', '_normalize'), $s);
if (! is_string($s)) return false;
return $s;
}
private static function _normalize(array $m)
{
#d($m);
if (isset($m[1]) && $m[1] !== '') return ''; #multi line comment
if (isset($m[2]) && $m[2] !== '') return ''; #single line comment
if (isset($m[3]) && $m[3] !== '') return ''; #spaces
if (isset($m[4]) && $m[4] !== '') $m[5] = $m[4]; #string
if (isset($m[5]) && $m[5] !== '')
{
#decode:
$unescape_table = array_flip(self::$escape_table);
$s = preg_replace_callback(
'~(?> \\\\u([\da-fA-F]{4}) #1
| \\\\(.) #2
| [^\\\\]++
)~sxSX',
function(array $m) use ($unescape_table)
{
if (isset($m[1]) && $m[1] !== '')
{
$codepoint = hexdec($m[1]);
return UTF8::chr($codepoint);
}
if (isset($m[2]) && $m[2] !== '')
{
if (array_key_exists($m[0], $unescape_table)) return $unescape_table[$m[0]];
return $m[2];
}
return $m[0];
},
$m[5]);
#encode:
$s = strtr($s, self::$escape_table);
return '"' . $s . '"';
}
if (isset($m[6]) && $m[6] !== '') return ''; #trailing commas
if (isset($m[7]) && $m[7] !== '') return '"' . $m[7] . '"'; #keys
return $m[0];
}
public static function tests()
{
assert_options(ASSERT_ACTIVE, true);
assert_options(ASSERT_BAIL, true);
assert_options(ASSERT_WARNING, true);
assert_options(ASSERT_QUIET_EVAL, false);
$s0 = '{"1":2,"33":-44.11,"<tag>":"&bar&","null":null,"true":true,"false":false,"c":"d","e":"f","\/":"\/","ПРИВЕТ":"привет","new\r\n\\\\line":"next\nline","\'":"\\"","g":[1,"a","b","c"]}';
$s1 = '{
//single line comment
1:2,
33:-44.11,
"\u003Ctag\u003E" : "\u0026bar\u0026",
/*multi
line
comment*/
null: null,
true : true,
false
:
false
,
\'c\' :\'d\',
"e" : "f",
"/": "\/",
"ПРИВЕТ": \'привет\' ,
"new\r\n\\\\line" : "next\\
line",
"\\\'" : \'"\',
g:[1,\'a\' ,\'b\', \'c\' , ],
}';
$a = array(
'self::normalize($s1) === $s0',
'gettype(self::decode($s1, true)) === "array"',
'self::encode(self::decode($s1, true)) === $s0',
);
foreach ($a as $k => $v) if (! assert($v)) return false;
return true;
}
}