webmcp

view libraries/json/json.c @ 168:e618ccd017a3

Cleaned up macros for json.import function
author jbe
date Fri Aug 01 02:48:18 2014 +0200 (2014-08-01)
parents 84497222db4e
children 681367a16657
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 // require argument to be a table:
59 luaL_checktype(L, 1, LUA_TTABLE);
60 // push shadow table on top of stack:
61 json_regfetch(L, shadowtbl);
62 lua_pushvalue(L, 1);
63 lua_rawget(L, -2);
64 // if shadow table does not exist:
65 if (lua_isnil(L, -1)) {
66 // create shadow table and leave it on top of stack:
67 lua_newtable(L);
68 lua_pushvalue(L, 1);
69 lua_pushvalue(L, -2);
70 lua_rawset(L, -5);
71 }
72 // move elements from original table to shadow table (that's expected on top of stack):
73 for(lua_pushnil(L); lua_next(L, 1); lua_pop(L, 1)) {
74 lua_pushvalue(L, -2);
75 lua_pushnil(L);
76 lua_rawset(L, 1);
77 lua_pushvalue(L, -2);
78 lua_pushvalue(L, -2);
79 lua_rawset(L, -5);
80 }
81 }
82 // discard everything but table to return:
83 lua_settop(L, 1);
84 // set metatable:
85 json_regfetchpointer(L, mt);
86 lua_setmetatable(L, 1);
87 // return table:
88 return 1;
89 }
91 // marks a table as JSON object:
92 // (returns its modified argument or a new table if argument is nil)
93 static int json_object(lua_State *L) {
94 return json_mark(L, json_regpointer(objectmt));
95 }
97 // marks a table as JSON array:
98 // (returns its modified argument or a new table if argument is nil)
99 static int json_array(lua_State *L) {
100 return json_mark(L, json_regpointer(arraymt));
101 }
103 // internal states of JSON parser:
104 #define JSON_STATE_VALUE 0
105 #define JSON_STATE_OBJECT_KEY 1
106 #define JSON_STATE_OBJECT_KEY_TERMINATOR 2
107 #define JSON_STATE_OBJECT_VALUE 3
108 #define JSON_STATE_OBJECT_SEPARATOR 4
109 #define JSON_STATE_ARRAY_VALUE 5
110 #define JSON_STATE_ARRAY_SEPARATOR 6
111 #define JSON_STATE_END 7
113 // special Lua stack indicies for json_import function:
114 #define json_import_objectmt_idx 2
115 #define json_import_arraymt_idx 3
116 #define json_import_shadowtbl_idx 4
118 // macros for hex decoding:
119 #define json_utf16_surrogate(x) ((x) >= 0xD800 && (x) <= 0xDFFF)
120 #define json_utf16_lead(x) ((x) >= 0xD800 && (x) <= 0xDBFF)
121 #define json_utf16_tail(x) ((x) >= 0xDC00 && (x) <= 0xDFFF)
122 #define json_import_readhex(x) \
123 do { \
124 x = 0; \
125 for (i=0; i<4; i++) { \
126 x <<= 4; \
127 c = str[pos++]; \
128 if (c >= '0' && c <= '9') x += c - '0'; \
129 else if (c >= 'A' && c <= 'F') x += c - 'A' + 10; \
130 else if (c >= 'a' && c <= 'f') x += c - 'a' + 10; \
131 else if (c == 0) goto json_import_unexpected_eof; \
132 else goto json_import_unexpected_escape; \
133 } \
134 } while (0)
136 // decodes a JSON document:
137 static int json_import(lua_State *L) {
138 int i; // loop variable
139 const char *str; // string to parse
140 size_t total; // total length of string to parse
141 size_t pos = 0; // current position in string to parse
142 size_t level = 0; // nested levels of objects/arrays currently being processed
143 int mode = JSON_STATE_VALUE; // state of parser (i.e. "what's expected next?")
144 char c; // variable to store a single character to be processed
145 luaL_Buffer luabuf; // Lua buffer to decode JSON string values
146 char *cbuf; // C buffer to decode JSON string values
147 size_t outlen; // maximum length or write position of C buffer
148 long codepoint; // decoded UTF-16 character or higher codepoint
149 long utf16tail; // second decoded UTF-16 character (surrogate tail)
150 size_t arraylen; // variable to temporarily store the array length
151 // require string as argument and convert to C string with length information:
152 str = luaL_checklstring(L, 1, &total);
153 // if string contains a NULL byte, this is a syntax error
154 if (strlen(str) != total) goto json_import_syntax_error;
155 // stack shall contain one function argument:
156 lua_settop(L, 1);
157 // push objectmt onto stack position 2:
158 json_regfetch(L, objectmt);
159 // push arraymt onto stack position 3:
160 json_regfetch(L, arraymt);
161 // push shadowtbl onto stack position 4:
162 json_regfetch(L, shadowtbl);
163 // main loop of parser:
164 json_import_loop:
165 // skip whitespace and store next character in variable 'c':
166 while (c = str[pos],
167 c == ' ' ||
168 c == '\f' ||
169 c == '\n' ||
170 c == '\r' ||
171 c == '\t' ||
172 c == '\v'
173 ) pos++;
174 // switch statement to handle certain (single) characters:
175 switch (c) {
176 // handle end of JSON document:
177 case 0:
178 // if end of JSON document was expected, then return top element of stack as result:
179 if (mode == JSON_STATE_END) return 1;
180 // otherwise, the JSON document was malformed:
181 if (level == 0) {
182 lua_pushnil(L);
183 lua_pushliteral(L, "Empty string");
184 } else {
185 json_import_unexpected_eof:
186 lua_pushnil(L);
187 lua_pushliteral(L, "Unexpected end of JSON document");
188 }
189 return 2;
190 // new JSON object:
191 case '{':
192 // if a JSON object is not expected here, then return an error:
193 if (
194 mode != JSON_STATE_VALUE &&
195 mode != JSON_STATE_OBJECT_VALUE &&
196 mode != JSON_STATE_ARRAY_VALUE
197 ) goto json_import_syntax_error;
198 // create JSON object on stack:
199 lua_newtable(L);
200 // set metatable of JSON object:
201 lua_pushvalue(L, json_import_objectmt_idx);
202 lua_setmetatable(L, -2);
203 // create internal shadow table on stack:
204 lua_newtable(L);
205 // register internal shadow table:
206 lua_pushvalue(L, -2);
207 lua_pushvalue(L, -2);
208 lua_rawset(L, json_import_shadowtbl_idx);
209 // expect object key (or end of object) to follow:
210 mode = JSON_STATE_OBJECT_KEY;
211 // jump to common code for opening JSON object and JSON array:
212 goto json_import_open;
213 // new JSON array:
214 case '[':
215 // if a JSON array is not expected here, then return an error:
216 if (
217 mode != JSON_STATE_VALUE &&
218 mode != JSON_STATE_OBJECT_VALUE &&
219 mode != JSON_STATE_ARRAY_VALUE
220 ) goto json_import_syntax_error;
221 // create JSON array on stack:
222 lua_newtable(L);
223 // set metatable of JSON array:
224 lua_pushvalue(L, json_import_arraymt_idx);
225 lua_setmetatable(L, -2);
226 // create internal shadow table on stack:
227 lua_newtable(L);
228 // register internal shadow table:
229 lua_pushvalue(L, -2);
230 lua_pushvalue(L, -2);
231 lua_rawset(L, json_import_shadowtbl_idx);
232 // add nil as key (needed to keep stack balance) and as magic to detect arrays:
233 lua_pushnil(L);
234 // expect array value (or end of array) to follow:
235 mode = JSON_STATE_ARRAY_VALUE;
236 // continue with common code for opening JSON object and JSON array:
237 // common code for opening JSON object or JSON array:
238 json_import_open:
239 // limit nested levels:
240 if (level >= JSON_MAXDEPTH) {
241 lua_pushnil(L);
242 lua_pushfstring(L, "More than %d nested JSON levels", JSON_MAXDEPTH);
243 return 2;
244 }
245 // additional buffer overflow protection:
246 if (!lua_checkstack(L, LUA_MINSTACK))
247 return luaL_error(L, "Caught stack overflow in JSON import function (too many nested levels and stack size too small)");
248 // increment level:
249 level++;
250 // consume input character:
251 pos++;
252 goto json_import_loop;
253 // end of JSON object:
254 case '}':
255 // if end of JSON object is not expected here, then return an error:
256 if (
257 mode != JSON_STATE_OBJECT_KEY &&
258 mode != JSON_STATE_OBJECT_SEPARATOR
259 ) goto json_import_syntax_error;
260 // jump to common code for end of JSON object and JSON array:
261 goto json_import_close;
262 // end of JSON array:
263 case ']':
264 // if end of JSON array is not expected here, then return an error:
265 if (
266 mode != JSON_STATE_ARRAY_VALUE &&
267 mode != JSON_STATE_ARRAY_SEPARATOR
268 ) goto json_import_syntax_error;
269 // pop nil key/magic (that was needed to keep stack balance):
270 lua_pop(L, 1);
271 // continue with common code for end of JSON object and JSON array:
272 // common code for end of JSON object or JSON array:
273 json_import_close:
274 // consume input character:
275 pos++;
276 // pop shadow table:
277 lua_pop(L, 1);
278 // check if nested:
279 if (--level) {
280 // if nested,
281 // check if outer(!) structure is an array or object:
282 if (lua_isnil(L, -2)) {
283 // select array value processing:
284 mode = JSON_STATE_ARRAY_VALUE;
285 } else {
286 // select object value processing:
287 mode = JSON_STATE_OBJECT_VALUE;
288 }
289 // store value in outer structure:
290 goto json_import_process_value;
291 }
292 // if not nested, then expect end of JSON document and continue with loop:
293 mode = JSON_STATE_END;
294 goto json_import_loop;
295 // key terminator:
296 case ':':
297 // if key terminator is not expected here, then return an error:
298 if (mode != JSON_STATE_OBJECT_KEY_TERMINATOR)
299 goto json_import_syntax_error;
300 // consume input character:
301 pos++;
302 // expect object value to follow:
303 mode = JSON_STATE_OBJECT_VALUE;
304 // continue with loop:
305 goto json_import_loop;
306 // value terminator (NOTE: trailing comma at end of value or key-value list is tolerated by this parser)
307 case ',':
308 // branch according to parser state:
309 if (mode == JSON_STATE_OBJECT_SEPARATOR) {
310 // expect an object key to follow:
311 mode = JSON_STATE_OBJECT_KEY;
312 } else if (mode == JSON_STATE_ARRAY_SEPARATOR) {
313 // expect an array value to follow:
314 mode = JSON_STATE_ARRAY_VALUE;
315 } else {
316 // if value terminator is not expected here, then return an error:
317 goto json_import_syntax_error;
318 }
319 // consume input character:
320 pos++;
321 // continue with loop:
322 goto json_import_loop;
323 // string literal:
324 case '"':
325 // consume quote character:
326 pos++;
327 // find last character in input string:
328 outlen = pos;
329 while ((c = str[outlen]) != '"') {
330 // consume one character:
331 outlen++;
332 // handle unexpected end of JSON document:
333 if (c == 0) goto json_import_unexpected_eof;
334 // consume one extra character when encountering an escaped quote:
335 else if (c == '\\' && str[outlen] == '"') outlen++;
336 }
337 // determine buffer length:
338 outlen -= pos;
339 // check if string is non empty:
340 if (outlen) {
341 // prepare buffer to decode string (with maximum possible length) and set write position to zero:
342 cbuf = luaL_buffinitsize(L, &luabuf, outlen);
343 outlen = 0;
344 // loop through the characters until encountering end quote:
345 while ((c = str[pos++]) != '"') {
346 // NOTE: unexpected end cannot happen anymore
347 if (c < 32 || c == 127) {
348 // do not allow ASCII control characters:
349 // NOTE: illegal UTF-8 sequences and extended control characters are not sanitized
350 // by this parser to allow different encodings than Unicode
351 lua_pushnil(L);
352 lua_pushliteral(L, "Unexpected control character in JSON string");
353 return 2;
354 } else if (c == '\\') {
355 // read next char after backslash escape:
356 c = str[pos++];
357 switch (c) {
358 // unexpected end-of-string:
359 case 0:
360 goto json_import_unexpected_eof;
361 // unescaping of quotation mark, slash, and backslash:
362 case '"':
363 case '/':
364 case '\\':
365 cbuf[outlen++] = c;
366 break;
367 // unescaping of backspace:
368 case 'b': cbuf[outlen++] = '\b'; break;
369 // unescaping of form-feed:
370 case 'f': cbuf[outlen++] = '\f'; break;
371 // unescaping of new-line:
372 case 'n': cbuf[outlen++] = '\n'; break;
373 // unescaping of carriage-return:
374 case 'r': cbuf[outlen++] = '\r'; break;
375 // unescaping of tabulator:
376 case 't': cbuf[outlen++] = '\t'; break;
377 // unescaping of UTF-16 characters
378 case 'u':
379 // decode 4 hex nibbles:
380 json_import_readhex(codepoint);
381 // handle surrogate character:
382 if (json_utf16_surrogate(codepoint)) {
383 // check if first surrogate is in valid range:
384 if (json_utf16_lead(codepoint)) {
385 // require second surrogate:
386 if ((c = str[pos++]) != '\\' || (c = str[pos++]) != 'u') {
387 if (c == 0) goto json_import_unexpected_eof;
388 else goto json_import_wrong_surrogate;
389 }
390 // read 4 hex nibbles of second surrogate character:
391 json_import_readhex(utf16tail);
392 // check if second surrogate is in valid range:
393 if (!json_utf16_tail(utf16tail)) goto json_import_wrong_surrogate;
394 // calculate codepoint:
395 codepoint = 0x10000 + (utf16tail - 0xDC00) + (codepoint - 0xD800) * 0x400;
396 } else {
397 // throw error for wrong surrogates:
398 json_import_wrong_surrogate:
399 lua_pushnil(L);
400 lua_pushliteral(L, "Illegal UTF-16 surrogate in JSON string escape sequence");
401 return 2;
402 }
403 }
404 // encode as UTF-8:
405 if (codepoint < 0x80) {
406 cbuf[outlen++] = (char)codepoint;
407 } else if (codepoint < 0x800) {
408 cbuf[outlen++] = (char)(0xc0 | (codepoint >> 6));
409 cbuf[outlen++] = (char)(0x80 | (codepoint & 0x3f));
410 } else if (codepoint < 0x10000) {
411 cbuf[outlen++] = (char)(0xe0 | (codepoint >> 12));
412 cbuf[outlen++] = (char)(0x80 | ((codepoint >> 6) & 0x3f));
413 cbuf[outlen++] = (char)(0x80 | (codepoint & 0x3f));
414 } else {
415 cbuf[outlen++] = (char)(0xf0 | (codepoint >> 18));
416 cbuf[outlen++] = (char)(0x80 | ((codepoint >> 12) & 0x3f));
417 cbuf[outlen++] = (char)(0x80 | ((codepoint >> 6) & 0x3f));
418 cbuf[outlen++] = (char)(0x80 | (codepoint & 0x3f));
419 }
420 break;
421 // unexpected escape sequence:
422 default:
423 json_import_unexpected_escape:
424 lua_pushnil(L);
425 lua_pushliteral(L, "Unexpected string escape sequence in JSON document");
426 return 2;
427 }
428 } else {
429 // normal character:
430 cbuf[outlen++] = c;
431 }
432 }
433 // process buffer to Lua string:
434 luaL_pushresultsize(&luabuf, outlen);
435 } else {
436 // if JSON string is empty,
437 // push empty Lua string:
438 lua_pushliteral(L, "");
439 // consume closing quote:
440 pos++;
441 }
442 // continue with processing of decoded string:
443 goto json_import_process_value;
444 }
445 // process values whose type is is not deducible from a single character:
446 if ((c >= '0' && c <= '9') || c == '-' || c == '+') {
447 // for numbers,
448 // use strtod() call to parse a (double precision) floating point number:
449 double numval;
450 char *endptr;
451 numval = strtod(str+pos, &endptr);
452 // catch parsing errors:
453 if (endptr == str+pos) goto json_import_syntax_error;
454 // consume characters that were parsed:
455 pos += endptr - (str+pos);
456 // push parsed (double precision) floating point number on Lua stack:
457 lua_pushnumber(L, numval);
458 } else if (!strncmp(str+pos, "true", 4)) {
459 // consume 4 input characters for "true":
460 pos += 4;
461 // put Lua true value onto stack:
462 lua_pushboolean(L, 1);
463 } else if (!strncmp(str+pos, "false", 5)) {
464 // consume 5 input characters for "false":
465 pos += 5;
466 // put Lua false value onto stack:
467 lua_pushboolean(L, 0);
468 } else if (!strncmp(str+pos, "null", 4)) {
469 // consume 4 input characters for "null":
470 pos += 4;
471 // different behavor for top-level and sub-levels:
472 if (level) {
473 // if sub-level,
474 // push special null-marker onto stack:
475 json_pushnullmark(L);
476 } else {
477 // if top-level,
478 // push nil onto stack:
479 lua_pushnil(L);
480 }
481 } else {
482 // all other cases are a syntax error:
483 goto json_import_syntax_error;
484 }
485 // process a decoded value or key value pair (expected on top of Lua stack):
486 json_import_process_value:
487 switch (mode) {
488 // an object key has been read:
489 case JSON_STATE_OBJECT_KEY:
490 // if an object key is not a string, then this is a syntax error:
491 if (lua_type(L, -1) != LUA_TSTRING) goto json_import_syntax_error;
492 // expect key terminator to follow:
493 mode = JSON_STATE_OBJECT_KEY_TERMINATOR;
494 // continue with loop:
495 goto json_import_loop;
496 // a key value pair has been read:
497 case JSON_STATE_OBJECT_VALUE:
498 // store key value pair in outer shadow table:
499 lua_rawset(L, -3);
500 // expect value terminator (or end of object) to follow:
501 mode = JSON_STATE_OBJECT_SEPARATOR;
502 // continue with loop:
503 goto json_import_loop;
504 // an array value has been read:
505 case JSON_STATE_ARRAY_VALUE:
506 // get current array length:
507 arraylen = lua_rawlen(L, -3);
508 // throw error if array would exceed INT_MAX elements:
509 // TODO: Lua 5.3 may support more elements
510 if (arraylen >= INT_MAX) {
511 lua_pushnil(L);
512 lua_pushfstring(L, "Array exceeded length of %d elements", INT_MAX);
513 }
514 // store value in outer shadow table:
515 lua_rawseti(L, -3, arraylen + 1);
516 // expect value terminator (or end of object) to follow:
517 mode = JSON_STATE_ARRAY_SEPARATOR;
518 // continue with loop
519 goto json_import_loop;
520 // a single value has been read:
521 case JSON_STATE_VALUE:
522 // leave value on top of stack, expect end of JSON document, and continue with loop:
523 mode = JSON_STATE_END;
524 goto json_import_loop;
525 }
526 // syntax error handling (reachable by goto statement):
527 json_import_syntax_error:
528 lua_pushnil(L);
529 lua_pushliteral(L, "Syntax error in JSON document");
530 return 2;
531 }
533 // special Lua stack indicies for json_path function:
534 #define json_path_shadowtbl_idx 1
536 // stack offset of arguments to json_path function:
537 #define json_path_idxshift 1
539 // gets a value or its type from a JSON document (passed as first argument)
540 // using a path (passed as variable number of keys after first argument):
541 static int json_path(lua_State *L, int type_mode) {
542 int stacktop; // stack index of top of stack (after shifting)
543 int idx = 2 + json_path_idxshift; // stack index of current argument to process
544 // insert shadowtbl into stack at position 1 (shifting the arguments):
545 json_regfetch(L, shadowtbl);
546 lua_insert(L, 1);
547 // store stack index of top of stack:
548 stacktop = lua_gettop(L);
549 // use first argument as "current value" (stored on top of stack):
550 lua_pushvalue(L, 1 + json_path_idxshift);
551 // process each "path key" (2nd argument and following arguments):
552 while (idx <= stacktop) {
553 // if "current value" (on top of stack) is nil, then the path cannot be walked and nil is returned:
554 if (lua_isnil(L, -1)) return 1;
555 // try to get shadow table of "current value":
556 lua_pushvalue(L, -1);
557 lua_rawget(L, json_path_shadowtbl_idx);
558 if (lua_isnil(L, -1)) {
559 // if no shadow table is found,
560 if (lua_type(L, -1) == LUA_TTABLE) {
561 // and if "current value" is a table,
562 // drop nil from stack:
563 lua_pop(L, 1);
564 // get "next value" using the "path key":
565 lua_pushvalue(L, idx++);
566 lua_gettable(L, -2);
567 } else {
568 // if "current value" is not a table,
569 // then the path cannot be walked and nil (already on top of stack) is returned:
570 return 1;
571 }
572 } else {
573 // if a shadow table is found,
574 // set "current value" to its shadow table:
575 lua_replace(L, -2);
576 // get "next value" using the "path key":
577 lua_pushvalue(L, idx++);
578 lua_rawget(L, -2);
579 }
580 // the "next value" replaces the "current value":
581 lua_replace(L, -2);
582 }
583 if (!type_mode) {
584 // if a value (and not its type) was requested,
585 // check if value is the null-marker, and store nil on top of Lua stack in that case:
586 if (json_isnullmark(L, -1)) lua_pushnil(L);
587 } else {
588 // if the type was requested,
589 // check if value is the null-marker:
590 if (json_isnullmark(L, -1)) {
591 // if yes, store string "null" on top of Lua stack:
592 lua_pushliteral(L, "null");
593 } else {
594 // otherwise,
595 // check if metatable indicates "object" or "array":
596 if (lua_getmetatable(L, -1)) {
597 json_regfetch(L, objectmt);
598 if (lua_rawequal(L, -2, -1)) {
599 // if value has metatable for JSON objects,
600 // return string "object":
601 lua_pushliteral(L, "object");
602 return 1;
603 }
604 json_regfetch(L, arraymt);
605 if (lua_rawequal(L, -3, -1)) {
606 // if value has metatable for JSON arrays,
607 // return string "object":
608 lua_pushliteral(L, "array");
609 return 1;
610 }
611 // remove 3 metatables (one of the value, two for comparison) from stack:
612 lua_pop(L, 3);
613 }
614 // otherwise, get the Lua type:
615 lua_pushstring(L, lua_typename(L, lua_type(L, -1)));
616 }
617 }
618 // return the top most value on the Lua stack:
619 return 1;
620 }
622 // gets a value from a JSON document (passed as first argument)
623 // using a path (passed as variable number of keys after first argument):
624 static int json_get(lua_State *L) {
625 return json_path(L, 0);
626 }
628 // gets a value's type from a JSON document (passed as first argument)
629 // using a path (variable number of keys after first argument):
630 static int json_type(lua_State *L) {
631 return json_path(L, 1);
632 }
634 // returns the length of a JSON array (or zero for a table without numeric keys):
635 static int json_len(lua_State *L) {
636 // stack shall contain one function argument:
637 lua_settop(L, 1);
638 // try to get corresponding shadow table for first argument:
639 json_regfetch(L, shadowtbl);
640 lua_pushvalue(L, 1);
641 lua_rawget(L, -2);
642 // if shadow table does not exist, return length of argument, else length of shadow table:
643 lua_pushnumber(L, lua_rawlen(L, lua_isnil(L, -1) ? 1 : -1));
644 return 1;
645 }
647 static int json_index(lua_State *L) {
648 // stack shall contain two function arguments:
649 lua_settop(L, 2);
650 // get corresponding shadow table for first argument:
651 json_regfetch(L, shadowtbl);
652 lua_pushvalue(L, 1);
653 lua_rawget(L, -2);
654 // throw error if no shadow table was found:
655 if (lua_isnil(L, -1)) return luaL_error(L, "Shadow table not found");
656 // use key passed as second argument to lookup value in shadow table:
657 lua_pushvalue(L, 2);
658 lua_rawget(L, -2);
659 // if value is null-marker, then push nil onto stack:
660 if (json_isnullmark(L, -1)) lua_pushnil(L);
661 // return either looked up value, or nil
662 return 1;
663 }
665 static int json_newindex(lua_State *L) {
666 // stack shall contain three function arguments:
667 lua_settop(L, 3);
668 // get corresponding shadow table for first argument:
669 json_regfetch(L, shadowtbl);
670 lua_pushvalue(L, 1);
671 lua_rawget(L, -2);
672 // throw error if no shadow table was found:
673 if (lua_isnil(L, -1)) return luaL_error(L, "Shadow table not found");
674 // replace first argument with shadow table:
675 lua_replace(L, 1);
676 // reset stack and use second and third argument to write to shadow table:
677 lua_settop(L, 3);
678 lua_rawset(L, 1);
679 // return nothing:
680 return 0;
681 }
683 static int json_pairs_iterfunc(lua_State *L) {
684 // stack shall contain two function arguments:
685 lua_settop(L, 2);
686 // get corresponding shadow table for first argument:
687 json_regfetch(L, shadowtbl);
688 lua_pushvalue(L, 1);
689 lua_rawget(L, -2);
690 // throw error if no shadow table was found:
691 if (lua_isnil(L, -1)) return luaL_error(L, "Shadow table not found");
692 // get next key value pair from shadow table (using previous key from argument 2)
693 // and return nothing if there is no next pair:
694 lua_pushvalue(L, 2);
695 if (!lua_next(L, -2)) return 0;
696 // replace null-marker with nil:
697 if (json_isnullmark(L, -1)) {
698 lua_pop(L, 1);
699 lua_pushnil(L);
700 }
701 // return key and value (or key and nil, if null-marker was found):
702 return 2;
703 }
705 // returns a triple such that 'for key, value in pairs(obj) do ... end'
706 // iterates through all key value pairs (including JSON null keys represented as Lua nil):
707 static int json_pairs(lua_State *L) {
708 // return triple of function json_pairs_iterfunc, first argument, and nil:
709 lua_pushcfunction(L, json_pairs_iterfunc);
710 lua_pushvalue(L, 1);
711 lua_pushnil(L);
712 return 3;
713 }
715 static int json_ipairs_iterfunc(lua_State *L) {
716 lua_Integer idx;
717 // stack shall contain two function arguments:
718 lua_settop(L, 2);
719 // calculate new index by incrementing second argument:
720 idx = lua_tointeger(L, 2) + 1;
721 // get corresponding shadow table for first argument:
722 json_regfetch(L, shadowtbl);
723 lua_pushvalue(L, 1);
724 lua_rawget(L, -2);
725 // throw error if no shadow table was found:
726 if (lua_isnil(L, -1)) return luaL_error(L, "Shadow table not found");
727 // do integer lookup in shadow table:
728 lua_rawgeti(L, -1, idx);
729 // return nothing if there was no value:
730 if (lua_isnil(L, -1)) return 0;
731 // return new index and
732 // either the looked up value if it is not equal to the null-marker
733 // or nil instead of null-marker:
734 lua_pushinteger(L, idx);
735 if (json_isnullmark(L, -2)) lua_pushnil(L);
736 else lua_pushvalue(L, -2);
737 return 2;
738 }
740 // returns a triple such that 'for idx, value in ipairs(ary) do ... end'
741 // iterates through all values (including JSON null represented as Lua nil):
742 static int json_ipairs(lua_State *L) {
743 // return triple of function json_ipairs_iterfunc, first argument, and zero:
744 lua_pushcfunction(L, json_ipairs_iterfunc);
745 lua_pushvalue(L, 1);
746 lua_pushinteger(L, 0);
747 return 3;
748 }
750 typedef struct {
751 size_t length;
752 const char *data;
753 } json_key_t;
755 static int json_key_cmp(json_key_t *key1, json_key_t *key2) {
756 size_t pos = 0;
757 unsigned char c1, c2;
758 while (1) {
759 if (key1->length > pos) {
760 if (key2->length > pos) {
761 c1 = key1->data[pos];
762 c2 = key2->data[pos];
763 if (c1 < c2) return -1;
764 else if (c1 > c2) return 1;
765 } else {
766 return 1;
767 }
768 } else {
769 if (key2->length > pos) {
770 return -1;
771 } else {
772 return 0;
773 }
774 }
775 pos++;
776 }
777 }
779 #define JSON_TABLETYPE_UNKNOWN 0
780 #define JSON_TABLETYPE_OBJECT 1
781 #define JSON_TABLETYPE_ARRAY 2
783 #define json_export_internal_indentstring_idx 1
784 #define json_export_internal_level_idx 2
785 #define json_export_internal_value_idx 3
786 #define json_export_internal_tmp_idx 4
788 static int json_export_internal(lua_State *L) {
789 int level;
790 int pretty;
791 int i;
792 lua_Number num;
793 const char *str;
794 unsigned char c;
795 size_t strlen;
796 size_t pos = 0;
797 luaL_Buffer buf;
798 char hexcode[7]; // backslash, character 'u', 4 hex digits, and terminating NULL byte
799 int tabletype = JSON_TABLETYPE_UNKNOWN;
800 int anyelement = 0;
801 size_t keycount = 0;
802 size_t keypos = 0;
803 json_key_t *keybuf = NULL;
804 lua_Integer idx;
805 lua_settop(L, json_export_internal_value_idx);
806 if (json_isnullmark(L, json_export_internal_value_idx)) {
807 lua_pop(L, 1);
808 lua_pushnil(L);
809 }
810 switch (lua_type(L, json_export_internal_value_idx)) {
811 case LUA_TNIL:
812 lua_pushliteral(L, "null");
813 return 1;
814 case LUA_TNUMBER:
815 num = lua_tonumber(L, json_export_internal_value_idx);
816 if (isnan(num)) return luaL_error(L, "JSON export not possible for NaN value");
817 if (isinf(num)) return luaL_error(L, "JSON export not possible for infinite numbers");
818 lua_tostring(L, json_export_internal_value_idx);
819 return 1;
820 case LUA_TBOOLEAN:
821 if (lua_toboolean(L, json_export_internal_value_idx)) {
822 lua_pushliteral(L, "true");
823 } else {
824 lua_pushliteral(L, "false");
825 }
826 return 1;
827 case LUA_TSTRING:
828 str = lua_tolstring(L, 3, &strlen);
829 luaL_buffinit(L, &buf);
830 luaL_addchar(&buf, '"');
831 while (pos < strlen) {
832 c = str[pos++];
833 if (c == '"') luaL_addstring(&buf, "\\\"");
834 else if (c == '\\') luaL_addstring(&buf, "\\\\");
835 else if (c == 127) luaL_addstring(&buf, "\\u007F");
836 else if (c >= 32) luaL_addchar(&buf, c);
837 else if (c == '\b') luaL_addstring(&buf, "\\b");
838 else if (c == '\f') luaL_addstring(&buf, "\\f");
839 else if (c == '\n') luaL_addstring(&buf, "\\n");
840 else if (c == '\r') luaL_addstring(&buf, "\\r");
841 else if (c == '\t') luaL_addstring(&buf, "\\t");
842 else if (c == '\v') luaL_addstring(&buf, "\\v");
843 else {
844 sprintf(hexcode, "\\u%04X", c);
845 luaL_addstring(&buf, hexcode);
846 }
847 }
848 luaL_addchar(&buf, '"');
849 luaL_pushresult(&buf);
850 return 1;
851 case LUA_TTABLE:
852 if (lua_getmetatable(L, json_export_internal_value_idx)) {
853 json_regfetch(L, objectmt);
854 if (lua_rawequal(L, -2, -1)) {
855 tabletype = JSON_TABLETYPE_OBJECT;
856 } else {
857 json_regfetch(L, arraymt);
858 if (lua_rawequal(L, -3, -1)) {
859 tabletype = JSON_TABLETYPE_ARRAY;
860 } else {
861 return luaL_error(L, "JSON export not possible for tables with nonsupported metatable");
862 }
863 }
864 }
865 json_regfetch(L, shadowtbl);
866 lua_pushvalue(L, json_export_internal_value_idx);
867 lua_rawget(L, -2);
868 if (!lua_isnil(L, -1)) lua_replace(L, json_export_internal_value_idx);
869 lua_settop(L, json_export_internal_value_idx);
870 if (tabletype == JSON_TABLETYPE_UNKNOWN) {
871 for (lua_pushnil(L); lua_next(L, json_export_internal_value_idx); lua_pop(L, 1)) {
872 switch (lua_type(L, -2)) {
873 case LUA_TSTRING:
874 keycount++;
875 if (tabletype == JSON_TABLETYPE_UNKNOWN) tabletype = JSON_TABLETYPE_OBJECT;
876 else if (tabletype == JSON_TABLETYPE_ARRAY) goto json_export_tabletype_error;
877 break;
878 case LUA_TNUMBER:
879 if (tabletype == JSON_TABLETYPE_UNKNOWN) tabletype = JSON_TABLETYPE_ARRAY;
880 else if (tabletype == JSON_TABLETYPE_OBJECT) goto json_export_tabletype_error;
881 break;
882 }
883 }
884 }
885 pretty = lua_toboolean(L, json_export_internal_indentstring_idx);
886 level = lua_tointeger(L, json_export_internal_level_idx) + 1;
887 if (level > JSON_MAXDEPTH) {
888 return luaL_error(L, "More than %d nested JSON levels", JSON_MAXDEPTH);
889 }
890 switch (tabletype) {
891 case JSON_TABLETYPE_OBJECT:
892 if (!keycount) {
893 for (lua_pushnil(L); lua_next(L, json_export_internal_value_idx); lua_pop(L, 1)) {
894 if (lua_type(L, -2) == LUA_TSTRING) keycount++;
895 }
896 }
897 if (keycount) {
898 keybuf = calloc(keycount, sizeof(json_key_t));
899 if (!keybuf) return luaL_error(L, "Memory allocation failed in JSON library");
900 for (lua_pushnil(L); lua_next(L, json_export_internal_value_idx); lua_pop(L, 1)) {
901 if (lua_type(L, -2) == LUA_TSTRING) {
902 json_key_t *key = keybuf + (keypos++);
903 key->data = lua_tolstring(L, -2, &key->length);
904 }
905 }
906 qsort(keybuf, keycount, sizeof(json_key_t), (void *)json_key_cmp);
907 }
908 luaL_buffinit(L, &buf);
909 luaL_addchar(&buf, '{');
910 for (keypos=0; keypos<keycount; keypos++) {
911 json_key_t *key = keybuf + keypos;
912 if (keypos) luaL_addchar(&buf, ',');
913 if (pretty) {
914 luaL_addchar(&buf, '\n');
915 for (i=0; i<level; i++) {
916 lua_pushvalue(L, json_export_internal_indentstring_idx);
917 luaL_addvalue(&buf);
918 }
919 }
920 lua_pushcfunction(L, json_export_internal);
921 lua_pushvalue(L, json_export_internal_indentstring_idx);
922 lua_pushinteger(L, level);
923 lua_pushlstring(L, key->data, key->length);
924 if (lua_pcall(L, 3, 1, 0)) {
925 if (keybuf) free(keybuf);
926 return lua_error(L);
927 }
928 luaL_addvalue(&buf);
929 luaL_addchar(&buf, ':');
930 if (pretty) luaL_addchar(&buf, ' ');
931 lua_pushcfunction(L, json_export_internal);
932 lua_pushvalue(L, json_export_internal_indentstring_idx);
933 lua_pushinteger(L, level);
934 lua_pushlstring(L, key->data, key->length);
935 lua_rawget(L, json_export_internal_value_idx);
936 if (lua_pcall(L, 3, 1, 0)) {
937 if (keybuf) free(keybuf);
938 return lua_error(L);
939 }
940 luaL_addvalue(&buf);
941 }
942 if (keybuf) free(keybuf);
943 if (pretty && keycount != 0) {
944 luaL_addchar(&buf, '\n');
945 for (i=0; i<level-1; i++) {
946 lua_pushvalue(L, json_export_internal_indentstring_idx);
947 luaL_addvalue(&buf);
948 }
949 }
950 luaL_addchar(&buf, '}');
951 if (pretty && level == 1) luaL_addchar(&buf, '\n');
952 luaL_pushresult(&buf);
953 return 1;
954 case JSON_TABLETYPE_ARRAY:
955 lua_settop(L, json_export_internal_tmp_idx);
956 luaL_buffinit(L, &buf);
957 luaL_addchar(&buf, '[');
958 for (idx = 1; ; idx++) {
959 lua_rawgeti(L, json_export_internal_value_idx, idx);
960 if (lua_isnil(L, -1)) {
961 lua_pop(L, 1);
962 break;
963 }
964 lua_replace(L, json_export_internal_tmp_idx);
965 if (anyelement) luaL_addchar(&buf, ',');
966 anyelement = 1;
967 if (pretty) {
968 luaL_addchar(&buf, '\n');
969 for (i=0; i<level; i++) {
970 lua_pushvalue(L, json_export_internal_indentstring_idx);
971 luaL_addvalue(&buf);
972 }
973 }
974 lua_pushcfunction(L, json_export_internal);
975 lua_pushvalue(L, json_export_internal_indentstring_idx);
976 lua_pushinteger(L, level);
977 lua_pushvalue(L, json_export_internal_tmp_idx);
978 lua_call(L, 3, 1);
979 luaL_addvalue(&buf);
980 }
981 if (pretty && anyelement) {
982 luaL_addchar(&buf, '\n');
983 for (i=0; i<level-1; i++) {
984 lua_pushvalue(L, json_export_internal_indentstring_idx);
985 luaL_addvalue(&buf);
986 }
987 }
988 luaL_addchar(&buf, ']');
989 if (pretty && level == 1) luaL_addchar(&buf, '\n');
990 luaL_pushresult(&buf);
991 return 1;
992 }
993 json_export_tabletype_error:
994 return luaL_error(L, "JSON export not possible for ambiguous table (cannot decide whether it is an object or array)");
995 }
996 return luaL_error(L, "JSON export not possible for values of type \"%s\"", lua_typename(L, lua_type(L, json_export_internal_value_idx)));
997 }
999 static int json_export(lua_State *L) {
1000 lua_settop(L, 1);
1001 lua_pushcfunction(L, json_export_internal);
1002 lua_pushnil(L);
1003 lua_pushinteger(L, 0);
1004 lua_pushvalue(L, 1);
1005 lua_call(L, 3, 1);
1006 return 1;
1009 static int json_pretty(lua_State *L) {
1010 lua_settop(L, 2);
1011 lua_pushcfunction(L, json_export_internal);
1012 if (lua_isnil(L, 2)) lua_pushliteral(L, " ");
1013 else lua_pushvalue(L, 2);
1014 lua_pushinteger(L, 0);
1015 lua_pushvalue(L, 1);
1016 lua_call(L, 3, 1);
1017 return 1;
1020 // functions in library module:
1021 static const struct luaL_Reg json_module_functions[] = {
1022 {"object", json_object},
1023 {"array", json_array},
1024 {"import", json_import},
1025 {"export", json_export},
1026 {"pretty", json_pretty},
1027 {"get", json_get},
1028 {"type", json_type},
1029 {NULL, NULL}
1030 };
1032 // metamethods for JSON objects, JSON arrays, and unknown JSON collections (object or array):
1033 static const struct luaL_Reg json_metatable_functions[] = {
1034 {"__len", json_len},
1035 {"__index", json_index},
1036 {"__newindex", json_newindex},
1037 {"__pairs", json_pairs},
1038 {"__ipairs", json_ipairs},
1039 {"__tostring", json_export},
1040 {NULL, NULL}
1041 };
1043 // metamethods for JSON null marker:
1044 static const struct luaL_Reg json_nullmark_metamethods[] = {
1045 {"__tostring", json_nullmark_tostring},
1046 {NULL, NULL}
1047 };
1049 // initializes json library:
1050 int luaopen_json(lua_State *L) {
1051 // empty stack:
1052 lua_settop(L, 0);
1053 // push library module onto stack position 1:
1054 lua_newtable(L);
1055 // register library functions:
1056 luaL_setfuncs(L, json_module_functions, 0);
1057 // create and store objectmt:
1058 lua_newtable(L);
1059 luaL_setfuncs(L, json_metatable_functions, 0);
1060 json_regstore(L, objectmt);
1061 // create and store arraymt:
1062 lua_newtable(L);
1063 luaL_setfuncs(L, json_metatable_functions, 0);
1064 json_regstore(L, arraymt);
1065 // create and store ephemeron table to store shadow tables for each JSON object/array
1066 // to allow NULL values returned as nil
1067 lua_newtable(L);
1068 lua_newtable(L); // metatable for ephemeron table
1069 lua_pushliteral(L, "__mode");
1070 lua_pushliteral(L, "k");
1071 lua_rawset(L, -3);
1072 lua_setmetatable(L, -2);
1073 json_regstore(L, shadowtbl);
1074 // set metatable of null marker and make it available through library module:
1075 json_pushnullmark(L);
1076 lua_newtable(L);
1077 luaL_setfuncs(L, json_nullmark_metamethods, 0);
1078 lua_setmetatable(L, -2);
1079 lua_setfield(L, 1, "null");
1080 // return library module (that's expected on top of stack):
1081 return 1;

Impressum / About Us