webmcp

view libraries/json/json.c @ 127:83aced09adc7

New function json.type(...)
author jbe
date Sun Jul 27 14:38:00 2014 +0200 (2014-07-27)
parents bccaa05aada7
children c507f8d62931
line source
1 #include <lua.h>
2 #include <lauxlib.h>
3 #include <stdlib.h>
4 #include <string.h>
6 #define JSON_UPVAL_TYPES lua_upvalueindex(1)
7 #define JSON_UPVAL_NULLS lua_upvalueindex(2)
8 #define JSON_UPVAL_METATABLE lua_upvalueindex(3)
10 #define JSON_STATE_VALUE 0
11 #define JSON_STATE_OBJECT_KEY 1
12 #define JSON_STATE_OBJECT_KEY_TERMINATOR 2
13 #define JSON_STATE_OBJECT_VALUE 3
14 #define JSON_STATE_OBJECT_SEPARATOR 4
15 #define JSON_STATE_ARRAY_VALUE 5
16 #define JSON_STATE_ARRAY_SEPARATOR 6
17 #define JSON_STATE_END 7
19 static int json_import(lua_State *L) {
20 const char *str;
21 size_t total;
22 size_t pos = 0;
23 size_t level = 0;
24 int mode = JSON_STATE_VALUE;
25 char c;
26 luaL_Buffer luabuf;
27 char *cbuf;
28 size_t writepos;
29 int aryidx;
30 lua_settop(L, 1);
31 str = lua_tostring(L, 1);
32 total = strlen(str);
33 json_import_loop:
34 while (c = str[pos], c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == '\f') pos++;
35 switch (c) {
36 case 0:
37 if (mode == JSON_STATE_END) return 1;
38 json_import_unexpected_eof:
39 lua_pushnil(L);
40 if (level == 0) lua_pushliteral(L, "Empty string");
41 else lua_pushliteral(L, "Unexpected end of JSON document");
42 return 2;
43 case '{':
44 if (mode != JSON_STATE_VALUE && mode != JSON_STATE_OBJECT_VALUE && mode != JSON_STATE_ARRAY_VALUE)
45 goto json_import_syntax_error;
46 pos++;
47 lua_newtable(L); // the actual JSON object
48 lua_pushvalue(L, JSON_UPVAL_METATABLE);
49 lua_setmetatable(L, -2);
50 lua_pushvalue(L, -1);
51 lua_pushliteral(L, "object");
52 lua_rawset(L, JSON_UPVAL_TYPES);
53 lua_newtable(L); // stores set of NULL values
54 lua_pushvalue(L, -2);
55 lua_pushvalue(L, -2);
56 lua_rawset(L, JSON_UPVAL_NULLS);
57 mode = JSON_STATE_OBJECT_KEY;
58 level++;
59 goto json_import_loop;
60 case '[':
61 if (mode != JSON_STATE_VALUE && mode != JSON_STATE_OBJECT_VALUE && mode != JSON_STATE_ARRAY_VALUE)
62 goto json_import_syntax_error;
63 pos++;
64 lua_newtable(L); // the actual JSON array
65 lua_pushvalue(L, JSON_UPVAL_METATABLE);
66 lua_setmetatable(L, -2);
67 lua_pushvalue(L, -1);
68 lua_pushliteral(L, "array");
69 lua_rawset(L, JSON_UPVAL_TYPES);
70 lua_newtable(L); // stores set of NULL values
71 lua_pushvalue(L, -2);
72 lua_pushvalue(L, -2);
73 lua_rawset(L, JSON_UPVAL_NULLS);
74 lua_pushinteger(L, 0); // length of array (since it may contain nil's)
75 mode = JSON_STATE_ARRAY_VALUE;
76 level++;
77 goto json_import_loop;
78 case '}':
79 if (mode != JSON_STATE_OBJECT_KEY && mode != JSON_STATE_OBJECT_SEPARATOR)
80 goto json_import_syntax_error;
81 goto json_import_close;
82 case ']':
83 if (mode != JSON_STATE_ARRAY_VALUE && mode != JSON_STATE_ARRAY_SEPARATOR)
84 goto json_import_syntax_error;
85 lua_pop(L, 1); // pop length information
86 json_import_close:
87 pos++;
88 lua_pop(L, 1); // pop table that stores set of NULL values
89 if (--level) {
90 if (lua_type(L, -2) == LUA_TNUMBER) {
91 mode = JSON_STATE_ARRAY_VALUE;
92 } else {
93 mode = JSON_STATE_OBJECT_VALUE;
94 }
95 goto json_import_process_value;
96 } else {
97 mode = JSON_STATE_END;
98 }
99 goto json_import_loop;
100 case ':':
101 if (mode != JSON_STATE_OBJECT_KEY_TERMINATOR)
102 goto json_import_syntax_error;
103 pos++;
104 mode = JSON_STATE_OBJECT_VALUE;
105 goto json_import_loop;
106 case ',':
107 if (mode == JSON_STATE_OBJECT_SEPARATOR) {
108 mode = JSON_STATE_OBJECT_KEY;
109 } else if (mode == JSON_STATE_ARRAY_SEPARATOR) {
110 mode = JSON_STATE_ARRAY_VALUE;
111 } else {
112 goto json_import_syntax_error;
113 }
114 pos++;
115 goto json_import_loop;
116 case '"':
117 cbuf = luaL_buffinitsize(L, &luabuf, total-pos);
118 writepos = 0;
119 pos++;
120 while ((c = str[pos++]) != '"') {
121 if (c == 0) {
122 goto json_import_unexpected_eof;
123 } else if (c < 32 || c == 127) {
124 lua_pushnil(L);
125 lua_pushliteral(L, "Unexpected control character in JSON string");
126 return 2;
127 } else if (c == '\\') {
128 c = str[pos++];
129 switch (c) {
130 case 0:
131 goto json_import_unexpected_eof;
132 case '"':
133 case '/':
134 case '\\':
135 cbuf[writepos++] = c;
136 break;
137 case 'b':
138 cbuf[writepos++] = '\b';
139 break;
140 case 'f':
141 cbuf[writepos++] = '\f';
142 break;
143 case 'n':
144 cbuf[writepos++] = '\n';
145 break;
146 case 'r':
147 cbuf[writepos++] = '\r';
148 break;
149 case 't':
150 cbuf[writepos++] = '\t';
151 break;
152 case 'u':
153 lua_pushnil(L);
154 lua_pushliteral(L, "JSON unicode escape sequences are not implemented yet"); // TODO
155 return 2;
156 default:
157 lua_pushnil(L);
158 lua_pushliteral(L, "Unexpected string escape sequence in JSON document");
159 return 2;
160 }
161 } else {
162 cbuf[writepos++] = c;
163 }
164 }
165 if (!c) goto json_import_unexpected_eof;
166 luaL_pushresultsize(&luabuf, writepos);
167 goto json_import_process_value;
168 }
169 if (c == '-' || (c >= '0' && c <= '9')) {
170 char *endptr;
171 double numval;
172 numval = strtod(str+pos, &endptr);
173 if (endptr == str+pos) goto json_import_syntax_error;
174 pos += endptr - (str+pos);
175 lua_pushnumber(L, numval);
176 } else if (!strncmp(str+pos, "true", 4)) {
177 lua_pushboolean(L, 1);
178 pos += 4;
179 } else if (!strncmp(str+pos, "false", 5)) {
180 lua_pushboolean(L, 0);
181 pos += 5;
182 } else if (!strncmp(str+pos, "null", 4)) {
183 lua_pushnil(L);
184 pos += 4;
185 } else {
186 goto json_import_syntax_error;
187 }
188 json_import_process_value:
189 switch (mode) {
190 case JSON_STATE_OBJECT_KEY:
191 if (lua_type(L, -1) != LUA_TSTRING) goto json_import_syntax_error;
192 mode = JSON_STATE_OBJECT_KEY_TERMINATOR;
193 goto json_import_loop;
194 case JSON_STATE_OBJECT_VALUE:
195 if (lua_isnil(L, -1)) {
196 lua_pushvalue(L, -2);
197 lua_pushboolean(L, 1);
198 lua_rawset(L, -5);
199 }
200 lua_rawset(L, -4);
201 mode = JSON_STATE_OBJECT_SEPARATOR;
202 goto json_import_loop;
203 case JSON_STATE_ARRAY_VALUE:
204 aryidx = lua_tointeger(L, -2) + 1;
205 if (lua_isnil(L, -1)) {
206 lua_pushinteger(L, aryidx);
207 lua_pushboolean(L, 1);
208 lua_rawset(L, -5);
209 }
210 lua_rawseti(L, -4, aryidx);
211 lua_pop(L, 1);
212 lua_pushinteger(L, aryidx);
213 mode = JSON_STATE_ARRAY_SEPARATOR;
214 goto json_import_loop;
215 case JSON_STATE_VALUE:
216 mode = JSON_STATE_END;
217 goto json_import_loop;
218 }
219 json_import_syntax_error:
220 lua_pushnil(L);
221 lua_pushliteral(L, "Syntax error in JSON document");
222 return 2;
223 }
225 static int json_arylen(lua_State *L) {
226 int lower, middle;
227 int upper = 1;
228 lua_settop(L, 1);
229 lua_pushvalue(L, 1);
230 lua_rawget(L, JSON_UPVAL_NULLS);
231 if (lua_isnil(L, 2)) goto json_arylen_default;
232 lua_pushnil(L);
233 if (!lua_next(L, 2)) goto json_arylen_default;
234 lua_settop(L, 2);
235 while (1) {
236 lua_rawgeti(L, 1, upper);
237 if (lua_isnil(L, -1)) {
238 lua_pop(L, 1);
239 lua_pushinteger(L, upper);
240 lua_rawget(L, 2);
241 }
242 if (lua_isnil(L, -1)) break;
243 lua_pop(L, 1);
244 upper *= 2;
245 // TODO: avoid integer overflow!
246 }
247 lua_pop(L, 1);
248 lower = upper / 2;
249 while (upper - lower > 1) {
250 middle = (lower + upper) / 2;
251 lua_rawgeti(L, 1, middle);
252 if (lua_isnil(L, -1)) {
253 lua_pop(L, 1);
254 lua_pushinteger(L, middle);
255 lua_rawget(L, 2);
256 }
257 if (lua_isnil(L, -1)) upper = middle;
258 else lower = middle;
259 lua_pop(L, 1);
260 }
261 lua_pushinteger(L, lower);
262 return 1;
263 json_arylen_default:
264 lua_pushinteger(L, lua_rawlen(L, 1));
265 return 1;
266 }
268 static int json_type(lua_State *L) {
269 lua_settop(L, 1);
270 lua_rawget(L, JSON_UPVAL_TYPES);
271 return 1;
272 }
274 static int json_isnull(lua_State *L) {
275 lua_settop(L, 2);
276 lua_getmetatable(L, 1);
277 if (!lua_rawequal(L, -1, JSON_UPVAL_METATABLE)) goto json_isnull_false;
278 lua_pushvalue(L, 1);
279 lua_rawget(L, JSON_UPVAL_NULLS);
280 if (lua_isnil(L, -1)) goto json_isnull_false;
281 lua_pushvalue(L, 2);
282 lua_rawget(L, -2);
283 if (!lua_isnil(L, -1)) return 1;
284 json_isnull_false:
285 lua_pushboolean(L, 0);
286 return 1;
287 }
289 static const struct luaL_Reg json_module_functions[] = {
290 {"import", json_import},
291 {"type", json_type},
292 {"isnull", json_isnull},
293 {NULL, NULL}
294 };
296 static const struct luaL_Reg json_metatable_functions[] = {
297 {"__len", json_arylen},
298 {NULL, NULL}
299 };
301 int luaopen_json(lua_State *L) {
302 lua_settop(L, 0);
303 lua_newtable(L); // 1: library table on stack position
304 lua_newtable(L); // 2: ephemeron table to store the type of the JSON object/array
305 lua_newtable(L); // 3: ephemeron table to store a set of keys associated with JSON-null values
306 lua_newtable(L); // 4: metatable for ephemeron tables
307 lua_pushliteral(L, "__mode");
308 lua_pushliteral(L, "k");
309 lua_rawset(L, 4);
310 lua_pushvalue(L, 4); // 5: cloned metatable reference
311 lua_setmetatable(L, 2);
312 lua_setmetatable(L, 3);
313 lua_newtable(L); // 4: metatable for JSON objects and JSON arrays
314 lua_pushvalue(L, 4);
315 lua_setfield(L, 1, "metatable");
316 lua_pushvalue(L, 2);
317 lua_pushvalue(L, 3);
318 lua_pushvalue(L, 4);
319 luaL_setfuncs(L, json_metatable_functions, 3);
320 luaL_setfuncs(L, json_module_functions, 3);
321 return 1;
322 }

Impressum / About Us