-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlib_json.php
155 lines (107 loc) · 3.56 KB
/
lib_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
<?
#
# this is a 'loose' JSON decoder. the built-in PHP JSON decoder
# is very strict, and will not accept things which are fairly
# common in the wild:
#
# * unquoted keys, e.g. {foo: 1}
# * single-quoted strings, e.g. {"foo": 'bar'}
# * escaped single quoted, e.g. {"foo": "b\'ar"}
# * empty array elements, e.g. [1,,2]
#
$GLOBALS['json_strings'] = array();
# these are used as placeholders. they must:
# 1) only contain alpha, numerics, underscore and dash
# 2) not exist in the actual json
$GLOBALS['json_str_prefix'] = 'JSON-STRING-XYZ';
$GLOBALS['json_slash_temp'] = 'JSON-SLASH-PAIR-XYZ';
function json_decode_loose($json){
$GLOBALS['json_strings'] = array();
#
# first find obvious strings
#
#echo "PRE-FIND: $json\n";
$json = preg_replace_callback('!"((?:[^\\\\"]|\\\\\\\\|\\\\")*)"!', 'json_dqs', $json);
$json = preg_replace_callback("!'((?:[^\\\\']|\\\\\\\\|\\\\')*)'!", 'json_sqs', $json);
#echo "POST-FIND: $json\n";
#print_r($GLOBALS['json_strings']);
$json = preg_replace('!\s+!', '', $json);
#
# missing elements
#
$json = str_replace('[,', '[null,', $json);
$json = str_replace('{,', '{', $json);
$json = str_replace(',]', ']', $json);
$json = str_replace(',}', '}', $json);
$json = preg_replace('!\[([^[{]+),(\s*),!', '[$1,null,', $json);
$json = preg_replace('!\{([^[{]+),(\s*),!', '{$1,', $json);
#
# quote unquoted key names
#
$json = preg_replace_callback('!([a-zA-Z0-9-_]+):!', 'json_key', $json);
#
# turn remaning barewords into nulls.
# loosely based on the ECMA spec, but avoiding requiring unicode PCRE.
# http://www.ecma-international.org/publications/files/ECMA-ST/Ecma-262.pdf
# (section 7.6)
#
$start = "[A-Za-z\$_]|(\\\\u[0-9A-Fa-f]{4})";
$continue = $start.'|[0-9-.]';
$json = preg_replace_callback("!(($start)($continue)*)!", 'json_bareword', $json);
#
# strip functions
#
$l = strlen($json);
while (1){
$json = preg_replace("!null\(([^()]*)\)!", 'null', $json);
$l2 = strlen($json);
if ($l == $l2) break;
$l = $l2;
}
#
# replace the strings
#
#echo "PRE-CONV: $json\n";
$pre = preg_quote($GLOBALS['json_str_prefix'], '!');
$json = preg_replace_callback('!'.$pre.'(\d+)!', 'json_strs', $json);
#echo "POST-CONV: $json\n";
$ret = JSON_decode($json, true);
if ($ret === null){
die("Failed to parse JSON:\n$json\n");
}
return $ret;
}
function json_dqs($m){
$idx = count($GLOBALS['json_strings']);
$GLOBALS['json_strings'][$idx] = $m[1];
return $GLOBALS['json_str_prefix'].$idx;
}
function json_sqs($m){
$text = str_replace("\\\\", $GLOBALS['json_slash_temp'], $m[1]);
$text = str_replace("\\'", "'", $text);
$text = str_replace('"', "\\\"", $text);
$text = str_replace($GLOBALS['json_slash_temp'], "\\\\", $text);
$idx = count($GLOBALS['json_strings']);
$GLOBALS['json_strings'][$idx] = $text;
return $GLOBALS['json_str_prefix'].$idx;
}
function json_strs($m){
return '"'.$GLOBALS['json_strings'][$m[1]].'"';
}
function json_key($m){
if (strpos($m[1], $GLOBALS['json_str_prefix']) === 0) return $m[0];
$idx = count($GLOBALS['json_strings']);
$GLOBALS['json_strings'][$idx] = $m[1];
return $GLOBALS['json_str_prefix'].$idx.':';
}
function json_bareword($m){
# just a string token
if (strpos($m[1], $GLOBALS['json_str_prefix']) === 0) return $m[0];
# reserved words we allow
$low = StrToLower($m[1]);
if ($low == 'null') return 'null';
if ($low == 'true') return 'true';
if ($low == 'false') return 'false';
# otherwise it's likely a variable reference, so remove it
return 'null';
}