forked from infojunkie/MantisBT-Slack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFastJSON.class.php5
370 lines (364 loc) · 12 KB
/
FastJSON.class.php5
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
<?php
/**_______________________________________
*
* FastJSON (strict version for PHP 5.2 or greater),
* simple and fast Pear Service_JSON encoder/decoder alternative
* [http://pear.php.net/pepr/pepr-proposal-show.php?id=198]
* ---------------------------------------
* This class is about two time faster than Pear Service_JSON class.
* This class is probably not powerful as Service_JSON but it has
* no dependencies and converts correctly ASCII range 0x00 - 0x1F too.
* There's any string convertion, just regular RFC specific characters are converted
* into \u00XX string.
* To don't have problems with other chars try to use utf8_encode($json_encoded_string).
* To recieve correctly JSON strings from JavaScript use encodeURIComponent then
* use, if is necessary, utef8_decode before JS to PHP convertion.
* decode method doesn't returns a standard object class but You can
* create the corret class directly with FastJSON::convert method
* and with them You can manage JS Date objects too.
* ---------------------------------------
* Summary of static public methods
*
* convert
* extra, special method
*
* decode
* converts a valid JSON string
* into a native PHP variable
*
* encode
* converts a native php variable
* into a valid JSON string
* ---------------------------------------
*
* Special FastJSON::convert method Informations
* _______________________________________
* ---------------------------------------
* This method is used by FastJSON::encode method but should be used
* to do these convertions too:
*
* - JSON string to time() integer:
*
* FastJSON::convert(decodedDate:String):time()
*
* If You recieve a date string rappresentation You
* could convert into respective time() integer.
* Example:
* FastJSON::convert(FastJSON::decode($clienttime));
* // i.e. $clienttime = 2006-11-09T14:42:30
* // returned time will be an integer useful with gmdate or date
* // to create, for example, this string
* // Thu Nov 09 2006 14:42:30 GMT+0100 (Rome, Europe)
*
* - time() to JSON string:
*
* FastJSON::convert(time():Int32, true:Boolean):JSON Date String format
*
* You could send server time() informations and send them to clients.
* Example:
* FastJSON::convert(time(), true);
* // i.e. 2006-11-09T14:42:30
*
* - associative array to generic class:
*
* FastJSON::convert(array(params=>values), new GenericClass):new Instance of GenericClass
*
* With a decoded JSON object You could convert them
* into a new instance of your Generic Class.
* Example:
* class MyClass {
* var $param = "somevalue";
* function MyClass($somevar) {
* $this->somevar = $somevar;
* };
* function getVar = function(){
* return $this->somevar;
* };
* };
*
* $instance = new MyClass("example");
* $encoded = FastJSON::encode($instance);
* // {"param":"somevalue"}
*
* $decoded = FastJSON::decode($encoded);
* // $decoded instanceof Object => true
* // $decoded instanceof MyClass => false
*
* $decoded = FastJSON::convert($decoded, new MyClass("example"));
* // $decoded instanceof Object => true
* // $decoded instanceof MyClass => true
*
* $decoded->getVar(); // example
*
* ---------------------------------------
*
* @author Andrea Giammarchi
* @site http://www.devpro.it/
* @version 0.4 [fixed string convertion problems, add stdClass optional convertion instead of associative array (used by default)]
* @requires anything
* @compatibility PHP >= 5
* @license
* ---------------------------------------
*
* Copyright (c) 2006 - 2007 Andrea Giammarchi
*
* 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.
* _______________________________________
*/
class FastJSON {
// public methods
/**
* public static method
*
* FastJSON::convert(params:* [, result:Instance]):*
*
* @param * String or Object
* @param Instance optional new generic class instance if first
* parameter is an object.
* @return * time() value or new Instance with object parameters.
*
* @note please read Special FastJSON::convert method Informations
*/
static public function convert($params, $result = null){
switch(gettype($params)){
case 'array':
$tmp = array();
foreach($params as $key => $value) {
if(($value = FastJSON::encode($value)) !== '')
array_push($tmp, FastJSON::encode(strval($key)).':'.$value);
};
$result = '{'.implode(',', $tmp).'}';
break;
case 'boolean':
$result = $params ? 'true' : 'false';
break;
case 'double':
case 'float':
case 'integer':
$result = $result !== null ? strftime('%Y-%m-%dT%H:%M:%S', $params) : strval($params);
break;
case 'NULL':
$result = 'null';
break;
case 'string':
$i = create_function('&$e, $p, $l', 'return intval(substr($e, $p, $l));');
if(preg_match('/^[0-9]{4}\-[0-9]{2}\-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}$/', $params))
$result = mktime($i($params, 11, 2), $i($params, 14, 2), $i($params, 17, 2), $i($params, 5, 2), $i($params, 9, 2), $i($params, 0, 4));
break;
case 'object':
$tmp = array();
if(is_object($result)) {
foreach($params as $key => $value)
$result->$key = $value;
} else {
$result = get_object_vars($params);
foreach($result as $key => $value) {
if(($value = FastJSON::encode($value)) !== '')
array_push($tmp, FastJSON::encode($key).':'.$value);
};
$result = '{'.implode(',', $tmp).'}';
}
break;
}
return $result;
}
/**
* public method
*
* FastJSON::decode(params:String[, useStdClass:Boolean]):*
*
* @param String valid JSON encoded string
* @param Bolean uses stdClass instead of associative array if params contains objects (default false)
* @return * converted variable or null
* is params is not a JSON compatible string.
* @note This method works in an optimist way. If JSON string is not valid
* the code execution will die using exit.
* This is probably not so good but JSON is often used combined with
* XMLHttpRequest then I suppose that's better more protection than
* just some WARNING.
* With every kind of valid JSON string the old error_reporting level
* and the old error_handler will be restored.
*
* @example
* FastJSON::decode('{"param":"value"}'); // associative array
* FastJSON::decode('{"param":"value"}', true); // stdClass
* FastJSON::decode('["one",two,true,false,null,{},[1,2]]'); // array
*/
static public function decode($encode, $stdClass = false){
$pos = 0;
$slen = is_string($encode) ? strlen($encode) : null;
if($slen !== null) {
$error = error_reporting(0);
set_error_handler(array('FastJSON', '__exit'));
$result = FastJSON::__decode($encode, $pos, $slen, $stdClass);
error_reporting($error);
restore_error_handler();
}
else
$result = null;
return $result;
}
/**
* public method
*
* FastJSON::encode(params:*):String
*
* @param * Array, Boolean, Float, Int, Object, String or NULL variable.
* @return String JSON genric object rappresentation
* or empty string if param is not compatible.
*
* @example
* FastJSON::encode(array(1,"two")); // '[1,"two"]'
*
* $obj = new MyClass();
* obj->param = "value";
* obj->param2 = "value2";
* FastJSON::encode(obj); // '{"param":"value","param2":"value2"}'
*/
static public function encode($decode){
$result = '';
switch(gettype($decode)){
case 'array':
if(!count($decode) || array_keys($decode) === range(0, count($decode) - 1)) {
$keys = array();
foreach($decode as $value) {
if(($value = FastJSON::encode($value)) !== '')
array_push($keys, $value);
}
$result = '['.implode(',', $keys).']';
}
else
$result = FastJSON::convert($decode);
break;
case 'string':
$replacement = FastJSON::__getStaticReplacement();
$result = '"'.str_replace($replacement['find'], $replacement['replace'], $decode).'"';
break;
default:
if(!is_callable($decode))
$result = FastJSON::convert($decode);
break;
}
return $result;
}
// private methods, uncommented, sorry
static private function __getStaticReplacement(){
static $replacement = array('find'=>array(), 'replace'=>array());
if($replacement['find'] == array()) {
foreach(array_merge(range(0, 7), array(11), range(14, 31)) as $v) {
$replacement['find'][] = chr($v);
$replacement['replace'][] = "\\u00".sprintf("%02x", $v);
}
$replacement['find'] = array_merge(array(chr(0x5c), chr(0x2F), chr(0x22), chr(0x0d), chr(0x0c), chr(0x0a), chr(0x09), chr(0x08)), $replacement['find']);
$replacement['replace'] = array_merge(array('\\\\', '\\/', '\\"', '\r', '\f', '\n', '\t', '\b'), $replacement['replace']);
}
return $replacement;
}
static private function __decode($encode, $pos, $slen, $stdClass){
switch($encode{$pos}) {
case 't':
$result = true;
$pos += 4;
break;
case 'f':
$result = false;
$pos += 5;
break;
case 'n':
$result = null;
$pos += 4;
break;
case '[':
$result = array();
++$pos;
while($encode{$pos} !== ']') {
array_push($result, FastJSON::__decode($encode, $pos, $slen, $stdClass));
if($encode{$pos} === ',')
++$pos;
}
++$pos;
break;
case '{':
$result = $stdClass ? new stdClass : array();
++$pos;
while($encode{$pos} !== '}') {
$tmp = FastJSON::__decodeString($encode, $pos);
//$tmp = FastJSON::__decode($encode, $pos, $slen, $stdClass);
++$pos;
if($stdClass)
$result->$tmp = FastJSON::__decode($encode, $pos, $slen, $stdClass);
else
$result[$tmp] = FastJSON::__decode($encode, $pos, $slen, $stdClass);
if($encode{$pos} === ',')
++$pos;
}
++$pos;
break;
case '"':
switch($encode{++$pos}) {
case '"':
$result = "";
break;
default:
$result = FastJSON::__decodeString($encode, $pos);
break;
}
++$pos;
break;
default:
$tmp = '';
preg_replace('/^(\-)?([0-9]+)(\.[0-9]+)?([eE]\+[0-9]+)?/e', '$tmp = "\\1\\2\\3\\4"', substr($encode, $pos));
if($tmp !== '') {
$pos += strlen($tmp);
$nint = intval($tmp);
$nfloat = floatval($tmp);
$result = $nfloat == $nint ? $nint : $nfloat;
}
break;
}
return $result;
}
static private function __decodeString(&$encode, &$pos) {
$replacement = FastJSON::__getStaticReplacement();
$endString = FastJSON::__endString($encode, $pos, $pos);
$result = str_replace($replacement['replace'], $replacement['find'], substr($encode, $pos, $endString));
$pos += $endString;
return $result;
}
static private function __endString(&$encode, $position, &$pos) {
do {
$position = strpos($encode, '"', $position + 1);
}while($position !== false && FastJSON::__slashedChar($encode, $position - 1));
if($position === false)
trigger_error('', E_USER_WARNING);
return $position - $pos;
}
static private function __exit($str, $a, $b) {
exit($a.'FATAL: FastJSON decode method failure [malicious or incorrect JSON string]');
}
static private function __slashedChar(&$encode, $position) {
$pos = 0;
while($encode{$position--} === '\\')
$pos++;
return $pos % 2;
}
}
?>