webmcp

view libraries/json/json.c @ 164:8e969519f7c7

JSON pretty printer
author jbe
date Thu Jul 31 20:49:16 2014 +0200 (2014-07-31)
parents 581878c613d6
children 0c230f701967
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_pushfstring(L, "More than %d nested JSON levels", JSON_MAXDEPTH);
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 #define json_export_internal_indentstring_idx 1
715 #define json_export_internal_level_idx 2
716 #define json_export_internal_value_idx 3
717 #define json_export_internal_tmp_idx 4
719 static int json_export_internal(lua_State *L) {
720 int level;
721 int pretty;
722 int i;
723 lua_Number num;
724 const char *str;
725 unsigned char c;
726 size_t strlen;
727 size_t pos = 0;
728 luaL_Buffer buf;
729 char hexcode[7]; // backslash, character 'u', 4 hex digits, and terminating NULL byte
730 int tabletype = JSON_TABLETYPE_UNKNOWN;
731 int anyelement = 0;
732 size_t keycount = 0;
733 size_t keypos = 0;
734 json_key_t *keybuf;
735 lua_Integer idx;
736 lua_settop(L, json_export_internal_value_idx);
737 if (json_isnullmark(L, json_export_internal_value_idx)) {
738 lua_pop(L, 1);
739 lua_pushnil(L);
740 }
741 switch (lua_type(L, json_export_internal_value_idx)) {
742 case LUA_TNIL:
743 lua_pushliteral(L, "null");
744 return 1;
745 case LUA_TNUMBER:
746 num = lua_tonumber(L, json_export_internal_value_idx);
747 if (isnan(num)) return luaL_error(L, "JSON export not possible for NaN value");
748 if (isinf(num)) return luaL_error(L, "JSON export not possible for infinite numbers");
749 lua_tostring(L, json_export_internal_value_idx);
750 return 1;
751 case LUA_TBOOLEAN:
752 if (lua_toboolean(L, json_export_internal_value_idx)) {
753 lua_pushliteral(L, "true");
754 } else {
755 lua_pushliteral(L, "false");
756 }
757 return 1;
758 case LUA_TSTRING:
759 str = lua_tolstring(L, 3, &strlen);
760 luaL_buffinit(L, &buf);
761 luaL_addchar(&buf, '"');
762 while (pos < strlen) {
763 c = str[pos++];
764 if (c == '"') luaL_addstring(&buf, "\\\"");
765 else if (c == '\\') luaL_addstring(&buf, "\\\\");
766 else if (c == 127) luaL_addstring(&buf, "\\u007F");
767 else if (c >= 32) luaL_addchar(&buf, c);
768 else if (c == '\b') luaL_addstring(&buf, "\\b");
769 else if (c == '\f') luaL_addstring(&buf, "\\f");
770 else if (c == '\n') luaL_addstring(&buf, "\\n");
771 else if (c == '\r') luaL_addstring(&buf, "\\r");
772 else if (c == '\t') luaL_addstring(&buf, "\\t");
773 else if (c == '\v') luaL_addstring(&buf, "\\v");
774 else {
775 sprintf(hexcode, "\\u%04X", c);
776 luaL_addstring(&buf, hexcode);
777 }
778 }
779 luaL_addchar(&buf, '"');
780 luaL_pushresult(&buf);
781 return 1;
782 case LUA_TTABLE:
783 if (lua_getmetatable(L, json_export_internal_value_idx)) {
784 json_regfetch(L, objectmt);
785 if (lua_rawequal(L, -2, -1)) {
786 tabletype = JSON_TABLETYPE_OBJECT;
787 } else {
788 json_regfetch(L, arraymt);
789 if (lua_rawequal(L, -3, -1)) {
790 tabletype = JSON_TABLETYPE_ARRAY;
791 } else {
792 return luaL_error(L, "JSON export not possible for tables with nonsupported metatable");
793 }
794 }
795 }
796 json_regfetch(L, shadowtbl);
797 lua_pushvalue(L, json_export_internal_value_idx);
798 lua_rawget(L, -2);
799 if (!lua_isnil(L, -1)) lua_replace(L, json_export_internal_value_idx);
800 lua_settop(L, json_export_internal_value_idx);
801 if (tabletype == JSON_TABLETYPE_UNKNOWN) {
802 for (lua_pushnil(L); lua_next(L, json_export_internal_value_idx); lua_pop(L, 1)) {
803 switch (lua_type(L, -2)) {
804 case LUA_TSTRING:
805 keycount++;
806 if (tabletype == JSON_TABLETYPE_UNKNOWN) tabletype = JSON_TABLETYPE_OBJECT;
807 else if (tabletype == JSON_TABLETYPE_ARRAY) goto json_export_tabletype_error;
808 break;
809 case LUA_TNUMBER:
810 if (tabletype == JSON_TABLETYPE_UNKNOWN) tabletype = JSON_TABLETYPE_ARRAY;
811 else if (tabletype == JSON_TABLETYPE_OBJECT) goto json_export_tabletype_error;
812 break;
813 }
814 }
815 }
816 pretty = lua_toboolean(L, json_export_internal_indentstring_idx);
817 level = lua_tointeger(L, json_export_internal_level_idx) + 1;
818 if (level > JSON_MAXDEPTH) {
819 return luaL_error(L, "More than %d nested JSON levels", JSON_MAXDEPTH);
820 }
821 switch (tabletype) {
822 case JSON_TABLETYPE_OBJECT:
823 if (!keycount) {
824 for (lua_pushnil(L); lua_next(L, json_export_internal_value_idx); lua_pop(L, 1)) {
825 if (lua_type(L, -2) == LUA_TSTRING) keycount++;
826 }
827 }
828 if (keycount) {
829 keybuf = calloc(keycount, sizeof(json_key_t));
830 if (!keybuf) return luaL_error(L, "Memory allocation failed in JSON library");
831 for (lua_pushnil(L); lua_next(L, json_export_internal_value_idx); lua_pop(L, 1)) {
832 if (lua_type(L, -2) == LUA_TSTRING) {
833 json_key_t *key = keybuf + (keypos++);
834 key->data = lua_tolstring(L, -2, &key->length);
835 }
836 }
837 qsort(keybuf, keycount, sizeof(json_key_t), (void *)json_key_cmp);
838 }
839 luaL_buffinit(L, &buf);
840 luaL_addchar(&buf, '{');
841 for (keypos=0; keypos<keycount; keypos++) {
842 json_key_t *key = keybuf + keypos;
843 if (keypos) luaL_addchar(&buf, ',');
844 if (pretty) {
845 luaL_addchar(&buf, '\n');
846 for (i=0; i<level; i++) {
847 lua_pushvalue(L, json_export_internal_indentstring_idx);
848 luaL_addvalue(&buf);
849 }
850 }
851 lua_pushcfunction(L, json_export_internal);
852 lua_pushvalue(L, json_export_internal_indentstring_idx);
853 lua_pushinteger(L, level);
854 lua_pushlstring(L, key->data, key->length);
855 if (lua_pcall(L, 3, 1, 0)) {
856 if (keybuf) free(keybuf);
857 return lua_error(L);
858 }
859 luaL_addvalue(&buf);
860 luaL_addchar(&buf, ':');
861 if (pretty) luaL_addchar(&buf, ' ');
862 lua_pushcfunction(L, json_export_internal);
863 lua_pushvalue(L, json_export_internal_indentstring_idx);
864 lua_pushinteger(L, level);
865 lua_pushlstring(L, key->data, key->length);
866 lua_rawget(L, json_export_internal_value_idx);
867 if (lua_pcall(L, 3, 1, 0)) {
868 if (keybuf) free(keybuf);
869 return lua_error(L);
870 }
871 luaL_addvalue(&buf);
872 }
873 if (keybuf) free(keybuf);
874 if (pretty && keycount != 0) {
875 luaL_addchar(&buf, '\n');
876 for (i=0; i<level-1; i++) {
877 lua_pushvalue(L, json_export_internal_indentstring_idx);
878 luaL_addvalue(&buf);
879 }
880 }
881 luaL_addchar(&buf, '}');
882 if (pretty && level == 1) luaL_addchar(&buf, '\n');
883 luaL_pushresult(&buf);
884 return 1;
885 case JSON_TABLETYPE_ARRAY:
886 lua_settop(L, json_export_internal_tmp_idx);
887 luaL_buffinit(L, &buf);
888 luaL_addchar(&buf, '[');
889 for (idx = 1; ; idx++) {
890 lua_rawgeti(L, json_export_internal_value_idx, idx);
891 if (lua_isnil(L, -1)) {
892 lua_pop(L, 1);
893 break;
894 }
895 lua_replace(L, json_export_internal_tmp_idx);
896 if (anyelement) luaL_addchar(&buf, ',');
897 anyelement = 1;
898 if (pretty) {
899 luaL_addchar(&buf, '\n');
900 for (i=0; i<level; i++) {
901 lua_pushvalue(L, json_export_internal_indentstring_idx);
902 luaL_addvalue(&buf);
903 }
904 }
905 lua_pushcfunction(L, json_export_internal);
906 lua_pushvalue(L, json_export_internal_indentstring_idx);
907 lua_pushinteger(L, level);
908 lua_pushvalue(L, json_export_internal_tmp_idx);
909 lua_call(L, 3, 1);
910 luaL_addvalue(&buf);
911 }
912 if (pretty && anyelement) {
913 luaL_addchar(&buf, '\n');
914 for (i=0; i<level-1; i++) {
915 lua_pushvalue(L, json_export_internal_indentstring_idx);
916 luaL_addvalue(&buf);
917 }
918 }
919 luaL_addchar(&buf, ']');
920 if (pretty && level == 1) luaL_addchar(&buf, '\n');
921 luaL_pushresult(&buf);
922 return 1;
923 }
924 json_export_tabletype_error:
925 return luaL_error(L, "JSON export not possible for ambiguous table (cannot decide whether it is an object or array)");
926 }
927 return luaL_error(L, "JSON export not possible for values of type \"%s\"", lua_typename(L, lua_type(L, 1)));
928 }
930 static int json_export(lua_State *L) {
931 lua_settop(L, 1);
932 lua_pushcfunction(L, json_export_internal);
933 lua_pushnil(L);
934 lua_pushinteger(L, 0);
935 lua_pushvalue(L, 1);
936 lua_call(L, 3, 1);
937 return 1;
938 }
940 static int json_pretty(lua_State *L) {
941 lua_settop(L, 2);
942 lua_pushcfunction(L, json_export_internal);
943 if (lua_isnil(L, 2)) lua_pushliteral(L, " ");
944 else lua_pushvalue(L, 2);
945 lua_pushinteger(L, 0);
946 lua_pushvalue(L, 1);
947 lua_call(L, 3, 1);
948 return 1;
949 }
951 // functions in library module:
952 static const struct luaL_Reg json_module_functions[] = {
953 {"object", json_object},
954 {"array", json_array},
955 {"import", json_import},
956 {"export", json_export},
957 {"pretty", json_pretty},
958 {"get", json_get},
959 {"type", json_type},
960 {NULL, NULL}
961 };
963 // metamethods for JSON objects, JSON arrays, and unknown JSON collections (object or array):
964 static const struct luaL_Reg json_metatable_functions[] = {
965 {"__len", json_len},
966 {"__index", json_index},
967 {"__newindex", json_newindex},
968 {"__pairs", json_pairs},
969 {"__ipairs", json_ipairs},
970 {"__tostring", json_export},
971 {NULL, NULL}
972 };
974 // metamethods for JSON null marker:
975 static const struct luaL_Reg json_nullmark_metamethods[] = {
976 {"__tostring", json_nullmark_tostring},
977 {NULL, NULL}
978 };
980 // initializes json library:
981 int luaopen_json(lua_State *L) {
982 // empty stack:
983 lua_settop(L, 0);
984 // push library module onto stack position 1:
985 lua_newtable(L);
986 // register library functions:
987 luaL_setfuncs(L, json_module_functions, 0);
988 // create and store objectmt:
989 lua_newtable(L);
990 luaL_setfuncs(L, json_metatable_functions, 0);
991 json_regstore(L, objectmt);
992 // create and store arraymt:
993 lua_newtable(L);
994 luaL_setfuncs(L, json_metatable_functions, 0);
995 json_regstore(L, arraymt);
996 // create and store ephemeron table to store shadow tables for each JSON object/array
997 // to allow NULL values returned as nil
998 lua_newtable(L);
999 lua_newtable(L); // metatable for ephemeron table
1000 lua_pushliteral(L, "__mode");
1001 lua_pushliteral(L, "k");
1002 lua_rawset(L, -3);
1003 lua_setmetatable(L, -2);
1004 json_regstore(L, shadowtbl);
1005 // set metatable of null marker and make it available through library module:
1006 json_pushnullmark(L);
1007 lua_newtable(L);
1008 luaL_setfuncs(L, json_nullmark_metamethods, 0);
1009 lua_setmetatable(L, -2);
1010 lua_setfield(L, 1, "null");
1011 // return library module (that's expected on top of stack):
1012 return 1;

Impressum / About Us