webmcp

view libraries/json/json.c @ 161:d476b3c8960d

Speed up JSON library by better approximation of required buffer length when parsing string values
author jbe
date Thu Jul 31 13:22:35 2014 +0200 (2014-07-31)
parents d5e5e8a9b79a
children 3b8c1e2aef9c
line source
1 #include <lua.h>
2 #include <lauxlib.h>
3 #include <stdlib.h>
4 #include <string.h>
5 #include <math.h>
7 // maximum number of nested JSON values (objects and arrays):
8 // NOTE: The Lua reference states that the stack may typically contain at least
9 // "a few thousand elements". Since every nested level consumes
10 // 3 elements on the Lua stack (the object/array, its shadow table,
11 // a string key or a placeholder), we limit the number of nested levels
12 // to 500. If a stack overflow would still happen in the import function,
13 // this is detected nevertheless and an error is thrown (instead of
14 // returning nil and an error string).
15 #define JSON_MAXDEPTH 500
17 // generate dummy memory addresses that represents null values:
18 char json_nullmark;
19 #define json_isnullmark(L, i) (lua_touserdata((L), (i)) == &json_nullmark)
20 #define json_pushnullmark(L) lua_pushlightuserdata((L), &json_nullmark)
22 // macros for usage of Lua registry:
23 #define JSON_REGENT char
24 #define JSON_REGPOINTER void *
25 #define json_regpointer(x) (&json_registry.x)
26 #define json_regfetchpointer(L, x) lua_rawgetp((L), LUA_REGISTRYINDEX, (x))
27 #define json_regfetch(L, x) json_regfetchpointer(L, json_regpointer(x))
28 #define json_regstore(L, x) lua_rawsetp(L, LUA_REGISTRYINDEX, json_regpointer(x))
30 // generate dummy memory addresses that represent Lua objects
31 // via lightuserdata keys and LUA_REGISTRYINDEX:
32 static struct {
33 JSON_REGENT shadowtbl; // ephemeron table that maps tables to their corresponding shadow table
34 JSON_REGENT objectmt; // metatable for JSON objects
35 JSON_REGENT arraymt; // metatable for JSON arrays
36 } json_registry;
38 // returns the string "<JSON null marker>":
39 static int json_nullmark_tostring(lua_State *L) {
40 lua_pushliteral(L, "<JSON null marker>");
41 return 1;
42 }
44 // marks a Lua table as JSON object or JSON array:
45 // (returns its modified argument or a new table if argument is nil)
46 static int json_mark(lua_State *L, JSON_REGPOINTER mt) {
47 // check if argument is nil
48 if (lua_isnoneornil(L, 1)) {
49 // create new table at stack position 1:
50 lua_settop(L, 0);
51 lua_newtable(L);
52 // create shadow table (leaving previously created table on stack position 1):
53 json_regfetch(L, shadowtbl);
54 lua_pushvalue(L, 1);
55 lua_newtable(L);
56 lua_rawset(L, -3);
57 } else {
58 // push shadow table on top of stack:
59 json_regfetch(L, shadowtbl);
60 lua_pushvalue(L, 1);
61 lua_rawget(L, -2);
62 // if shadow table does not exist:
63 if (lua_isnil(L, -1)) {
64 // create shadow table and leave it on top of stack:
65 lua_newtable(L);
66 lua_pushvalue(L, 1);
67 lua_pushvalue(L, -2);
68 lua_rawset(L, -5);
69 }
70 // move elements from original table to shadow table (that's expected on top of stack):
71 for(lua_pushnil(L); lua_next(L, 1); lua_pop(L, 1)) {
72 lua_pushvalue(L, -2);
73 lua_pushnil(L);
74 lua_rawset(L, 1);
75 lua_pushvalue(L, -2);
76 lua_pushvalue(L, -2);
77 lua_rawset(L, -5);
78 }
79 }
80 // discard everything but table to return:
81 lua_settop(L, 1);
82 // set metatable:
83 json_regfetchpointer(L, mt);
84 lua_setmetatable(L, 1);
85 // return table:
86 return 1;
87 }
89 // marks a table as JSON object:
90 // (returns its modified argument or a new table if argument is nil)
91 static int json_object(lua_State *L) {
92 return json_mark(L, json_regpointer(objectmt));
93 }
95 // marks a table as JSON array:
96 // (returns its modified argument or a new table if argument is nil)
97 static int json_array(lua_State *L) {
98 return json_mark(L, json_regpointer(arraymt));
99 }
101 // internal states of JSON parser:
102 #define JSON_STATE_VALUE 0
103 #define JSON_STATE_OBJECT_KEY 1
104 #define JSON_STATE_OBJECT_KEY_TERMINATOR 2
105 #define JSON_STATE_OBJECT_VALUE 3
106 #define JSON_STATE_OBJECT_SEPARATOR 4
107 #define JSON_STATE_ARRAY_VALUE 5
108 #define JSON_STATE_ARRAY_SEPARATOR 6
109 #define JSON_STATE_END 7
111 // special Lua stack indicies for json_import function:
112 #define json_import_objectmt_idx 2
113 #define json_import_arraymt_idx 3
114 #define json_import_shadowtbl_idx 4
116 // decodes a JSON document:
117 static int json_import(lua_State *L) {
118 const char *str; // string to parse
119 size_t total; // total length of string to parse
120 size_t pos = 0; // current position in string to parse
121 size_t level = 0; // nested levels of objects/arrays currently being processed
122 int mode = JSON_STATE_VALUE; // state of parser (i.e. "what's expected next?")
123 char c; // variable to store a single character to be processed
124 luaL_Buffer luabuf; // Lua buffer to decode JSON string values
125 char *cbuf; // C buffer to decode JSON string values
126 size_t writepos; // write position of decoded strings in C buffer
127 size_t arraylen; // variable to temporarily store the array length
128 // stack shall contain one function argument:
129 lua_settop(L, 1);
130 // push objectmt onto stack position 2:
131 json_regfetch(L, objectmt);
132 // push arraymt onto stack position 3:
133 json_regfetch(L, arraymt);
134 // push shadowtbl onto stack position 4:
135 json_regfetch(L, shadowtbl);
136 // require string as first argument:
137 str = luaL_checklstring(L, 1, &total);
138 // if string contains a NULL byte, this is a syntax error
139 if (strlen(str) != total) goto json_import_syntax_error;
140 // main loop of parser:
141 json_import_loop:
142 // skip whitespace and store next character in variable 'c':
143 while (c = str[pos],
144 c == ' ' ||
145 c == '\f' ||
146 c == '\n' ||
147 c == '\r' ||
148 c == '\t' ||
149 c == '\v'
150 ) pos++;
151 // switch statement to handle certain (single) characters:
152 switch (c) {
153 // handle end of JSON document:
154 case 0:
155 // if end of JSON document was expected, then return top element of stack as result:
156 if (mode == JSON_STATE_END) return 1;
157 // otherwise, the JSON document was malformed:
158 json_import_unexpected_eof:
159 lua_pushnil(L);
160 if (level == 0) lua_pushliteral(L, "Empty string");
161 else lua_pushliteral(L, "Unexpected end of JSON document");
162 return 2;
163 // new JSON object:
164 case '{':
165 // if a JSON object is not expected here, then return an error:
166 if (
167 mode != JSON_STATE_VALUE &&
168 mode != JSON_STATE_OBJECT_VALUE &&
169 mode != JSON_STATE_ARRAY_VALUE
170 ) goto json_import_syntax_error;
171 // create JSON object on stack:
172 lua_newtable(L);
173 // set metatable of JSON object:
174 lua_pushvalue(L, json_import_objectmt_idx);
175 lua_setmetatable(L, -2);
176 // create internal shadow table on stack:
177 lua_newtable(L);
178 // register internal shadow table:
179 lua_pushvalue(L, -2);
180 lua_pushvalue(L, -2);
181 lua_rawset(L, json_import_shadowtbl_idx);
182 // expect object key (or end of object) to follow:
183 mode = JSON_STATE_OBJECT_KEY;
184 // jump to common code for opening JSON object and JSON array:
185 goto json_import_open;
186 // new JSON array:
187 case '[':
188 // if a JSON array is not expected here, then return an error:
189 if (
190 mode != JSON_STATE_VALUE &&
191 mode != JSON_STATE_OBJECT_VALUE &&
192 mode != JSON_STATE_ARRAY_VALUE
193 ) goto json_import_syntax_error;
194 // create JSON array on stack:
195 lua_newtable(L);
196 // set metatable of JSON array:
197 lua_pushvalue(L, json_import_arraymt_idx);
198 lua_setmetatable(L, -2);
199 // create internal shadow table on stack:
200 lua_newtable(L);
201 // register internal shadow table:
202 lua_pushvalue(L, -2);
203 lua_pushvalue(L, -2);
204 lua_rawset(L, json_import_shadowtbl_idx);
205 // add nil as key (needed to keep stack balance) and as magic to detect arrays:
206 lua_pushnil(L);
207 // expect array value (or end of array) to follow:
208 mode = JSON_STATE_ARRAY_VALUE;
209 // continue with common code for opening JSON object and JSON array:
210 // common code for opening JSON object or JSON array:
211 json_import_open:
212 // limit nested levels:
213 if (level >= JSON_MAXDEPTH) {
214 lua_pushnil(L);
215 lua_pushliteral(L, "Too many nested JSON levels");
216 return 2;
217 }
218 // additional buffer overflow protection:
219 if (!lua_checkstack(L, LUA_MINSTACK))
220 return luaL_error(L, "Caught stack overflow in JSON import function (too many nested levels and stack size too small)");
221 // increment level:
222 level++;
223 // consume input character:
224 pos++;
225 goto json_import_loop;
226 // end of JSON object:
227 case '}':
228 // if end of JSON object is not expected here, then return an error:
229 if (
230 mode != JSON_STATE_OBJECT_KEY &&
231 mode != JSON_STATE_OBJECT_SEPARATOR
232 ) goto json_import_syntax_error;
233 // jump to common code for end of JSON object and JSON array:
234 goto json_import_close;
235 // end of JSON array:
236 case ']':
237 // if end of JSON array is not expected here, then return an error:
238 if (
239 mode != JSON_STATE_ARRAY_VALUE &&
240 mode != JSON_STATE_ARRAY_SEPARATOR
241 ) goto json_import_syntax_error;
242 // pop nil key/magic (that was needed to keep stack balance):
243 lua_pop(L, 1);
244 // continue with common code for end of JSON object and JSON array:
245 // common code for end of JSON object or JSON array:
246 json_import_close:
247 // consume input character:
248 pos++;
249 // pop shadow table:
250 lua_pop(L, 1);
251 // check if nested:
252 if (--level) {
253 // if nested,
254 // check if outer(!) structure is an array or object:
255 if (lua_isnil(L, -2)) {
256 // select array value processing:
257 mode = JSON_STATE_ARRAY_VALUE;
258 } else {
259 // select object value processing:
260 mode = JSON_STATE_OBJECT_VALUE;
261 }
262 // store value in outer structure:
263 goto json_import_process_value;
264 }
265 // if not nested, then expect end of JSON document and continue with loop:
266 mode = JSON_STATE_END;
267 goto json_import_loop;
268 // key terminator:
269 case ':':
270 // if key terminator is not expected here, then return an error:
271 if (mode != JSON_STATE_OBJECT_KEY_TERMINATOR)
272 goto json_import_syntax_error;
273 // consume input character:
274 pos++;
275 // expect object value to follow:
276 mode = JSON_STATE_OBJECT_VALUE;
277 // continue with loop:
278 goto json_import_loop;
279 // value terminator (NOTE: trailing comma at end of value or key-value list is tolerated by this parser)
280 case ',':
281 // branch according to parser state:
282 if (mode == JSON_STATE_OBJECT_SEPARATOR) {
283 // expect an object key to follow:
284 mode = JSON_STATE_OBJECT_KEY;
285 } else if (mode == JSON_STATE_ARRAY_SEPARATOR) {
286 // expect an array value to follow:
287 mode = JSON_STATE_ARRAY_VALUE;
288 } else {
289 // if value terminator is not expected here, then return an error:
290 goto json_import_syntax_error;
291 }
292 // consume input character:
293 pos++;
294 // continue with loop:
295 goto json_import_loop;
296 // string literal:
297 case '"':
298 // consume quote character:
299 pos++;
300 // determine buffer length:
301 writepos = pos;
302 while ((c = str[writepos]) != '"') {
303 // consume one character:
304 writepos++;
305 // handle unexpected end of JSON document:
306 if (c == 0) goto json_import_unexpected_eof;
307 // consume one extra character when encountering an escaped quote:
308 else if (c == '\\' && str[writepos] == '"') writepos++;
309 }
310 writepos -= pos;
311 // check if string is non empty:
312 if (writepos) {
313 // prepare buffer to decode string (with maximum possible length) and set write position to zero:
314 cbuf = luaL_buffinitsize(L, &luabuf, writepos);
315 writepos = 0;
316 // loop through the characters until encountering end quote:
317 while ((c = str[pos++]) != '"') {
318 if (c == 0) {
319 // handle unexpected end of JSON document:
320 goto json_import_unexpected_eof;
321 } else if (c < 32 || c == 127) {
322 // do not allow ASCII control characters:
323 // NOTE: illegal UTF-8 sequences and extended control characters are not sanitized
324 // by this parser to allow different encodings than Unicode
325 lua_pushnil(L);
326 lua_pushliteral(L, "Unexpected control character in JSON string");
327 return 2;
328 } else if (c == '\\') {
329 // read next char after backslash escape:
330 c = str[pos++];
331 switch (c) {
332 // unexpected end-of-string:
333 case 0:
334 goto json_import_unexpected_eof;
335 // unescaping of quotation mark, slash, and backslash:
336 case '"':
337 case '/':
338 case '\\':
339 cbuf[writepos++] = c;
340 break;
341 // unescaping of backspace:
342 case 'b': cbuf[writepos++] = '\b'; break;
343 // unescaping of form-feed:
344 case 'f': cbuf[writepos++] = '\f'; break;
345 // unescaping of new-line:
346 case 'n': cbuf[writepos++] = '\n'; break;
347 // unescaping of carriage-return:
348 case 'r': cbuf[writepos++] = '\r'; break;
349 // unescaping of tabulator:
350 case 't': cbuf[writepos++] = '\t'; break;
351 // unescaping of UTF-16 characters
352 case 'u':
353 lua_pushnil(L);
354 lua_pushliteral(L, "JSON unicode escape sequences are not implemented yet"); // TODO
355 return 2;
356 // unexpected escape sequence:
357 default:
358 lua_pushnil(L);
359 lua_pushliteral(L, "Unexpected string escape sequence in JSON document");
360 return 2;
361 }
362 } else {
363 // normal character:
364 cbuf[writepos++] = c;
365 }
366 }
367 // process buffer to Lua string:
368 luaL_pushresultsize(&luabuf, writepos);
369 } else {
370 // if JSON string is empty,
371 // push empty Lua string:
372 lua_pushliteral(L, "");
373 }
374 // continue with processing of decoded string:
375 goto json_import_process_value;
376 }
377 // process values whose type is is not deducible from a single character:
378 if ((c >= '0' && c <= '9') || c == '-' || c == '+') {
379 // for numbers,
380 // use strtod() call to parse a (double precision) floating point number:
381 char *endptr;
382 double numval;
383 numval = strtod(str+pos, &endptr);
384 // catch parsing errors:
385 if (endptr == str+pos) goto json_import_syntax_error;
386 // consume characters that were parsed:
387 pos += endptr - (str+pos);
388 // push parsed (double precision) floating point number on Lua stack:
389 lua_pushnumber(L, numval);
390 } else if (!strncmp(str+pos, "true", 4)) {
391 // consume 4 input characters for "true":
392 pos += 4;
393 // put Lua true value onto stack:
394 lua_pushboolean(L, 1);
395 } else if (!strncmp(str+pos, "false", 5)) {
396 // consume 5 input characters for "false":
397 pos += 5;
398 // put Lua false value onto stack:
399 lua_pushboolean(L, 0);
400 } else if (!strncmp(str+pos, "null", 4)) {
401 // consume 4 input characters for "null":
402 pos += 4;
403 // different behavor for top-level and sub-levels:
404 if (level) {
405 // if sub-level,
406 // push special null-marker onto stack:
407 json_pushnullmark(L);
408 } else {
409 // if top-level,
410 // push nil onto stack:
411 lua_pushnil(L);
412 }
413 } else {
414 // all other cases are a syntax error:
415 goto json_import_syntax_error;
416 }
417 // process a decoded value or key value pair (expected on top of Lua stack):
418 json_import_process_value:
419 switch (mode) {
420 // an object key has been read:
421 case JSON_STATE_OBJECT_KEY:
422 // if an object key is not a string, then this is a syntax error:
423 if (lua_type(L, -1) != LUA_TSTRING) goto json_import_syntax_error;
424 // expect key terminator to follow:
425 mode = JSON_STATE_OBJECT_KEY_TERMINATOR;
426 // continue with loop:
427 goto json_import_loop;
428 // a key value pair has been read:
429 case JSON_STATE_OBJECT_VALUE:
430 // store key value pair in outer shadow table:
431 lua_rawset(L, -3);
432 // expect value terminator (or end of object) to follow:
433 mode = JSON_STATE_OBJECT_SEPARATOR;
434 // continue with loop:
435 goto json_import_loop;
436 // an array value has been read:
437 case JSON_STATE_ARRAY_VALUE:
438 // get current array length:
439 arraylen = lua_rawlen(L, -3);
440 // throw error if array would exceed INT_MAX elements:
441 // TODO: Lua 5.3 may support more elements
442 if (arraylen >= INT_MAX) {
443 lua_pushnil(L);
444 lua_pushfstring(L, "Array exceeded length of %d elements", INT_MAX);
445 }
446 // store value in outer shadow table:
447 lua_rawseti(L, -3, arraylen + 1);
448 // expect value terminator (or end of object) to follow:
449 mode = JSON_STATE_ARRAY_SEPARATOR;
450 // continue with loop
451 goto json_import_loop;
452 // a single value has been read:
453 case JSON_STATE_VALUE:
454 // leave value on top of stack, expect end of JSON document, and continue with loop:
455 mode = JSON_STATE_END;
456 goto json_import_loop;
457 }
458 // syntax error handling (reachable by goto statement):
459 json_import_syntax_error:
460 lua_pushnil(L);
461 lua_pushliteral(L, "Syntax error in JSON document");
462 return 2;
463 }
465 // special Lua stack indicies for json_path function:
466 #define json_path_shadowtbl_idx 1
468 // stack offset of arguments to json_path function:
469 #define json_path_idxshift 1
471 // gets a value or its type from a JSON document (passed as first argument)
472 // using a path (passed as variable number of keys after first argument):
473 static int json_path(lua_State *L, int type_mode) {
474 int stacktop; // stack index of top of stack (after shifting)
475 int idx = 2 + json_path_idxshift; // stack index of current argument to process
476 // insert shadowtbl into stack at position 1 (shifting the arguments):
477 json_regfetch(L, shadowtbl);
478 lua_insert(L, 1);
479 // store stack index of top of stack:
480 stacktop = lua_gettop(L);
481 // use first argument as "current value" (stored on top of stack):
482 lua_pushvalue(L, 1 + json_path_idxshift);
483 // process each "path key" (2nd argument and following arguments):
484 while (idx <= stacktop) {
485 // if "current value" (on top of stack) is nil, then the path cannot be walked and nil is returned:
486 if (lua_isnil(L, -1)) return 1;
487 // try to get shadow table of "current value":
488 lua_pushvalue(L, -1);
489 lua_rawget(L, json_path_shadowtbl_idx);
490 if (lua_isnil(L, -1)) {
491 // if no shadow table is found,
492 if (lua_type(L, -1) == LUA_TTABLE) {
493 // and if "current value" is a table,
494 // drop nil from stack:
495 lua_pop(L, 1);
496 // get "next value" using the "path key":
497 lua_pushvalue(L, idx++);
498 lua_gettable(L, -2);
499 } else {
500 // if "current value" is not a table,
501 // then the path cannot be walked and nil (already on top of stack) is returned:
502 return 1;
503 }
504 } else {
505 // if a shadow table is found,
506 // set "current value" to its shadow table:
507 lua_replace(L, -2);
508 // get "next value" using the "path key":
509 lua_pushvalue(L, idx++);
510 lua_rawget(L, -2);
511 }
512 // the "next value" replaces the "current value":
513 lua_replace(L, -2);
514 }
515 if (!type_mode) {
516 // if a value (and not its type) was requested,
517 // check if value is the null-marker, and store nil on top of Lua stack in that case:
518 if (json_isnullmark(L, -1)) lua_pushnil(L);
519 } else {
520 // if the type was requested,
521 // check if value is the null-marker:
522 if (json_isnullmark(L, -1)) {
523 // if yes, store string "null" on top of Lua stack:
524 lua_pushliteral(L, "null");
525 } else {
526 // otherwise,
527 // check if metatable indicates "object" or "array":
528 if (lua_getmetatable(L, -1)) {
529 json_regfetch(L, objectmt);
530 if (lua_rawequal(L, -2, -1)) {
531 // if value has metatable for JSON objects,
532 // return string "object":
533 lua_pushliteral(L, "object");
534 return 1;
535 }
536 json_regfetch(L, arraymt);
537 if (lua_rawequal(L, -3, -1)) {
538 // if value has metatable for JSON arrays,
539 // return string "object":
540 lua_pushliteral(L, "array");
541 return 1;
542 }
543 // remove 3 metatables (one of the value, two for comparison) from stack:
544 lua_pop(L, 3);
545 }
546 // otherwise, get the Lua type:
547 lua_pushstring(L, lua_typename(L, lua_type(L, -1)));
548 }
549 }
550 // return the top most value on the Lua stack:
551 return 1;
552 }
554 // gets a value from a JSON document (passed as first argument)
555 // using a path (passed as variable number of keys after first argument):
556 static int json_get(lua_State *L) {
557 return json_path(L, 0);
558 }
560 // gets a value's type from a JSON document (passed as first argument)
561 // using a path (variable number of keys after first argument):
562 static int json_type(lua_State *L) {
563 return json_path(L, 1);
564 }
566 // returns the length of a JSON array (or zero for a table without numeric keys):
567 static int json_len(lua_State *L) {
568 // stack shall contain one function argument:
569 lua_settop(L, 1);
570 // try to get corresponding shadow table for first argument:
571 json_regfetch(L, shadowtbl);
572 lua_pushvalue(L, 1);
573 lua_rawget(L, -2);
574 // if shadow table does not exist, return length of argument, else length of shadow table:
575 lua_pushnumber(L, lua_rawlen(L, lua_isnil(L, -1) ? 1 : -1));
576 return 1;
577 }
579 static int json_index(lua_State *L) {
580 // stack shall contain two function arguments:
581 lua_settop(L, 2);
582 // get corresponding shadow table for first argument:
583 json_regfetch(L, shadowtbl);
584 lua_pushvalue(L, 1);
585 lua_rawget(L, -2);
586 // throw error if no shadow table was found:
587 if (lua_isnil(L, -1)) return luaL_error(L, "Shadow table not found");
588 // use key passed as second argument to lookup value in shadow table:
589 lua_pushvalue(L, 2);
590 lua_rawget(L, -2);
591 // if value is null-marker, then push nil onto stack:
592 if (json_isnullmark(L, -1)) lua_pushnil(L);
593 // return either looked up value, or nil
594 return 1;
595 }
597 static int json_newindex(lua_State *L) {
598 // stack shall contain three function arguments:
599 lua_settop(L, 3);
600 // get corresponding shadow table for first argument:
601 json_regfetch(L, shadowtbl);
602 lua_pushvalue(L, 1);
603 lua_rawget(L, -2);
604 // throw error if no shadow table was found:
605 if (lua_isnil(L, -1)) return luaL_error(L, "Shadow table not found");
606 // replace first argument with shadow table:
607 lua_replace(L, 1);
608 // reset stack and use second and third argument to write to shadow table:
609 lua_settop(L, 3);
610 lua_rawset(L, 1);
611 // return nothing:
612 return 0;
613 }
615 static int json_pairs_iterfunc(lua_State *L) {
616 // stack shall contain two function arguments:
617 lua_settop(L, 2);
618 // get corresponding shadow table for first argument:
619 json_regfetch(L, shadowtbl);
620 lua_pushvalue(L, 1);
621 lua_rawget(L, -2);
622 // throw error if no shadow table was found:
623 if (lua_isnil(L, -1)) return luaL_error(L, "Shadow table not found");
624 // get next key value pair from shadow table (using previous key from argument 2)
625 // and return nothing if there is no next pair:
626 lua_pushvalue(L, 2);
627 if (!lua_next(L, -2)) return 0;
628 // replace null-marker with nil:
629 if (json_isnullmark(L, -1)) {
630 lua_pop(L, 1);
631 lua_pushnil(L);
632 }
633 // return key and value (or key and nil, if null-marker was found):
634 return 2;
635 }
637 // returns a triple such that 'for key, value in pairs(obj) do ... end'
638 // iterates through all key value pairs (including JSON null keys represented as Lua nil):
639 static int json_pairs(lua_State *L) {
640 // return triple of function json_pairs_iterfunc, first argument, and nil:
641 lua_pushcfunction(L, json_pairs_iterfunc);
642 lua_pushvalue(L, 1);
643 lua_pushnil(L);
644 return 3;
645 }
647 static int json_ipairs_iterfunc(lua_State *L) {
648 lua_Integer idx;
649 // stack shall contain two function arguments:
650 lua_settop(L, 2);
651 // calculate new index by incrementing second argument:
652 idx = lua_tointeger(L, 2) + 1;
653 // get corresponding shadow table for first argument:
654 json_regfetch(L, shadowtbl);
655 lua_pushvalue(L, 1);
656 lua_rawget(L, -2);
657 // throw error if no shadow table was found:
658 if (lua_isnil(L, -1)) return luaL_error(L, "Shadow table not found");
659 // do integer lookup in shadow table:
660 lua_rawgeti(L, -1, idx);
661 // return nothing if there was no value:
662 if (lua_isnil(L, -1)) return 0;
663 // return new index and
664 // either the looked up value if it is not equal to the null-marker
665 // or nil instead of null-marker:
666 lua_pushinteger(L, idx);
667 if (json_isnullmark(L, -2)) lua_pushnil(L);
668 else lua_pushvalue(L, -2);
669 return 2;
670 }
672 // returns a triple such that 'for idx, value in ipairs(ary) do ... end'
673 // iterates through all values (including JSON null represented as Lua nil):
674 static int json_ipairs(lua_State *L) {
675 // return triple of function json_ipairs_iterfunc, first argument, and zero:
676 lua_pushcfunction(L, json_ipairs_iterfunc);
677 lua_pushvalue(L, 1);
678 lua_pushinteger(L, 0);
679 return 3;
680 }
682 #define JSON_TABLETYPE_UNKNOWN 0
683 #define JSON_TABLETYPE_OBJECT 1
684 #define JSON_TABLETYPE_ARRAY 2
686 static int json_export(lua_State *L) {
687 lua_Number num;
688 const char *str;
689 unsigned char c;
690 size_t strlen;
691 size_t pos = 0;
692 luaL_Buffer buf;
693 char hexcode[7]; // backslash, character 'u', 4 hex digits, and terminating NULL byte
694 int luatype;
695 int tabletype = JSON_TABLETYPE_UNKNOWN;
696 int needsep = 0;
697 lua_Integer idx;
698 lua_settop(L, 1);
699 if (json_isnullmark(L, 1)) {
700 lua_pushnil(L);
701 lua_replace(L, 1);
702 }
703 switch (lua_type(L, 1)) {
704 case LUA_TNIL:
705 lua_pushliteral(L, "null");
706 return 1;
707 case LUA_TNUMBER:
708 num = lua_tonumber(L, 1);
709 if (isnan(num)) return luaL_error(L, "JSON export not possible for NaN value");
710 if (isinf(num)) return luaL_error(L, "JSON export not possible for infinite numbers");
711 lua_tostring(L, 1);
712 return 1;
713 case LUA_TBOOLEAN:
714 if (lua_toboolean(L, 1)) lua_pushliteral(L, "true");
715 else lua_pushliteral(L, "false");
716 return 1;
717 case LUA_TSTRING:
718 str = lua_tolstring(L, 1, &strlen);
719 luaL_buffinit(L, &buf);
720 luaL_addchar(&buf, '"');
721 while (pos < strlen) {
722 c = str[pos++];
723 if (c == '"') luaL_addstring(&buf, "\\\"");
724 else if (c == '\\') luaL_addstring(&buf, "\\\\");
725 else if (c == 127) luaL_addstring(&buf, "\\u007F");
726 else if (c >= 32) luaL_addchar(&buf, c);
727 else if (c == '\b') luaL_addstring(&buf, "\\b");
728 else if (c == '\f') luaL_addstring(&buf, "\\f");
729 else if (c == '\n') luaL_addstring(&buf, "\\n");
730 else if (c == '\r') luaL_addstring(&buf, "\\r");
731 else if (c == '\t') luaL_addstring(&buf, "\\t");
732 else if (c == '\v') luaL_addstring(&buf, "\\v");
733 else {
734 sprintf(hexcode, "\\u%04X", c);
735 luaL_addstring(&buf, hexcode);
736 }
737 }
738 luaL_addchar(&buf, '"');
739 luaL_pushresult(&buf);
740 return 1;
741 case LUA_TTABLE:
742 if (lua_getmetatable(L, 1)) {
743 json_regfetch(L, objectmt);
744 if (lua_rawequal(L, -2, -1)) {
745 tabletype = JSON_TABLETYPE_OBJECT;
746 } else {
747 json_regfetch(L, arraymt);
748 if (lua_rawequal(L, -3, -1)) tabletype = JSON_TABLETYPE_ARRAY;
749 }
750 }
751 json_regfetch(L, shadowtbl);
752 lua_pushvalue(L, 1);
753 lua_rawget(L, -2);
754 if (!lua_isnil(L, -1)) lua_replace(L, 1);
755 lua_settop(L, 1);
756 if (tabletype == JSON_TABLETYPE_UNKNOWN) {
757 for (lua_pushnil(L); lua_next(L, 1); lua_pop(L, 1)) {
758 luatype = lua_type(L, -2);
759 if (tabletype == JSON_TABLETYPE_UNKNOWN) {
760 if (luatype == LUA_TSTRING) tabletype = JSON_TABLETYPE_OBJECT;
761 else if (luatype == LUA_TNUMBER) tabletype = JSON_TABLETYPE_ARRAY;
762 } else if (
763 (tabletype == JSON_TABLETYPE_OBJECT && luatype == LUA_TNUMBER) ||
764 (tabletype == JSON_TABLETYPE_ARRAY && luatype == LUA_TSTRING)
765 ) {
766 goto json_export_tabletype_error;
767 }
768 }
769 }
770 switch (tabletype) {
771 case JSON_TABLETYPE_OBJECT:
772 lua_settop(L, 3);
773 luaL_buffinit(L, &buf);
774 luaL_addchar(&buf, '{');
775 for (lua_pushnil(L); lua_next(L, 1); ) {
776 if (lua_type(L, -2) == LUA_TSTRING) {
777 lua_replace(L, 3);
778 lua_replace(L, 2);
779 if (needsep) luaL_addchar(&buf, ',');
780 else needsep = 1;
781 lua_pushcfunction(L, json_export);
782 lua_pushvalue(L, 2);
783 lua_call(L, 1, 1);
784 luaL_addvalue(&buf);
785 luaL_addchar(&buf, ':');
786 if (json_isnullmark(L, 3)) {
787 luaL_addstring(&buf, "null");
788 } else {
789 lua_pushcfunction(L, json_export);
790 lua_pushvalue(L, 3);
791 lua_call(L, 1, 1);
792 luaL_addvalue(&buf);
793 }
794 lua_pushvalue(L, 2);
795 } else {
796 lua_pop(L, 1);
797 }
798 }
799 luaL_addchar(&buf, '}');
800 luaL_pushresult(&buf);
801 return 1;
802 case JSON_TABLETYPE_ARRAY:
803 lua_settop(L, 2);
804 luaL_buffinit(L, &buf);
805 luaL_addchar(&buf, '[');
806 for (idx = 1; ; idx++) {
807 lua_rawgeti(L, 1, idx);
808 if (lua_isnil(L, -1)) {
809 lua_pop(L, 1);
810 break;
811 }
812 lua_replace(L, 2);
813 if (needsep) luaL_addchar(&buf, ',');
814 else needsep = 1;
815 lua_pushcfunction(L, json_export);
816 lua_pushvalue(L, 2);
817 lua_call(L, 1, 1);
818 luaL_addvalue(&buf);
819 }
820 luaL_addchar(&buf, ']');
821 luaL_pushresult(&buf);
822 return 1;
823 }
824 json_export_tabletype_error:
825 return luaL_error(L, "JSON export not possible for ambiguous table (cannot decide whether it is an object or array)");
826 }
827 return luaL_error(L, "JSON export not possible for values of type \"%s\"", lua_typename(L, lua_type(L, 1)));
828 }
830 // functions in library module:
831 static const struct luaL_Reg json_module_functions[] = {
832 {"object", json_object},
833 {"array", json_array},
834 {"import", json_import},
835 {"export", json_export},
836 {"get", json_get},
837 {"type", json_type},
838 {NULL, NULL}
839 };
841 // metamethods for JSON objects, JSON arrays, and unknown JSON collections (object or array):
842 static const struct luaL_Reg json_metatable_functions[] = {
843 {"__len", json_len},
844 {"__index", json_index},
845 {"__newindex", json_newindex},
846 {"__pairs", json_pairs},
847 {"__ipairs", json_ipairs},
848 {"__tostring", json_export},
849 {NULL, NULL}
850 };
852 // metamethods for JSON null marker:
853 static const struct luaL_Reg json_nullmark_metamethods[] = {
854 {"__tostring", json_nullmark_tostring},
855 {NULL, NULL}
856 };
858 // initializes json library:
859 int luaopen_json(lua_State *L) {
860 // empty stack:
861 lua_settop(L, 0);
862 // push library module onto stack position 1:
863 lua_newtable(L);
864 // register library functions:
865 luaL_setfuncs(L, json_module_functions, 0);
866 // create and store objectmt:
867 lua_newtable(L);
868 luaL_setfuncs(L, json_metatable_functions, 0);
869 json_regstore(L, objectmt);
870 // create and store arraymt:
871 lua_newtable(L);
872 luaL_setfuncs(L, json_metatable_functions, 0);
873 json_regstore(L, arraymt);
874 // create and store ephemeron table to store shadow tables for each JSON object/array
875 // to allow NULL values returned as nil
876 lua_newtable(L);
877 lua_newtable(L); // metatable for ephemeron table
878 lua_pushliteral(L, "__mode");
879 lua_pushliteral(L, "k");
880 lua_rawset(L, -3);
881 lua_setmetatable(L, -2);
882 json_regstore(L, shadowtbl);
883 // set metatable of null marker and make it available through library module:
884 json_pushnullmark(L);
885 lua_newtable(L);
886 luaL_setfuncs(L, json_nullmark_metamethods, 0);
887 lua_setmetatable(L, -2);
888 lua_setfield(L, 1, "null");
889 // return library module (that's expected on top of stack):
890 return 1;
891 }

Impressum / About Us