-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathjson_decode.php
More file actions
185 lines (177 loc) · 6.06 KB
/
json_decode.php
File metadata and controls
185 lines (177 loc) · 6.06 KB
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
<?php
/**
* Implementation of function json_decode on PHP
*
* @author Alexander Muzychenko
* @link https://github.com/alexmuz/php-json
* @see http://php.net/json_decode
* @license GNU Lesser General Public License (LGPL) http://www.gnu.org/copyleft/lesser.html
*/
if (!function_exists('json_decode')) {
function json_decode($json, $assoc = false)
{
mb_internal_encoding("UTF-8");
$i = 0;
$n = strlen($json);
try {
$result = json_decode_value($json, $i, $assoc);
while ($i < $n && $json[$i] && $json[$i] <= ' ') $i++;
if ($i < $n) {
return null;
}
return $result;
} catch (Exception $e) {
return null;
}
}
function json_decode_value($json, &$i, $assoc = false)
{
$n = strlen($json);
while ($i < $n && $json[$i] && $json[$i] <= ' ') $i++;
switch ($json[$i]) {
// object
case '{':
$i++;
$result = $assoc ? array() : new stdClass();
while ($i < $n && $json[$i] && $json[$i] <= ' ') $i++;
if ($json[$i] === '}') {
$i++;
return $result;
}
while ($i < $n) {
$key = json_decode_string($json, $i);
while ($i < $n && $json[$i] && $json[$i] <= ' ') $i++;
if ($json[$i++] != ':') {
throw new Exception("Expected ':' on ".($i - 1));
}
if ($assoc) {
$result[$key] = json_decode_value($json, $i, $assoc);
} else {
$result->$key = json_decode_value($json, $i, $assoc);
}
while ($i < $n && $json[$i] && $json[$i] <= ' ') $i++;
if ($json[$i] === '}') {
$i++;
return $result;
}
if ($json[$i++] != ',') {
throw new Exception("Expected ',' on ".($i - 1));
}
while ($i < $n && $json[$i] && $json[$i] <= ' ') $i++;
}
throw new Exception("Syntax error");
// array
case '[':
$i++;
$result = array();
while ($i < $n && $json[$i] && $json[$i] <= ' ') $i++;
if ($json[$i] === ']') {
$i++;
return array();
}
while ($i < $n) {
$result[] = json_decode_value($json, $i, $assoc);
while ($i < $n && $json[$i] && $json[$i] <= ' ') $i++;
if ($json[$i] === ']') {
$i++;
return $result;
}
if ($json[$i++] != ',') {
throw new Exception("Expected ',' on ".($i - 1));
}
while ($i < $n && $json[$i] && $json[$i] <= ' ') $i++;
}
throw new Exception("Syntax error");
// string
case '"':
return json_decode_string($json, $i);
// number
case '-':
return json_decode_number($json, $i);
// true
case 't':
if ($i + 3 < $n && substr($json, $i, 4) === 'true') {
$i += 4;
return true;
}
// false
case 'f':
if ($i + 4 < $n && substr($json, $i, 5) === 'false') {
$i += 5;
return false;
}
// null
case 'n':
if ($i + 3 < $n && substr($json, $i, 4) === 'null') {
$i += 4;
return null;
}
default:
// number
if ($json[$i] >= '0' && $json[$i] <= '9') {
return json_decode_number($json, $i);
} else {
throw new Exception("Syntax error");
};
}
}
function json_decode_string($json, &$i)
{
$result = '';
$escape = array('"' => '"', '\\' => '\\', '/' => '/', 'b' => "\b", 'f' => "\f", 'n' => "\n", 'r' => "\r", 't' => "\t");
$n = strlen($json);
if ($json[$i] === '"') {
while (++$i < $n) {
if ($json[$i] === '"') {
$i++;
return $result;
} elseif ($json[$i] === '\\') {
$i++;
if ($json[$i] === 'u') {
$code = "&#".hexdec(substr($json, $i + 1, 4)).";";
$convmap = array(0x80, 0xFFFF, 0, 0xFFFF);
$result .= mb_decode_numericentity($code, $convmap, 'UTF-8');
$i += 4;
} elseif (isset($escape[$json[$i]])) {
$result .= $escape[$json[$i]];
} else {
break;
}
} else {
$result .= $json[$i];
}
}
}
throw new Exception("Syntax error");
}
function json_decode_number($json, &$i)
{
$result = '';
if ($json[$i] === '-') {
$result = '-';
$i++;
}
$n = strlen($json);
while ($i < $n && $json[$i] >= '0' && $json[$i] <= '9') {
$result .= $json[$i++];
}
if ($i < $n && $json[$i] === '.') {
$result .= '.';
$i++;
while ($i < $n && $json[$i] >= '0' && $json[$i] <= '9') {
$result .= $json[$i++];
}
}
if ($i < $n && ($json[$i] === 'e' || $json[$i] === 'E')) {
$result .= $json[$i];
$i++;
if ($json[$i] === '-' || $json[$i] === '+') {
$result .= $json[$i++];
}
while ($i < $n && $json[$i] >= '0' && $json[$i] <= '9') {
$result .= $json[$i++];
}
}
return (0 + $result);
}
}