forked from ress/public.parser.json3
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmodule.pmod.in
200 lines (177 loc) · 3.74 KB
/
module.pmod.in
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
//! @ignore
constant __author = "Christian Ress <[email protected]>";
constant __version = "1.0";
constant __components = ({ "Public.pmod/Parser.pmod/JSON3.pmod/module.pmod" });
//! @endignore
#define SMB(x) #x : (x)
private class myHandler {
private mapping(string:function) allowedSymbols =
([
SMB(`-),
SMB(aggregate_mapping),
SMB(aggregate),
SMB(allocate)
]);
mapping(string:mixed)|object get_default_module() {
return ([ ]);
}
mixed resolv(string symbol, string filename, object handler) {
if (zero_type(allowedSymbols[symbol]))
return UNDEFINED;
else
return allowedSymbols[symbol];
}
}
private int|array convert_to_pike(array|string s) {
array(string) tokens;
// split string into tokens
if (stringp(s))
tokens = Parser.C.split(s);
else
tokens = s;
// check if first character is a valid json-token starting character
int n = sizeof(tokens);
for(int i = 0; i < n; ++i) {
switch(tokens[i][0]) {
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
case '"':
case '-':
case '+':
case ':':
case ',':
break;
#if 0
case '+':
tokens[i] = "0" + tokens[i];
break;
#endif
case '{': // JSON object, Pike mapping
tokens[i] = "([";
break;
case '}':
tokens[i] = "])";
break;
case '[': // JSON array, Pike array
tokens[i] = "({";
break;
case ']':
tokens[i] = "})";
break;
case ' ':
case '\t':
case '\n':
case '\r':
tokens[i] = "";
break;
default:
if (tokens[i] == "null") {
tokens[i] = "UNDEFINED";
break;
}
else if (tokens[i] == "true") {
tokens[i] = "1";
break;
}
else if (tokens[i] == "false") {
tokens[i] = "0";
break;
}
return 0;
}
}
return tokens;
}
private int|array convert_to_json(array|string s) {
array(string) tokens;
// split string into tokens
if (stringp(s))
tokens = Parser.C.split(s);
else
tokens = s;
// check if first character is a valid pike-token starting character
int n = sizeof(tokens);
for(int i = 0; i < n; ++i) {
switch(tokens[i][0]) {
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
case '"':
case '-':
case '+':
case ':':
case ',':
break;
case '{': // JSON object, Pike mapping
tokens[i] = "[";
break;
case '}':
tokens[i] = "]";
break;
case '[': // JSON array, Pike array
tokens[i] = "{";
break;
case ']':
tokens[i] = "}";
break;
case ' ':
case '\t':
case '\n':
case '\r':
case '/':
case '(':
case ')':
tokens[i] = "";
break;
default:
if (tokens[i] == "null") {
tokens[i] = "UNDEFINED";
break;
}
else if (tokens[i] == "true") {
tokens[i] = "1";
break;
}
else if (tokens[i] == "false") {
tokens[i] = "0";
break;
}
write(">> returning because %O\n", tokens[i]);
return 0;
}
}
return tokens;
}
mixed decode(string s) {
array(string) tokens = Parser.C.split(s);
// check if input is valid
if ((tokens = convert_to_pike(tokens)) == 0)
error(sprintf("JSON.parse(%.10s) Invalid JSON serialization.", s));
program x = compile("mixed get() { return " + (tokens*"") + "; }", myHandler());
object o = x();
return o->get();
}
string encode(mixed var) {
array(string) tokens;
if (intp(var)) tokens = Parser.C.split(sprintf("%d", var));
else if (floatp(var)) tokens = Parser.C.split(sprintf("%f", var));
else tokens = Parser.C.split(sprintf("%O", var));
if ((tokens = convert_to_json(tokens)) == 0)
error(sprintf("JSON.serialize(%.10O) Cannot convert Pike datatype to JSON object.", var));
return tokens*"";
}