-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjson_lite.php
400 lines (383 loc) · 10.2 KB
/
json_lite.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
<?php
/**
* JSON Lite
*
* A small and fast PHP JSON library
*
* @package JSON-Lite
* @version 1.0
* @author Jeremy Messenger <[email protected]>
* @copyright 2011 Jeremy Messenger
* @license LGPL <http://www.gnu.org/licenses/lgpl.html>
* @link http://jlmessenger.com
*/
/**
* Class that provides JSON creation and parsing along with JSON-RPC helpers
* @package JSON-Lite
*/
class json_lite
{
/**
* Creates a JSON string in simple JSON RPC format
* @param string $method Remote method to execute
* @param array $params Array of parameters for the method
* @return string A JSON-RPC string
*/
function encode_json_rpc($method, $params)
{
return json_lite::to_json(array(
'method' => $method,
'params' => $params
));
}
/**
* Execute a JSON-PRC call on the matching method in the supplied class
* @param string $json A JSON-RPC string
* @param object $class A class with methods available to for RPC execution
* @param string $method_prefix A prefix to add to each RPC method (to limit what can be executed)
* @return mixed|null The result of the executed method
*/
function exec_json_rpc($json, $class, $method_prefix = '', &$rpc_values)
{
$rpc_values = json_lite::from_json($json);
if (is_array($rpc_values)
&& array_key_exists('method', $rpc_values)
&& array_key_exists('params', $rpc_values)
&& is_array($rpc_values['params']))
{
$method = $method_prefix.$rpc_values['method'];
if (method_exists($class, $method))
{
return call_user_func_array(array($class, $method), $rpc_values['params']);
}
}
return NULL;
}
/**
* Convert a PHP array or value into a JSON string
* @param mixed $value PHP array or value
* @return string A JSON string
*/
function to_json($value)
{
if (is_bool($value))
{
return $value ? 'true' : 'false';
}
elseif (is_null($value))
{
return 'null';
}
elseif (is_numeric($value))
{
return (string)$value;
}
elseif (is_array($value))
{
if (count($value) == 0 || array_keys($value) === range(0, count($value)-1))
{
// array is 0 based sequential
$items = array();
foreach ($value as $val)
{
$items[] = json_lite::to_json($val);
}
return '['.implode(',', $items).']';
}
else
{
// array is associative
$items = array();
foreach ($value as $key => $val)
{
$items[] = json_lite::_json_str($key).':'.json_lite::to_json($val);
}
return '{'.implode(',', $items).'}';
}
}
else
{
return json_lite::_json_str($value);
}
}
/**
* Convert PHP string to a valid JSON string
* Additionally encodes , [ ] { } characters in hex format
*/
function _json_str($value)
{
$value = (string)$value;
$json = '"';
for ($i = 0; $i < strlen($value); $i++)
{
$o = ord($value{$i});
switch ($o)
{
case 8: $json .= '\\b'; break; // backspace
case 9: $json .= '\\t'; break; // tab
case 10: $json .= '\\n'; break; // linefeed
case 12: $json .= '\\f'; break; // formfeed
case 13: $json .= '\\r'; break; // carriage return
case 34: // double-quote
case 47: // forward-slash
case 92: // back-slash
$json .= '\\'.$value{$i}; break;
case 44: $json .= '\\u002c'; break; // comma
case 91: $json .= '\\u005b'; break; // open-bracket
case 93: $json .= '\\u005d'; break; // close-bracket
case 123: $json .= '\\u007b'; break; // open-curly-bracket
case 125: $json .= '\\u007d'; break; // close-curly-bracket
default:
if ($o < 32 || $o > 126)
{
$json .= '\\u'.str_pad(dechex($o), 4, '0', STR_PAD_LEFT);
}
else
{
$json .= $value{$i};
}
}
}
$json .= '"';
return $json;
}
/**
* Convert a JSON string into native PHP arrays/values
* @param string $json A JSON string (created by to_json)
* @return mixed PHP array or value
*/
function from_json(&$json, $try_php_json = TRUE)
{
if ($try_php_json && extension_loaded('json'))
{
return json_decode($json, TRUE);
}
else
{
return json_lite_node::parse_json($json);
}
}
}
/**
* Class for recursively parsing JSON nodes
* @package JSON-Lite
*/
class json_lite_node
{
/**
* Position of start boundary character within JSON string for this node
* @var integer
*/
var $start_str_pos;
/**
* Position of end boundary character within JSON string for this node
* @var integer
*/
var $end_str_pos;
/**
* If this JSON node is { } then TRUE, if [ ] then FALSE
* @var boolean
*/
var $is_assoc;
/**
* Final parsed value of this node
* @var array
*/
var $value = array();
/**
* Array of sub-nodes within this node
* @var array
*/
var $sub_nodes = array();
/**
* Use json_lite_node::parse_json()
* Parse a JSON string into php array
* @param string $json JSON string
* @return array PHP representation of JSON data
*/
function parse_json(&$json)
{
// find all { } [ ] JSON array/object boundaries
preg_match_all('/[\[\]\{\}]/', $json, $boundaries, PREG_OFFSET_CAPTURE);
// recursivly populate the nodes
$b_index = 0;
$nodes = new json_lite_node($json, $boundaries[0], $b_index);
return $nodes->value;
}
/**
* Don't use directly, Use json_lite_node::parse_json()
* Creates a JSON node
* @param string $json JSON string
* @param array $boundaries Array of { } [ ] boundary points from json_lite_node::parse_json
* @param integer $b_index The starting index of the boundaries array to process
*/
function json_lite_node(&$json, &$boundaries, &$b_index)
{
$this->start_str_pos = $boundaries[$b_index][1];
$this->is_assoc = $boundaries[$b_index][0] == '{';
for ($i = $b_index + 1; $i < count($boundaries); $i++)
{
switch ($boundaries[$i][0])
{
case '{':
case '[':
// begin processing of a nested-node
// (note: $i is passed by reference so that subnodes
// are skipped upon returning from the sub-node)
$this->sub_nodes[] = new json_lite_node($json, $boundaries, $i);
break;
case '}':
if ($this->is_assoc)
{
// complete processing of the current associative node
$b_index = $i; // ending b_index is updated for reference by the parent
$this->close_node($json, $boundaries, $i);
break 2;
}
case ']':
if (!$this->is_assoc)
{
// complete processing of the current sequential node
$b_index = $i; // ending b_index is updated for reference by the parent
$this->close_node($json, $boundaries, $i);
break 2;
}
}
}
}
/**
* Closing the node populates the final data points
* including the nodes value array
* @param string $json JSON string
* @param array $boundaries Array of { } [ ] boundary points from json_lite_node::parse_json
* @param integer $b_index The ending index of the boundaries array being closed
*/
function close_node(&$json, &$boundaries, $b_index)
{
$this->end_b_index = $b_index;
$this->end_str_pos = $boundaries[$b_index][1];
if (count($this->sub_nodes))
{
// deal with sub-node values
// (note: each subnode is removed and replaced with a string in the format '&#'
// which is then pupulated with that sub-nodes value later)
$content = substr($json, $this->start_str_pos + 1, $this->sub_nodes[0]->start_str_pos - $this->start_str_pos - 1);
for ($j = 0; $j < count($this->sub_nodes) - 1; $j++)
{
$content .= "&$j";
$content .= substr($json, $this->sub_nodes[$j]->end_str_pos + 1, $this->sub_nodes[$j+1]->start_str_pos - $this->sub_nodes[$j]->end_str_pos - 1);
}
$content .= "&$j";
$content .= substr($json, $this->sub_nodes[$j]->end_str_pos + 1, $this->end_str_pos - $this->sub_nodes[$j]->end_str_pos - 1);
}
else
{
// no sub-nodes, parse entire string as usual
$content = substr($json, $this->start_str_pos + 1, $this->end_str_pos - $this->start_str_pos - 1);
}
// items in JSON are separated by commas
$segments = explode(',', $content);
if (count($segments) == 1 && trim($segments[0]) == '')
{
return; // empty array
}
if ($this->is_assoc)
{
// associative array nodes have keys: value format
foreach ($segments as $segment)
{
list($key, $val) = explode(':', $segment, 2);
$key = $this->_node_str_value(trim($key));
$this->value[$key] = $this->_node_value(trim($val));
}
}
else
{
// sequential array node are just lists of values
foreach ($segments as $segment)
{
$this->value[] = $this->_node_value(trim($segment));
}
}
}
/**
* Convert node value to native PHP type
* @param string $json JSON number, string, boolean, null, or sub-node reference
* @return mixed PHP value
*/
function _node_value($json)
{
// fix simple types
switch ($json)
{
case 'true': return TRUE;
case 'false': return FALSE;
case 'null': return NULL;
}
if (is_numeric($json))
{
// rely on php type juggling for numbers
return 0 + $json;
}
switch ($json{0})
{
case '"':
// parse string types
return $this->_node_str_value($json);
case '&':
// parse sub-node references
$sub_key = substr($json, 1);
if (array_key_exists($sub_key, $this->sub_nodes))
return $this->sub_nodes[$sub_key]->value;
}
return ''; // unknown defaults to empty string
}
/**
* Convert string node value to native PHP string
* @param string $json JSON string
* @return string PHP string
*/
function _node_str_value($json)
{
$str = '';
$bs = FALSE; // backslashed state
for ($i = 1; $i < strlen($json) - 1; $i++)
{
if ($bs)
{
// in backslash mode
switch ($json{$i})
{
case 'b': $str .= chr(8); break; // backspace
case 't': $str .= chr(9); break; // tab
case 'n': $str .= chr(10); break; // linefeed
case 'f': $str .= chr(12); break; // formfeed
case 'r': $str .= chr(13); break; // carriage return
case '"': // double-quote
case '/': // forward-slash
case '\\': // back-slash
$str .= $json{$i}; break;
case 'u': // hex mode
$h = substr($json, $i+1, 4);
$i += 4;
$str .= chr(hexdec($h));
break;
default:
// unknown escape char
// return literal
$str .= $json{$i};
}
$bs = FALSE;
}
elseif ($json{$i} == '\\')
{
$bs = TRUE; // put in backslash mode
}
else
{
$str .= $json{$i};
}
}
return utf8_encode($str);
}
}