webmcp

view libraries/json/json.c @ 163:581878c613d6

Always sort keys in JSON objects when exporting
author jbe
date Thu Jul 31 19:27:29 2014 +0200 (2014-07-31)
parents 3b8c1e2aef9c
children 8e969519f7c7
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 outlen; // maximum length or write position of 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 // find last character in input string:
301 outlen = pos;
302 while ((c = str[outlen]) != '"') {
303 // consume one character:
304 outlen++;
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[outlen] == '"') outlen++;
309 }
310 // determine buffer length:
311 outlen -= pos;
312 // check if string is non empty:
313 if (outlen) {
314 // prepare buffer to decode string (with maximum possible length) and set write position to zero:
315 cbuf = luaL_buffinitsize(L, &luabuf, outlen);
316 outlen = 0;
317 // loop through the characters until encountering end quote:
318 while ((c = str[pos++]) != '"') {
319 // NOTE: unexpected end cannot happen anymore
320 if (c < 32 || c == 127) {
321 // do not allow ASCII control characters:
322 // NOTE: illegal UTF-8 sequences and extended control characters are not sanitized
323 // by this parser to allow different encodings than Unicode
324 lua_pushnil(L);
325 lua_pushliteral(L, "Unexpected control character in JSON string");
326 return 2;
327 } else if (c == '\\') {
328 // read next char after backslash escape:
329 c = str[pos++];
330 switch (c) {
331 // unexpected end-of-string:
332 case 0:
333 goto json_import_unexpected_eof;
334 // unescaping of quotation mark, slash, and backslash:
335 case '"':
336 case '/':
337 case '\\':
338 cbuf[outlen++] = c;
339 break;
340 // unescaping of backspace:
341 case 'b': cbuf[outlen++] = '\b'; break;
342 // unescaping of form-feed:
343 case 'f': cbuf[outlen++] = '\f'; break;
344 // unescaping of new-line:
345 case 'n': cbuf[outlen++] = '\n'; break;
346 // unescaping of carriage-return:
347 case 'r': cbuf[outlen++] = '\r'; break;
348 // unescaping of tabulator:
349 case 't': cbuf[outlen++] = '\t'; break;
350 // unescaping of UTF-16 characters
351 case 'u':
352 lua_pushnil(L);
353 lua_pushliteral(L, "JSON unicode escape sequences are not implemented yet"); // TODO
354 return 2;
355 // unexpected escape sequence:
356 default:
357 lua_pushnil(L);
358 lua_pushliteral(L, "Unexpected string escape sequence in JSON document");
359 return 2;
360 }
361 } else {
362 // normal character:
363 cbuf[outlen++] = c;
364 }
365 }
366 // process buffer to Lua string:
367 luaL_pushresultsize(&luabuf, outlen);
368 } else {
369 // if JSON string is empty,
370 // push empty Lua string:
371 lua_pushliteral(L, "");
372 }
373 // continue with processing of decoded string:
374 goto json_import_process_value;
375 }
376 // process values whose type is is not deducible from a single character:
377 if ((c >= '0' && c <= '9') || c == '-' || c == '+') {
378 // for numbers,
379 // use strtod() call to parse a (double precision) floating point number:
380 char *endptr;
381 double numval;
382 numval = strtod(str+pos, &endptr);
383 // catch parsing errors:
384 if (endptr == str+pos) goto json_import_syntax_error;
385 // consume characters that were parsed:
386 pos += endptr - (str+pos);
387 // push parsed (double precision) floating point number on Lua stack:
388 lua_pushnumber(L, numval);
389 } else if (!strncmp(str+pos, "true", 4)) {
390 // consume 4 input characters for "true":
391 pos += 4;
392 // put Lua true value onto stack:
393 lua_pushboolean(L, 1);
394 } else if (!strncmp(str+pos, "false", 5)) {
395 // consume 5 input characters for "false":
396 pos += 5;
397 // put Lua false value onto stack:
398 lua_pushboolean(L, 0);
399 } else if (!strncmp(str+pos, "null", 4)) {
400 // consume 4 input characters for "null":
401 pos += 4;
402 // different behavor for top-level and sub-levels:
403 if (level) {
404 // if sub-level,
405 // push special null-marker onto stack:
406 json_pushnullmark(L);
407 } else {
408 // if top-level,
409 // push nil onto stack:
410 lua_pushnil(L);
411 }
412 } else {
413 // all other cases are a syntax error:
414 goto json_import_syntax_error;
415 }
416 // process a decoded value or key value pair (expected on top of Lua stack):
417 json_import_process_value:
418 switch (mode) {
419 // an object key has been read:
420 case JSON_STATE_OBJECT_KEY:
421 // if an object key is not a string, then this is a syntax error:
422 if (lua_type(L, -1) != LUA_TSTRING) goto json_import_syntax_error;
423 // expect key terminator to follow:
424 mode = JSON_STATE_OBJECT_KEY_TERMINATOR;
425 // continue with loop:
426 goto json_import_loop;
427 // a key value pair has been read:
428 case JSON_STATE_OBJECT_VALUE:
429 // store key value pair in outer shadow table:
430 lua_rawset(L, -3);
431 // expect value terminator (or end of object) to follow:
432 mode = JSON_STATE_OBJECT_SEPARATOR;
433 // continue with loop:
434 goto json_import_loop;
435 // an array value has been read:
436 case JSON_STATE_ARRAY_VALUE:
437 // get current array length:
438 arraylen = lua_rawlen(L, -3);
439 // throw error if array would exceed INT_MAX elements:
440 // TODO: Lua 5.3 may support more elements
441 if (arraylen >= INT_MAX) {
442 lua_pushnil(L);
443 lua_pushfstring(L, "Array exceeded length of %d elements", INT_MAX);
444 }
445 // store value in outer shadow table:
446 lua_rawseti(L, -3, arraylen + 1);
447 // expect value terminator (or end of object) to follow:
448 mode = JSON_STATE_ARRAY_SEPARATOR;
449 // continue with loop
450 goto json_import_loop;
451 // a single value has been read:
452 case JSON_STATE_VALUE:
453 // leave value on top of stack, expect end of JSON document, and continue with loop:
454 mode = JSON_STATE_END;
455 goto json_import_loop;
456 }
457 // syntax error handling (reachable by goto statement):
458 json_import_syntax_error:
459 lua_pushnil(L);
460 lua_pushliteral(L, "Syntax error in JSON document");
461 return 2;
462 }
464 // special Lua stack indicies for json_path function:
465 #define json_path_shadowtbl_idx 1
467 // stack offset of arguments to json_path function:
468 #define json_path_idxshift 1
470 // gets a value or its type from a JSON document (passed as first argument)
471 // using a path (passed as variable number of keys after first argument):
472 static int json_path(lua_State *L, int type_mode) {
473 int stacktop; // stack index of top of stack (after shifting)
474 int idx = 2 + json_path_idxshift; // stack index of current argument to process
475 // insert shadowtbl into stack at position 1 (shifting the arguments):
476 json_regfetch(L, shadowtbl);
477 lua_insert(L, 1);
478 // store stack index of top of stack:
479 stacktop = lua_gettop(L);
480 // use first argument as "current value" (stored on top of stack):
481 lua_pushvalue(L, 1 + json_path_idxshift);
482 // process each "path key" (2nd argument and following arguments):
483 while (idx <= stacktop) {
484 // if "current value" (on top of stack) is nil, then the path cannot be walked and nil is returned:
485 if (lua_isnil(L, -1)) return 1;
486 // try to get shadow table of "current value":
487 lua_pushvalue(L, -1);
488 lua_rawget(L, json_path_shadowtbl_idx);
489 if (lua_isnil(L, -1)) {
490 // if no shadow table is found,
491 if (lua_type(L, -1) == LUA_TTABLE) {
492 // and if "current value" is a table,
493 // drop nil from stack:
494 lua_pop(L, 1);
495 // get "next value" using the "path key":
496 lua_pushvalue(L, idx++);
497 lua_gettable(L, -2);
498 } else {
499 // if "current value" is not a table,
500 // then the path cannot be walked and nil (already on top of stack) is returned:
501 return 1;
502 }
503 } else {
504 // if a shadow table is found,
505 // set "current value" to its shadow table:
506 lua_replace(L, -2);
507 // get "next value" using the "path key":
508 lua_pushvalue(L, idx++);
509 lua_rawget(L, -2);
510 }
511 // the "next value" replaces the "current value":
512 lua_replace(L, -2);
513 }
514 if (!type_mode) {
515 // if a value (and not its type) was requested,
516 // check if value is the null-marker, and store nil on top of Lua stack in that case:
517 if (json_isnullmark(L, -1)) lua_pushnil(L);
518 } else {
519 // if the type was requested,
520 // check if value is the null-marker:
521 if (json_isnullmark(L, -1)) {
522 // if yes, store string "null" on top of Lua stack:
523 lua_pushliteral(L, "null");
524 } else {
525 // otherwise,
526 // check if metatable indicates "object" or "array":
527 if (lua_getmetatable(L, -1)) {
528 json_regfetch(L, objectmt);
529 if (lua_rawequal(L, -2, -1)) {
530 // if value has metatable for JSON objects,
531 // return string "object":
532 lua_pushliteral(L, "object");
533 return 1;
534 }
535 json_regfetch(L, arraymt);
536 if (lua_rawequal(L, -3, -1)) {
537 // if value has metatable for JSON arrays,
538 // return string "object":
539 lua_pushliteral(L, "array");
540 return 1;
541 }
542 // remove 3 metatables (one of the value, two for comparison) from stack:
543 lua_pop(L, 3);
544 }
545 // otherwise, get the Lua type:
546 lua_pushstring(L, lua_typename(L, lua_type(L, -1)));
547 }
548 }
549 // return the top most value on the Lua stack:
550 return 1;
551 }
553 // gets a value from a JSON document (passed as first argument)
554 // using a path (passed as variable number of keys after first argument):
555 static int json_get(lua_State *L) {
556 return json_path(L, 0);
557 }
559 // gets a value's type from a JSON document (passed as first argument)
560 // using a path (variable number of keys after first argument):
561 static int json_type(lua_State *L) {
562 return json_path(L, 1);
563 }
565 // returns the length of a JSON array (or zero for a table without numeric keys):
566 static int json_len(lua_State *L) {
567 // stack shall contain one function argument:
568 lua_settop(L, 1);
569 // try to get corresponding shadow table for first argument:
570 json_regfetch(L, shadowtbl);
571 lua_pushvalue(L, 1);
572 lua_rawget(L, -2);
573 // if shadow table does not exist, return length of argument, else length of shadow table:
574 lua_pushnumber(L, lua_rawlen(L, lua_isnil(L, -1) ? 1 : -1));
575 return 1;
576 }
578 static int json_index(lua_State *L) {
579 // stack shall contain two function arguments:
580 lua_settop(L, 2);
581 // get corresponding shadow table for first argument:
582 json_regfetch(L, shadowtbl);
583 lua_pushvalue(L, 1);
584 lua_rawget(L, -2);
585 // throw error if no shadow table was found:
586 if (lua_isnil(L, -1)) return luaL_error(L, "Shadow table not found");
587 // use key passed as second argument to lookup value in shadow table:
588 lua_pushvalue(L, 2);
589 lua_rawget(L, -2);
590 // if value is null-marker, then push nil onto stack:
591 if (json_isnullmark(L, -1)) lua_pushnil(L);
592 // return either looked up value, or nil
593 return 1;
594 }
596 static int json_newindex(lua_State *L) {
597 // stack shall contain three function arguments:
598 lua_settop(L, 3);
599 // get corresponding shadow table for first argument:
600 json_regfetch(L, shadowtbl);
601 lua_pushvalue(L, 1);
602 lua_rawget(L, -2);
603 // throw error if no shadow table was found:
604 if (lua_isnil(L, -1)) return luaL_error(L, "Shadow table not found");
605 // replace first argument with shadow table:
606 lua_replace(L, 1);
607 // reset stack and use second and third argument to write to shadow table:
608 lua_settop(L, 3);
609 lua_rawset(L, 1);
610 // return nothing:
611 return 0;
612 }
614 static int json_pairs_iterfunc(lua_State *L) {
615 // stack shall contain two function arguments:
616 lua_settop(L, 2);
617 // get corresponding shadow table for first argument:
618 json_regfetch(L, shadowtbl);
619 lua_pushvalue(L, 1);
620 lua_rawget(L, -2);
621 // throw error if no shadow table was found:
622 if (lua_isnil(L, -1)) return luaL_error(L, "Shadow table not found");
623 // get next key value pair from shadow table (using previous key from argument 2)
624 // and return nothing if there is no next pair:
625 lua_pushvalue(L, 2);
626 if (!lua_next(L, -2)) return 0;
627 // replace null-marker with nil:
628 if (json_isnullmark(L, -1)) {
629 lua_pop(L, 1);
630 lua_pushnil(L);
631 }
632 // return key and value (or key and nil, if null-marker was found):
633 return 2;
634 }
636 // returns a triple such that 'for key, value in pairs(obj) do ... end'
637 // iterates through all key value pairs (including JSON null keys represented as Lua nil):
638 static int json_pairs(lua_State *L) {
639 // return triple of function json_pairs_iterfunc, first argument, and nil:
640 lua_pushcfunction(L, json_pairs_iterfunc);
641 lua_pushvalue(L, 1);
642 lua_pushnil(L);
643 return 3;
644 }
646 static int json_ipairs_iterfunc(lua_State *L) {
647 lua_Integer idx;
648 // stack shall contain two function arguments:
649 lua_settop(L, 2);
650 // calculate new index by incrementing second argument:
651 idx = lua_tointeger(L, 2) + 1;
652 // get corresponding shadow table for first argument:
653 json_regfetch(L, shadowtbl);
654 lua_pushvalue(L, 1);
655 lua_rawget(L, -2);
656 // throw error if no shadow table was found:
657 if (lua_isnil(L, -1)) return luaL_error(L, "Shadow table not found");
658 // do integer lookup in shadow table:
659 lua_rawgeti(L, -1, idx);
660 // return nothing if there was no value:
661 if (lua_isnil(L, -1)) return 0;
662 // return new index and
663 // either the looked up value if it is not equal to the null-marker
664 // or nil instead of null-marker:
665 lua_pushinteger(L, idx);
666 if (json_isnullmark(L, -2)) lua_pushnil(L);
667 else lua_pushvalue(L, -2);
668 return 2;
669 }
671 // returns a triple such that 'for idx, value in ipairs(ary) do ... end'
672 // iterates through all values (including JSON null represented as Lua nil):
673 static int json_ipairs(lua_State *L) {
674 // return triple of function json_ipairs_iterfunc, first argument, and zero:
675 lua_pushcfunction(L, json_ipairs_iterfunc);
676 lua_pushvalue(L, 1);
677 lua_pushinteger(L, 0);
678 return 3;
679 }
681 typedef struct {
682 size_t length;
683 const char *data;
684 } json_key_t;
686 static int json_key_cmp(json_key_t *key1, json_key_t *key2) {
687 size_t pos = 0;
688 unsigned char c1, c2;
689 while (1) {
690 if (key1->length > pos) {
691 if (key2->length > pos) {
692 c1 = key1->data[pos];
693 c2 = key2->data[pos];
694 if (c1 < c2) return -1;
695 else if (c1 > c2) return 1;
696 } else {
697 return 1;
698 }
699 } else {
700 if (key2->length > pos) {
701 return -1;
702 } else {
703 return 0;
704 }
705 }
706 pos++;
707 }
708 }
710 #define JSON_TABLETYPE_UNKNOWN 0
711 #define JSON_TABLETYPE_OBJECT 1
712 #define JSON_TABLETYPE_ARRAY 2
714 static int json_export(lua_State *L) {
715 lua_Number num;
716 const char *str;
717 unsigned char c;
718 size_t strlen;
719 size_t pos = 0;
720 luaL_Buffer buf;
721 char hexcode[7]; // backslash, character 'u', 4 hex digits, and terminating NULL byte
722 int luatype;
723 int tabletype = JSON_TABLETYPE_UNKNOWN;
724 int needsep = 0;
725 size_t keycount = 0;
726 size_t keypos = 0;
727 json_key_t *keybuf;
728 lua_Integer idx;
729 lua_settop(L, 1);
730 if (json_isnullmark(L, 1)) {
731 lua_pushnil(L);
732 lua_replace(L, 1);
733 }
734 switch (lua_type(L, 1)) {
735 case LUA_TNIL:
736 lua_pushliteral(L, "null");
737 return 1;
738 case LUA_TNUMBER:
739 num = lua_tonumber(L, 1);
740 if (isnan(num)) return luaL_error(L, "JSON export not possible for NaN value");
741 if (isinf(num)) return luaL_error(L, "JSON export not possible for infinite numbers");
742 lua_tostring(L, 1);
743 return 1;
744 case LUA_TBOOLEAN:
745 if (lua_toboolean(L, 1)) lua_pushliteral(L, "true");
746 else lua_pushliteral(L, "false");
747 return 1;
748 case LUA_TSTRING:
749 str = lua_tolstring(L, 1, &strlen);
750 luaL_buffinit(L, &buf);
751 luaL_addchar(&buf, '"');
752 while (pos < strlen) {
753 c = str[pos++];
754 if (c == '"') luaL_addstring(&buf, "\\\"");
755 else if (c == '\\') luaL_addstring(&buf, "\\\\");
756 else if (c == 127) luaL_addstring(&buf, "\\u007F");
757 else if (c >= 32) luaL_addchar(&buf, c);
758 else if (c == '\b') luaL_addstring(&buf, "\\b");
759 else if (c == '\f') luaL_addstring(&buf, "\\f");
760 else if (c == '\n') luaL_addstring(&buf, "\\n");
761 else if (c == '\r') luaL_addstring(&buf, "\\r");
762 else if (c == '\t') luaL_addstring(&buf, "\\t");
763 else if (c == '\v') luaL_addstring(&buf, "\\v");
764 else {
765 sprintf(hexcode, "\\u%04X", c);
766 luaL_addstring(&buf, hexcode);
767 }
768 }
769 luaL_addchar(&buf, '"');
770 luaL_pushresult(&buf);
771 return 1;
772 case LUA_TTABLE:
773 if (lua_getmetatable(L, 1)) {
774 json_regfetch(L, objectmt);
775 if (lua_rawequal(L, -2, -1)) {
776 tabletype = JSON_TABLETYPE_OBJECT;
777 } else {
778 json_regfetch(L, arraymt);
779 if (lua_rawequal(L, -3, -1)) tabletype = JSON_TABLETYPE_ARRAY;
780 }
781 }
782 json_regfetch(L, shadowtbl);
783 lua_pushvalue(L, 1);
784 lua_rawget(L, -2);
785 if (!lua_isnil(L, -1)) lua_replace(L, 1);
786 lua_settop(L, 1);
787 if (tabletype == JSON_TABLETYPE_UNKNOWN) {
788 for (lua_pushnil(L); lua_next(L, 1); lua_pop(L, 1)) {
789 luatype = lua_type(L, -2);
790 if (tabletype == JSON_TABLETYPE_UNKNOWN) {
791 if (luatype == LUA_TSTRING) tabletype = JSON_TABLETYPE_OBJECT;
792 else if (luatype == LUA_TNUMBER) tabletype = JSON_TABLETYPE_ARRAY;
793 } else if (
794 (tabletype == JSON_TABLETYPE_OBJECT && luatype == LUA_TNUMBER) ||
795 (tabletype == JSON_TABLETYPE_ARRAY && luatype == LUA_TSTRING)
796 ) {
797 goto json_export_tabletype_error;
798 }
799 }
800 }
801 switch (tabletype) {
802 case JSON_TABLETYPE_OBJECT:
803 for (lua_pushnil(L); lua_next(L, 1); lua_pop(L, 1)) {
804 if (lua_type(L, -2) == LUA_TSTRING) keycount++;
805 }
806 if (keycount) {
807 keybuf = calloc(keycount, sizeof(json_key_t));
808 if (!keybuf) return luaL_error(L, "Memory allocation failed in JSON library");
809 for (lua_pushnil(L); lua_next(L, 1); lua_pop(L, 1)) {
810 if (lua_type(L, -2) == LUA_TSTRING) {
811 json_key_t *key = keybuf + (keypos++);
812 key->data = lua_tolstring(L, -2, &key->length);
813 }
814 }
815 qsort(keybuf, keycount, sizeof(json_key_t), (void *)json_key_cmp);
816 }
817 luaL_buffinit(L, &buf);
818 luaL_addchar(&buf, '{');
819 for (keypos=0; keypos<keycount; keypos++) {
820 json_key_t *key = keybuf + keypos;
821 if (keypos) luaL_addchar(&buf, ',');
822 lua_pushcfunction(L, json_export);
823 lua_pushlstring(L, key->data, key->length);
824 if (lua_pcall(L, 1, 1, 0)) {
825 if (keybuf) free(keybuf);
826 return lua_error(L);
827 }
828 luaL_addvalue(&buf);
829 luaL_addchar(&buf, ':');
830 lua_pushcfunction(L, json_export);
831 lua_pushlstring(L, key->data, key->length);
832 lua_rawget(L, 1);
833 if (lua_pcall(L, 1, 1, 0)) {
834 if (keybuf) free(keybuf);
835 return lua_error(L);
836 }
837 luaL_addvalue(&buf);
838 }
839 if (keybuf) free(keybuf);
840 luaL_addchar(&buf, '}');
841 luaL_pushresult(&buf);
842 return 1;
843 case JSON_TABLETYPE_ARRAY:
844 lua_settop(L, 2);
845 luaL_buffinit(L, &buf);
846 luaL_addchar(&buf, '[');
847 for (idx = 1; ; idx++) {
848 lua_rawgeti(L, 1, idx);
849 if (lua_isnil(L, -1)) {
850 lua_pop(L, 1);
851 break;
852 }
853 lua_replace(L, 2);
854 if (needsep) luaL_addchar(&buf, ',');
855 else needsep = 1;
856 lua_pushcfunction(L, json_export);
857 lua_pushvalue(L, 2);
858 lua_call(L, 1, 1);
859 luaL_addvalue(&buf);
860 }
861 luaL_addchar(&buf, ']');
862 luaL_pushresult(&buf);
863 return 1;
864 }
865 json_export_tabletype_error:
866 return luaL_error(L, "JSON export not possible for ambiguous table (cannot decide whether it is an object or array)");
867 }
868 return luaL_error(L, "JSON export not possible for values of type \"%s\"", lua_typename(L, lua_type(L, 1)));
869 }
871 // functions in library module:
872 static const struct luaL_Reg json_module_functions[] = {
873 {"object", json_object},
874 {"array", json_array},
875 {"import", json_import},
876 {"export", json_export},
877 {"get", json_get},
878 {"type", json_type},
879 {NULL, NULL}
880 };
882 // metamethods for JSON objects, JSON arrays, and unknown JSON collections (object or array):
883 static const struct luaL_Reg json_metatable_functions[] = {
884 {"__len", json_len},
885 {"__index", json_index},
886 {"__newindex", json_newindex},
887 {"__pairs", json_pairs},
888 {"__ipairs", json_ipairs},
889 {"__tostring", json_export},
890 {NULL, NULL}
891 };
893 // metamethods for JSON null marker:
894 static const struct luaL_Reg json_nullmark_metamethods[] = {
895 {"__tostring", json_nullmark_tostring},
896 {NULL, NULL}
897 };
899 // initializes json library:
900 int luaopen_json(lua_State *L) {
901 // empty stack:
902 lua_settop(L, 0);
903 // push library module onto stack position 1:
904 lua_newtable(L);
905 // register library functions:
906 luaL_setfuncs(L, json_module_functions, 0);
907 // create and store objectmt:
908 lua_newtable(L);
909 luaL_setfuncs(L, json_metatable_functions, 0);
910 json_regstore(L, objectmt);
911 // create and store arraymt:
912 lua_newtable(L);
913 luaL_setfuncs(L, json_metatable_functions, 0);
914 json_regstore(L, arraymt);
915 // create and store ephemeron table to store shadow tables for each JSON object/array
916 // to allow NULL values returned as nil
917 lua_newtable(L);
918 lua_newtable(L); // metatable for ephemeron table
919 lua_pushliteral(L, "__mode");
920 lua_pushliteral(L, "k");
921 lua_rawset(L, -3);
922 lua_setmetatable(L, -2);
923 json_regstore(L, shadowtbl);
924 // set metatable of null marker and make it available through library module:
925 json_pushnullmark(L);
926 lua_newtable(L);
927 luaL_setfuncs(L, json_nullmark_metamethods, 0);
928 lua_setmetatable(L, -2);
929 lua_setfield(L, 1, "null");
930 // return library module (that's expected on top of stack):
931 return 1;
932 }

Impressum / About Us