webmcp

view libraries/json/json.c @ 171:ce208edffcc9

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

Impressum / About Us