webmcp

view libraries/json/json.c @ 193:0014a7c22013

Improved performance of JSON library by storing shadow tables directly via a lightuserdata key instead of using ephemeron tables
author jbe
date Mon Aug 11 13:18:49 2014 +0200 (2014-08-11)
parents 33c8f7029cfa
children 654ddbcc49d0
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: json_import can store 2^32 / 3 levels on stack swap (using
9 // also negative indicies after integer wraparound), and
10 // json_export can store even more levels, so 1024^3 =
11 // 1073741824 is a safe value and allows practically unlimited
12 // levels for JSON documents <= 2 GiB.
13 #define JSON_MAXDEPTH (1024*1024*1024)
15 // define type JSON_LIGHTUSERDATA and
16 // generate dummy memory addresses for lightuserdata values:
17 #define JSON_LIGHTUSERDATA char
18 static struct {
19 JSON_LIGHTUSERDATA nullmark; // lightuserdata value represents a NULL value
20 JSON_LIGHTUSERDATA shadowtbl; // lightuserdata key for shadow table
21 } json_lightuserdata;
23 // macros for special nullmark value:
24 #define json_isnullmark(L, i) (lua_touserdata((L), (i)) == &json_lightuserdata.nullmark)
25 #define json_pushnullmark(L) lua_pushlightuserdata((L), &json_lightuserdata.nullmark)
27 // macros for getting and setting shadow tables
28 #define json_setshadow(L, i) lua_rawsetp((L), (i), &json_lightuserdata.shadowtbl)
29 #define json_getshadow(L, i) lua_rawgetp((L), (i), &json_lightuserdata.shadowtbl)
30 #define json_createproxy(L) lua_createtable((L), 0, 1)
32 // generate additional dummy memory addresses that represent Lua objects
33 // via lightuserdata keys and LUA_REGISTRYINDEX:
34 static struct {
35 JSON_LIGHTUSERDATA objectmt; // metatable for JSON objects
36 JSON_LIGHTUSERDATA arraymt; // metatable for JSON arrays
37 } json_registry;
39 // macros for usage of Lua registry:
40 #define json_regpointer(x) (&json_registry.x)
41 #define json_regfetchpointer(L, x) lua_rawgetp((L), LUA_REGISTRYINDEX, (x))
42 #define json_regfetch(L, x) json_regfetchpointer(L, json_regpointer(x))
43 #define json_regstore(L, x) lua_rawsetp(L, LUA_REGISTRYINDEX, json_regpointer(x))
45 // returns the string "<JSON null marker>":
46 static int json_nullmark_tostring(lua_State *L) {
47 lua_pushliteral(L, "<JSON null marker>");
48 return 1;
49 }
51 #define json_convert_source_idx 1
52 #define json_convert_iterator_idx 2
53 #define json_convert_output_idx 3
54 #define json_convert_shadow_idx 4
55 #define json_convert_iterfun_idx 5
56 #define json_convert_itertbl_idx 6
58 // converts a Lua table (or any other iterable value) to a JSON object or JSON array:
59 // (does never modify the argument, returns an empty object or array if argument is nil)
60 static int json_convert(lua_State *L, int array) {
61 int arrayidx = 0;
62 // determine is argument is given:
63 if (lua_isnoneornil(L, json_convert_source_idx)) {
64 // if no argument is given (or if argument is nil),
65 // create proxy table with shadow table, and leave proxy table on top of stack:
66 json_createproxy(L);
67 lua_newtable(L);
68 json_setshadow(L, -2);
69 } else {
70 // if an argument was given,
71 // stack shall contain only one function argument:
72 lua_settop(L, 1);
73 // check if there is an iterator function in its metatable:
74 if (luaL_getmetafield(L, json_convert_source_idx, array ? "__ipairs" : "__pairs")) {
75 // if there is an iterator function,
76 // leave it on stack position 2 and verify its type:
77 if (lua_type(L, json_convert_iterator_idx) != LUA_TFUNCTION)
78 return luaL_error(L, "%s metamethod is not a function", array ? "__ipairs" : "__pairs");
79 } else {
80 // if there is no iterator function,
81 // verify the type of the argument itself:
82 luaL_checktype(L, json_convert_source_idx, LUA_TTABLE);
83 // push nil onto stack position 2:
84 lua_pushnil(L);
85 }
86 // create result table on stack position 3:
87 json_createproxy(L);
88 // create shadow table on stack position 4:
89 lua_newtable(L);
90 lua_pushvalue(L, -1);
91 json_setshadow(L, -3);
92 // check if iterator function exists:
93 if (lua_isnil(L, json_convert_iterator_idx)) {
94 // if there is no iterator function,
95 // distinguish between objects and arrays:
96 if (array == 0) {
97 // for an object, copy all string key value pairs to shadow table:
98 for (lua_pushnil(L); lua_next(L, json_convert_source_idx); lua_pop(L, 1)) {
99 if (lua_type(L, -2) == LUA_TSTRING) {
100 lua_pushvalue(L, -2);
101 lua_pushvalue(L, -2);
102 lua_rawset(L, json_convert_shadow_idx);
103 }
104 }
105 } else {
106 // for an array, copy consecutive integer value pairs to shadow table:
107 while (1) {
108 // throw error if array would exceed INT_MAX elements:
109 // TODO: Lua 5.3 may support more elements
110 if (arrayidx == INT_MAX) {
111 lua_pushnumber(L, (size_t)INT_MAX+1);
112 lua_rawget(L, json_convert_source_idx);
113 if (lua_isnil(L, -1)) break;
114 return luaL_error(L, "Array exceeded length of %d elements", INT_MAX);
115 }
116 // get next array entry:
117 arrayidx++;
118 lua_rawgeti(L, json_convert_source_idx, arrayidx);
119 // break if value is nil:
120 if (lua_isnil(L, -1)) break;
121 // store value in shadow table:
122 lua_rawseti(L, json_convert_shadow_idx, arrayidx);
123 }
124 }
125 } else {
126 // if there is an iterator function,
127 // call iterator function with source value (first argument)
128 // and store 3 result values on stack positions 5 through 7:
129 lua_pushvalue(L, json_convert_iterator_idx);
130 lua_pushvalue(L, 1);
131 lua_call(L, 1, 3);
132 // iterate through key value pairs and store some of them in shadow table
133 // while replacing nil values with null-marker:
134 while (1) {
135 // call iterfun function:
136 lua_pushvalue(L, json_convert_iterfun_idx);
137 lua_pushvalue(L, json_convert_itertbl_idx);
138 lua_pushvalue(L, -3);
139 lua_remove(L, -4);
140 lua_call(L, 2, 2);
141 // break iteration loop if key is nil:
142 if (lua_isnil(L, -2)) break;
143 // store key value pair only if key type is correct:
144 if (lua_type(L, -2) == (array ? LUA_TNUMBER : LUA_TSTRING)) {
145 // if key type is correct,
146 // push key onto stack:
147 lua_pushvalue(L, -2);
148 // if value is nil, push null-marker onto stack (as value):
149 if (lua_isnil(L, -2)) json_pushnullmark(L);
150 // else push value onto stack:
151 else lua_pushvalue(L, -2);
152 // set key value pair in shadow table:
153 lua_rawset(L, json_convert_shadow_idx);
154 }
155 // pop value from stack, but leave key on stack:
156 lua_pop(L, 1);
157 }
158 }
159 // let result table be on top of stack:
160 lua_settop(L, json_convert_output_idx);
161 }
162 // set metatable (for result table on top of stack):
163 if (array == 0) json_regfetch(L, objectmt);
164 else json_regfetch(L, arraymt);
165 lua_setmetatable(L, -2);
166 // return table on top of stack:
167 return 1;
168 }
170 // converts a Lua table (or any other iterable value) to a JSON object:
171 // (does never modify the argument, returns an empty object or array if argument is nil)
172 static int json_object(lua_State *L) {
173 return json_convert(L, 0);
174 }
176 // converts a Lua table (or any other iterable value) to a JSON array:
177 // (does never modify the argument, returns an empty object or array if argument is nil)
178 static int json_array(lua_State *L) {
179 return json_convert(L, 1);
180 }
182 // internal states of JSON parser:
183 #define JSON_STATE_VALUE 0
184 #define JSON_STATE_OBJECT_KEY 1
185 #define JSON_STATE_OBJECT_KEY_TERMINATOR 2
186 #define JSON_STATE_OBJECT_VALUE 3
187 #define JSON_STATE_OBJECT_SEPARATOR 4
188 #define JSON_STATE_ARRAY_VALUE 5
189 #define JSON_STATE_ARRAY_SEPARATOR 6
190 #define JSON_STATE_END 7
192 // special Lua stack indicies for json_import function:
193 #define json_import_objectmt_idx 2
194 #define json_import_arraymt_idx 3
195 #define json_import_stackswap_idx 4
197 // macros for hex decoding:
198 #define json_utf16_surrogate(x) ((x) >= 0xD800 && (x) <= 0xDFFF)
199 #define json_utf16_lead(x) ((x) >= 0xD800 && (x) <= 0xDBFF)
200 #define json_utf16_tail(x) ((x) >= 0xDC00 && (x) <= 0xDFFF)
201 #define json_import_readhex(x) \
202 do { \
203 x = 0; \
204 for (i=0; i<4; i++) { \
205 x <<= 4; \
206 c = str[pos++]; \
207 if (c >= '0' && c <= '9') x += c - '0'; \
208 else if (c >= 'A' && c <= 'F') x += c - 'A' + 10; \
209 else if (c >= 'a' && c <= 'f') x += c - 'a' + 10; \
210 else if (c == 0) goto json_import_unexpected_eof; \
211 else goto json_import_unexpected_escape; \
212 } \
213 } while (0)
215 // decodes a JSON document:
216 static int json_import(lua_State *L) {
217 int stackswapidx = 0; // elements in stack swap table
218 int i; // loop variable
219 const char *str; // string to parse
220 size_t total; // total length of string to parse
221 size_t pos = 0; // current position in string to parse
222 size_t level = 0; // nested levels of objects/arrays currently being processed
223 int mode = JSON_STATE_VALUE; // state of parser (i.e. "what's expected next?")
224 unsigned char c; // variable to store a single character to be processed (unsigned!)
225 luaL_Buffer luabuf; // Lua buffer to decode JSON string values
226 char *cbuf; // C buffer to decode JSON string values
227 size_t outlen; // maximum length or write position of C buffer
228 long codepoint; // decoded UTF-16 character or higher codepoint
229 long utf16tail; // second decoded UTF-16 character (surrogate tail)
230 size_t arraylen; // variable to temporarily store the array length
231 // require string as argument and convert to C string with length information:
232 str = luaL_checklstring(L, 1, &total);
233 // if string contains a NULL byte, this is a syntax error
234 if (strlen(str) != total) goto json_import_syntax_error;
235 // stack shall contain one function argument:
236 lua_settop(L, 1);
237 // push objectmt onto stack position 2:
238 json_regfetch(L, objectmt);
239 // push arraymt onto stack position 3:
240 json_regfetch(L, arraymt);
241 // push table for stack swapping onto stack position 5:
242 // (needed to avoid Lua stack overflows)
243 lua_newtable(L);
244 // main loop of parser:
245 json_import_loop:
246 // skip whitespace and store next character in variable 'c':
247 while (c = str[pos],
248 c == ' ' ||
249 c == '\f' ||
250 c == '\n' ||
251 c == '\r' ||
252 c == '\t' ||
253 c == '\v'
254 ) pos++;
255 // NOTE: variable c needs to be unsigned in the following code
256 // switch statement to handle certain (single) characters:
257 switch (c) {
258 // handle end of JSON document:
259 case 0:
260 // if end of JSON document was expected, then return top element of stack as result:
261 if (mode == JSON_STATE_END) return 1;
262 // otherwise, the JSON document was malformed:
263 if (level == 0) {
264 lua_pushnil(L);
265 lua_pushliteral(L, "Empty string");
266 } else {
267 json_import_unexpected_eof:
268 lua_pushnil(L);
269 lua_pushliteral(L, "Unexpected end of JSON document");
270 }
271 return 2;
272 // new JSON object or JSON array:
273 case '{':
274 case '[':
275 // if an encountered JSON object is not expected here, then return an error:
276 if (
277 c == '{' &&
278 mode != JSON_STATE_VALUE &&
279 mode != JSON_STATE_OBJECT_VALUE &&
280 mode != JSON_STATE_ARRAY_VALUE
281 ) goto json_import_syntax_error;
282 // if an encountered JSON array is not expected here, then return an error:
283 if (
284 c == '[' &&
285 mode != JSON_STATE_VALUE &&
286 mode != JSON_STATE_OBJECT_VALUE &&
287 mode != JSON_STATE_ARRAY_VALUE
288 ) goto json_import_syntax_error;
289 // consume input character:
290 pos++;
291 // limit nested levels:
292 if (level >= JSON_MAXDEPTH) {
293 lua_pushnil(L);
294 lua_pushfstring(L, "More than %d nested JSON levels", JSON_MAXDEPTH);
295 return 2;
296 }
297 // swap Lua stack entries for previous level to swap table:
298 // (avoids depth limitations due to Lua stack size)
299 if (level) {
300 lua_rawseti(L, json_import_stackswap_idx, ++stackswapidx);
301 lua_rawseti(L, json_import_stackswap_idx, ++stackswapidx);
302 lua_rawseti(L, json_import_stackswap_idx, ++stackswapidx);
303 }
304 // increment level:
305 level++;
306 // create JSON object or JSON array on stack:
307 lua_newtable(L);
308 // set metatable of JSON object or JSON array:
309 lua_pushvalue(L, c == '{' ? json_import_objectmt_idx : json_import_arraymt_idx);
310 lua_setmetatable(L, -2);
311 // create internal shadow table on stack:
312 lua_newtable(L);
313 // register internal shadow table:
314 lua_pushvalue(L, -1);
315 json_setshadow(L, -3);
316 // distinguish between JSON objects and JSON arrays:
317 if (c == '{') {
318 // if JSON object,
319 // expect object key (or end of object) to follow:
320 mode = JSON_STATE_OBJECT_KEY;
321 } else {
322 // if JSON array,
323 // expect array value (or end of array) to follow:
324 mode = JSON_STATE_ARRAY_VALUE;
325 // add nil as key (needed to keep stack balance) and as magic to detect arrays:
326 if (c == '[') lua_pushnil(L);
327 }
328 goto json_import_loop;
329 // end of JSON object:
330 case '}':
331 // if end of JSON object is not expected here, then return an error:
332 if (
333 mode != JSON_STATE_OBJECT_KEY &&
334 mode != JSON_STATE_OBJECT_SEPARATOR
335 ) goto json_import_syntax_error;
336 // jump to common code for end of JSON object and JSON array:
337 goto json_import_close;
338 // end of JSON array:
339 case ']':
340 // if end of JSON array is not expected here, then return an error:
341 if (
342 mode != JSON_STATE_ARRAY_VALUE &&
343 mode != JSON_STATE_ARRAY_SEPARATOR
344 ) goto json_import_syntax_error;
345 // pop nil key/magic (that was needed to keep stack balance):
346 lua_pop(L, 1);
347 // continue with common code for end of JSON object and JSON array:
348 // common code for end of JSON object or JSON array:
349 json_import_close:
350 // consume input character:
351 pos++;
352 // pop shadow table:
353 lua_pop(L, 1);
354 // check if nested:
355 if (--level) {
356 // if nested,
357 // restore previous stack elements from stack swap:
358 lua_rawgeti(L, json_import_stackswap_idx, stackswapidx--);
359 lua_insert(L, -2);
360 lua_rawgeti(L, json_import_stackswap_idx, stackswapidx--);
361 lua_insert(L, -2);
362 lua_rawgeti(L, json_import_stackswap_idx, stackswapidx--);
363 lua_insert(L, -2);
364 // check if outer(!) structure is an array or object:
365 if (lua_isnil(L, -2)) {
366 // select array value processing:
367 mode = JSON_STATE_ARRAY_VALUE;
368 } else {
369 // select object value processing:
370 mode = JSON_STATE_OBJECT_VALUE;
371 }
372 // store value in outer structure:
373 goto json_import_process_value;
374 }
375 // if not nested, then expect end of JSON document and continue with loop:
376 mode = JSON_STATE_END;
377 goto json_import_loop;
378 // key terminator:
379 case ':':
380 // if key terminator is not expected here, then return an error:
381 if (mode != JSON_STATE_OBJECT_KEY_TERMINATOR)
382 goto json_import_syntax_error;
383 // consume input character:
384 pos++;
385 // expect object value to follow:
386 mode = JSON_STATE_OBJECT_VALUE;
387 // continue with loop:
388 goto json_import_loop;
389 // value terminator (NOTE: trailing comma at end of value or key-value list is tolerated by this parser)
390 case ',':
391 // branch according to parser state:
392 if (mode == JSON_STATE_OBJECT_SEPARATOR) {
393 // expect an object key to follow:
394 mode = JSON_STATE_OBJECT_KEY;
395 } else if (mode == JSON_STATE_ARRAY_SEPARATOR) {
396 // expect an array value to follow:
397 mode = JSON_STATE_ARRAY_VALUE;
398 } else {
399 // if value terminator is not expected here, then return an error:
400 goto json_import_syntax_error;
401 }
402 // consume input character:
403 pos++;
404 // continue with loop:
405 goto json_import_loop;
406 // string literal:
407 case '"':
408 // consume quote character:
409 pos++;
410 // find last character in input string:
411 outlen = pos;
412 while ((c = str[outlen]) != '"') {
413 // consume one character:
414 outlen++;
415 // handle unexpected end of JSON document:
416 if (c == 0) goto json_import_unexpected_eof;
417 // consume one extra character when encountering an escaped quote:
418 else if (c == '\\' && str[outlen] == '"') outlen++;
419 }
420 // determine buffer length:
421 outlen -= pos;
422 // check if string is non empty:
423 if (outlen) {
424 // prepare buffer to decode string (with maximum possible length) and set write position to zero:
425 cbuf = luaL_buffinitsize(L, &luabuf, outlen);
426 outlen = 0;
427 // loop through the characters until encountering end quote:
428 while ((c = str[pos++]) != '"') {
429 // NOTE: unexpected end cannot happen anymore
430 if (c < 32 || c == 127) {
431 // do not allow ASCII control characters:
432 // NOTE: illegal UTF-8 sequences and extended control characters are not sanitized
433 // by this parser to allow different encodings than Unicode
434 lua_pushnil(L);
435 lua_pushliteral(L, "Unexpected control character in JSON string");
436 return 2;
437 } else if (c == '\\') {
438 // read next char after backslash escape:
439 c = str[pos++];
440 switch (c) {
441 // unexpected end-of-string:
442 case 0:
443 goto json_import_unexpected_eof;
444 // unescaping of quotation mark, slash, and backslash:
445 case '"':
446 case '/':
447 case '\\':
448 cbuf[outlen++] = c;
449 break;
450 // unescaping of backspace:
451 case 'b': cbuf[outlen++] = '\b'; break;
452 // unescaping of form-feed:
453 case 'f': cbuf[outlen++] = '\f'; break;
454 // unescaping of new-line:
455 case 'n': cbuf[outlen++] = '\n'; break;
456 // unescaping of carriage-return:
457 case 'r': cbuf[outlen++] = '\r'; break;
458 // unescaping of tabulator:
459 case 't': cbuf[outlen++] = '\t'; break;
460 // unescaping of UTF-16 characters
461 case 'u':
462 // decode 4 hex nibbles:
463 json_import_readhex(codepoint);
464 // handle surrogate character:
465 if (json_utf16_surrogate(codepoint)) {
466 // check if first surrogate is in valid range:
467 if (json_utf16_lead(codepoint)) {
468 // require second surrogate:
469 if ((c = str[pos++]) != '\\' || (c = str[pos++]) != 'u') {
470 if (c == 0) goto json_import_unexpected_eof;
471 else goto json_import_wrong_surrogate;
472 }
473 // read 4 hex nibbles of second surrogate character:
474 json_import_readhex(utf16tail);
475 // check if second surrogate is in valid range:
476 if (!json_utf16_tail(utf16tail)) goto json_import_wrong_surrogate;
477 // calculate codepoint:
478 codepoint = 0x10000 + (utf16tail - 0xDC00) + (codepoint - 0xD800) * 0x400;
479 } else {
480 // throw error for wrong surrogates:
481 json_import_wrong_surrogate:
482 lua_pushnil(L);
483 lua_pushliteral(L, "Illegal UTF-16 surrogate in JSON string escape sequence");
484 return 2;
485 }
486 }
487 // encode as UTF-8:
488 if (codepoint < 0x80) {
489 cbuf[outlen++] = (char)codepoint;
490 } else if (codepoint < 0x800) {
491 cbuf[outlen++] = (char)(0xc0 | (codepoint >> 6));
492 cbuf[outlen++] = (char)(0x80 | (codepoint & 0x3f));
493 } else if (codepoint < 0x10000) {
494 cbuf[outlen++] = (char)(0xe0 | (codepoint >> 12));
495 cbuf[outlen++] = (char)(0x80 | ((codepoint >> 6) & 0x3f));
496 cbuf[outlen++] = (char)(0x80 | (codepoint & 0x3f));
497 } else {
498 cbuf[outlen++] = (char)(0xf0 | (codepoint >> 18));
499 cbuf[outlen++] = (char)(0x80 | ((codepoint >> 12) & 0x3f));
500 cbuf[outlen++] = (char)(0x80 | ((codepoint >> 6) & 0x3f));
501 cbuf[outlen++] = (char)(0x80 | (codepoint & 0x3f));
502 }
503 break;
504 // unexpected escape sequence:
505 default:
506 json_import_unexpected_escape:
507 lua_pushnil(L);
508 lua_pushliteral(L, "Unexpected string escape sequence in JSON document");
509 return 2;
510 }
511 } else {
512 // normal character:
513 cbuf[outlen++] = c;
514 }
515 }
516 // process buffer to Lua string:
517 luaL_pushresultsize(&luabuf, outlen);
518 } else {
519 // if JSON string is empty,
520 // push empty Lua string:
521 lua_pushliteral(L, "");
522 // consume closing quote:
523 pos++;
524 }
525 // continue with processing of decoded string:
526 goto json_import_process_value;
527 }
528 // process values whose type is is not deducible from a single character:
529 if ((c >= '0' && c <= '9') || c == '-' || c == '+') {
530 // for numbers,
531 // use strtod() call to parse a (double precision) floating point number:
532 double numval;
533 char *endptr;
534 numval = strtod(str+pos, &endptr);
535 // catch parsing errors:
536 if (endptr == str+pos) goto json_import_syntax_error;
537 // consume characters that were parsed:
538 pos += endptr - (str+pos);
539 // push parsed (double precision) floating point number on Lua stack:
540 lua_pushnumber(L, numval);
541 } else if (!strncmp(str+pos, "true", 4)) {
542 // consume 4 input characters for "true":
543 pos += 4;
544 // put Lua true value onto stack:
545 lua_pushboolean(L, 1);
546 } else if (!strncmp(str+pos, "false", 5)) {
547 // consume 5 input characters for "false":
548 pos += 5;
549 // put Lua false value onto stack:
550 lua_pushboolean(L, 0);
551 } else if (!strncmp(str+pos, "null", 4)) {
552 // consume 4 input characters for "null":
553 pos += 4;
554 // different behavor for top-level and sub-levels:
555 if (level) {
556 // if sub-level,
557 // push special null-marker onto stack:
558 json_pushnullmark(L);
559 } else {
560 // if top-level,
561 // push nil onto stack:
562 lua_pushnil(L);
563 }
564 } else {
565 // all other cases are a syntax error:
566 goto json_import_syntax_error;
567 }
568 // process a decoded value or key value pair (expected on top of Lua stack):
569 json_import_process_value:
570 switch (mode) {
571 // an object key has been read:
572 case JSON_STATE_OBJECT_KEY:
573 // if an object key is not a string, then this is a syntax error:
574 if (lua_type(L, -1) != LUA_TSTRING) goto json_import_syntax_error;
575 // expect key terminator to follow:
576 mode = JSON_STATE_OBJECT_KEY_TERMINATOR;
577 // continue with loop:
578 goto json_import_loop;
579 // a key value pair has been read:
580 case JSON_STATE_OBJECT_VALUE:
581 // store key value pair in outer shadow table:
582 lua_rawset(L, -3);
583 // expect value terminator (or end of object) to follow:
584 mode = JSON_STATE_OBJECT_SEPARATOR;
585 // continue with loop:
586 goto json_import_loop;
587 // an array value has been read:
588 case JSON_STATE_ARRAY_VALUE:
589 // get current array length:
590 arraylen = lua_rawlen(L, -3);
591 // throw error if array would exceed INT_MAX elements:
592 // TODO: Lua 5.3 may support more elements
593 if (arraylen >= INT_MAX) {
594 lua_pushnil(L);
595 lua_pushfstring(L, "Array exceeded length of %d elements", INT_MAX);
596 }
597 // store value in outer shadow table:
598 lua_rawseti(L, -3, arraylen + 1);
599 // expect value terminator (or end of object) to follow:
600 mode = JSON_STATE_ARRAY_SEPARATOR;
601 // continue with loop
602 goto json_import_loop;
603 // a single value has been read:
604 case JSON_STATE_VALUE:
605 // leave value on top of stack, expect end of JSON document, and continue with loop:
606 mode = JSON_STATE_END;
607 goto json_import_loop;
608 }
609 // syntax error handling (reachable by goto statement):
610 json_import_syntax_error:
611 lua_pushnil(L);
612 lua_pushliteral(L, "Syntax error in JSON document");
613 return 2;
614 }
616 // gets a value or its type from a JSON document (passed as first argument)
617 // using a path (passed as variable number of keys after the first argument):
618 static int json_path(lua_State *L, int type_mode) {
619 int stacktop; // number of arguments
620 int idx = 2; // stack index of current argument to process
621 // require at least one argument:
622 luaL_checkany(L, 1);
623 // store stack index of top of stack (number of arguments):
624 stacktop = lua_gettop(L);
625 // use first argument as "current value" (stored on top of stack):
626 lua_pushvalue(L, 1);
627 // process each "path key" (2nd argument and following arguments):
628 while (idx <= stacktop) {
629 // if "current value" (on top of stack) is nil, then the path cannot be walked and nil is returned:
630 if (lua_isnil(L, -1)) return 1;
631 // try to get shadow table of "current value":
632 json_getshadow(L, -1);
633 if (lua_isnil(L, -1)) {
634 // if no shadow table is found,
635 if (lua_type(L, -2) == LUA_TTABLE) {
636 // and if "current value" is a table,
637 // pop nil from stack:
638 lua_pop(L, 1);
639 // get "next value" using the "path key":
640 lua_pushvalue(L, idx++);
641 lua_gettable(L, -2);
642 } else {
643 // if "current value" is not a table,
644 // then the path cannot be walked and nil (already on top of stack) is returned:
645 return 1;
646 }
647 } else {
648 // if a shadow table is found,
649 // set "current value" to its shadow table:
650 lua_replace(L, -2);
651 // get "next value" using the "path key":
652 lua_pushvalue(L, idx++);
653 lua_rawget(L, -2);
654 }
655 // the "next value" replaces the "current value":
656 lua_replace(L, -2);
657 }
658 if (!type_mode) {
659 // if a value (and not its type) was requested,
660 // check if value is the null-marker, and store nil on top of Lua stack in that case:
661 if (json_isnullmark(L, -1)) lua_pushnil(L);
662 } else {
663 // if the type was requested,
664 // check if value is the null-marker:
665 if (json_isnullmark(L, -1)) {
666 // if yes, store string "null" on top of Lua stack:
667 lua_pushliteral(L, "null");
668 } else {
669 // otherwise,
670 // check if metatable indicates "object" or "array":
671 if (lua_getmetatable(L, -1)) {
672 json_regfetch(L, objectmt);
673 if (lua_rawequal(L, -2, -1)) {
674 // if value has metatable for JSON objects,
675 // return string "object":
676 lua_pushliteral(L, "object");
677 return 1;
678 }
679 json_regfetch(L, arraymt);
680 if (lua_rawequal(L, -3, -1)) {
681 // if value has metatable for JSON arrays,
682 // return string "object":
683 lua_pushliteral(L, "array");
684 return 1;
685 }
686 // remove 3 metatables (one of the value, two for comparison) from stack:
687 lua_pop(L, 3);
688 }
689 // otherwise, get the Lua type:
690 lua_pushstring(L, lua_typename(L, lua_type(L, -1)));
691 }
692 }
693 // return the top most value on the Lua stack:
694 return 1;
695 }
697 // gets a value from a JSON document (passed as first argument)
698 // using a path (passed as variable number of keys after the first argument):
699 static int json_get(lua_State *L) {
700 return json_path(L, 0);
701 }
703 // gets a value's type from a JSON document (passed as first argument)
704 // using a path (passed as variable number of keys after first the argument):
705 static int json_type(lua_State *L) {
706 return json_path(L, 1);
707 }
709 // special Lua stack indicies for json_set function:
710 #define json_set_objectmt_idx 1
711 #define json_set_arraymt_idx 2
713 // stack offset of arguments to json_set function:
714 #define json_set_idxshift 2
716 // sets a value (passed as second argument) in a JSON document (passed as first argument)
717 // using a path (passed as variable number of keys starting at third argument):
718 static int json_set(lua_State *L) {
719 int stacktop; // stack index of top of stack (after shifting)
720 int idx; // stack index of current argument to process
721 // require at least two arguments:
722 luaL_checkany(L, 1);
723 luaL_checkany(L, 2);
724 // insert objectmt into stack at position 1 (shifting the arguments):
725 json_regfetch(L, objectmt);
726 lua_insert(L, 1);
727 // insert arraymt into stack at position 2 (shifting the arguments):
728 json_regfetch(L, arraymt);
729 lua_insert(L, 2);
730 // store stack index of top of stack:
731 stacktop = lua_gettop(L);
732 // use nil as initial "parent value":
733 lua_pushnil(L);
734 // use first argument as "current value":
735 lua_pushvalue(L, 1 + json_set_idxshift);
736 // set all necessary values in path:
737 for (idx = 3 + json_set_idxshift; idx<=stacktop; idx++) {
738 // push metatable of "current value" onto stack:
739 if (!lua_getmetatable(L, -1)) lua_pushnil(L);
740 // distinguish according to type of path key:
741 switch (lua_type(L, idx)) {
742 case LUA_TSTRING:
743 // if path key is a string,
744 // check if "current value" is a JSON object (or table without metatable):
745 if (
746 lua_rawequal(L, -1, json_set_objectmt_idx) ||
747 (lua_isnil(L, -1) && lua_type(L, -2) == LUA_TTABLE)
748 ) {
749 // if "current value" is acceptable,
750 // pop metatable and leave "current value" on top of stack:
751 lua_pop(L, 1);
752 } else {
753 // if "current value" is not acceptable:
754 // pop metatable and "current value":
755 lua_pop(L, 2);
756 // throw error if parent element does not exist:
757 if (lua_isnil(L, -1)) return luaL_error(L, "Root element is not a JSON object");
758 // push new JSON object as "current value" onto stack:
759 json_createproxy(L);
760 // create and register shadow table:
761 lua_newtable(L);
762 json_setshadow(L, -2);
763 // set metatable of JSON object:
764 lua_pushvalue(L, json_set_objectmt_idx);
765 lua_setmetatable(L, -2);
766 // set entry in "parent value":
767 lua_pushvalue(L, idx-1);
768 lua_pushvalue(L, -2);
769 lua_settable(L, -4);
770 }
771 break;
772 case LUA_TNUMBER:
773 // if path key is a number,
774 // check if "current value" is a JSON array (or table without metatable):
775 if (
776 lua_rawequal(L, -1, json_set_arraymt_idx) ||
777 (lua_isnil(L, -1) && lua_type(L, -2) == LUA_TTABLE)
778 ) {
779 // if "current value" is acceptable,
780 // pop metatable and leave "current value" on top of stack:
781 lua_pop(L, 1);
782 } else {
783 // if "current value" is not acceptable:
784 // pop metatable and "current value":
785 lua_pop(L, 2);
786 // throw error if parent element does not exist:
787 if (lua_isnil(L, -1)) return luaL_error(L, "Root element is not a JSON array");
788 // push new JSON array as "current value" onto stack:
789 json_createproxy(L);
790 // create and register shadow table:
791 lua_newtable(L);
792 json_setshadow(L, -2);
793 // set metatable of JSON array:
794 lua_pushvalue(L, json_set_arraymt_idx);
795 lua_setmetatable(L, -2);
796 // set entry in "parent value":
797 lua_pushvalue(L, idx-1);
798 lua_pushvalue(L, -2);
799 lua_settable(L, -4);
800 }
801 break;
802 default:
803 return luaL_error(L, "Invalid path key of type %s", lua_typename(L, lua_type(L, idx)));
804 }
805 // check if last path element is being processed:
806 if (idx == stacktop) {
807 // if the last path element is being processed,
808 // set last path value in "current value" container:
809 lua_pushvalue(L, idx);
810 lua_pushvalue(L, 2 + json_set_idxshift);
811 lua_settable(L, -3);
812 } else {
813 // if the processed path element is not the last,
814 // use old "current value" as new "parent value"
815 lua_remove(L, -2);
816 // push new "current value" onto stack by performing a lookup:
817 lua_pushvalue(L, idx);
818 lua_gettable(L, -2);
819 }
820 }
821 // return first argument for convenience:
822 lua_settop(L, 1 + json_set_idxshift);
823 return 1;
824 }
826 // returns the length of a JSON array (or zero for a table without numeric keys):
827 static int json_len(lua_State *L) {
828 // stack shall contain one function argument:
829 lua_settop(L, 1);
830 // push shadow table or nil onto stack:
831 json_getshadow(L, 1);
832 // pop nil from stack if no shadow table has been found:
833 if (lua_isnil(L, -1)) lua_pop(L, 1);
834 // return length of argument or shadow table:
835 lua_pushnumber(L, lua_rawlen(L, -1));
836 return 1;
837 }
839 // __index metamethod for JSON objects and JSON arrays:
840 static int json_index(lua_State *L) {
841 // stack shall contain two function arguments:
842 lua_settop(L, 2);
843 // replace first argument with its shadow table
844 // or throw error if no shadow table is found:
845 json_getshadow(L, 1);
846 if (lua_isnil(L, -1)) return luaL_error(L, "Shadow table not found");
847 lua_replace(L, 1);
848 // use key passed as second argument to lookup value in shadow table:
849 lua_rawget(L, 1);
850 // if value is null-marker, then push nil onto stack:
851 if (json_isnullmark(L, 2)) lua_pushnil(L);
852 // return either looked up value, or nil
853 return 1;
854 }
856 // __newindex metamethod for JSON objects and JSON arrays:
857 static int json_newindex(lua_State *L) {
858 // stack shall contain three function arguments:
859 lua_settop(L, 3);
860 // replace first argument with its shadow table
861 // or throw error if no shadow table is found:
862 json_getshadow(L, 1);
863 if (lua_isnil(L, -1)) return luaL_error(L, "Shadow table not found");
864 lua_replace(L, 1);
865 // second and third argument to write to shadow table:
866 lua_rawset(L, 1);
867 // return nothing:
868 return 0;
869 }
871 // function returned as first value by json_pairs function:
872 static int json_pairs_iterfunc(lua_State *L) {
873 // stack shall contain two function arguments:
874 lua_settop(L, 2);
875 // replace first argument with its shadow table
876 // or throw error if no shadow table is found:
877 json_getshadow(L, 1);
878 if (lua_isnil(L, -1)) return luaL_error(L, "Shadow table not found");
879 lua_replace(L, 1);
880 // get next key value pair from shadow table (using previous key from argument 2)
881 // and return nothing if there is no next pair:
882 if (!lua_next(L, 1)) return 0;
883 // replace null-marker with nil:
884 if (json_isnullmark(L, -1)) {
885 lua_pop(L, 1);
886 lua_pushnil(L);
887 }
888 // return key and value (or key and nil, if null-marker was found):
889 return 2;
890 }
892 // returns a triple such that 'for key, value in pairs(obj) do ... end'
893 // iterates through all key value pairs (including JSON null values represented as Lua nil):
894 static int json_pairs(lua_State *L) {
895 // require one argument to function
896 luaL_checkany(L, 1);
897 // return triple of function json_pairs_iterfunc, first argument, and nil:
898 lua_pushcfunction(L, json_pairs_iterfunc);
899 lua_pushvalue(L, 1);
900 lua_pushnil(L);
901 return 3;
902 }
904 // function returned as first value by json_ipairs function:
905 static int json_ipairs_iterfunc(lua_State *L) {
906 lua_Integer idx;
907 // stack shall contain two function arguments:
908 lua_settop(L, 2);
909 // calculate new index by incrementing second argument:
910 idx = lua_tointeger(L, 2) + 1;
911 // push shadow table onto stack position 3
912 // or throw error if no shadow table is found:
913 json_getshadow(L, 1);
914 if (lua_isnil(L, -1)) return luaL_error(L, "Shadow table not found");
915 // do integer lookup in shadow table and store result on stack position 4:
916 lua_rawgeti(L, 3, idx);
917 // return nothing if there was no value:
918 if (lua_isnil(L, 4)) return 0;
919 // return new index and
920 // either the looked up value if it is not equal to the null-marker
921 // or nil instead of null-marker:
922 lua_pushinteger(L, idx);
923 if (json_isnullmark(L, 4)) lua_pushnil(L);
924 else lua_pushvalue(L, 4);
925 return 2;
926 }
928 // returns a triple such that 'for idx, value in ipairs(ary) do ... end'
929 // iterates through all values (including JSON null values represented as Lua nil):
930 static int json_ipairs(lua_State *L) {
931 // require one argument to function
932 luaL_checkany(L, 1);
933 // return triple of function json_ipairs_iterfunc, first argument, and zero:
934 lua_pushcfunction(L, json_ipairs_iterfunc);
935 lua_pushvalue(L, 1);
936 lua_pushinteger(L, 0);
937 return 3;
938 }
940 // datatype representing a table key:
941 // (used for sorting)
942 typedef struct {
943 size_t length;
944 const char *data;
945 } json_key_t;
947 // comparation function for table keys to be passed to qsort function:
948 static int json_key_cmp(json_key_t *key1, json_key_t *key2) {
949 size_t pos = 0;
950 unsigned char c1, c2;
951 while (1) {
952 if (key1->length > pos) {
953 if (key2->length > pos) {
954 c1 = key1->data[pos];
955 c2 = key2->data[pos];
956 if (c1 < c2) return -1;
957 else if (c1 > c2) return 1;
958 } else {
959 return 1;
960 }
961 } else {
962 if (key2->length > pos) {
963 return -1;
964 } else {
965 return 0;
966 }
967 }
968 pos++;
969 }
970 }
972 // constants for type detection of ambiguous tables:
973 #define JSON_TABLETYPE_UNKNOWN 0
974 #define JSON_TABLETYPE_OBJECT 1
975 #define JSON_TABLETYPE_ARRAY 2
977 typedef struct {
978 int type;
979 int pos;
980 int count;
981 json_key_t keys[1]; // or more
982 } json_container_t;
984 // special Lua stack indicies for json_export function:
985 #define json_export_value_idx 1
986 #define json_export_indentstring_idx 2
987 #define json_export_objectmt_idx 3
988 #define json_export_arraymt_idx 4
989 #define json_export_stackswap_idx 5
990 #define json_export_luacontainer_idx 6
991 #define json_export_ccontainer_idx 7
992 #define json_export_buffer_idx 8
994 // encodes a JSON document (passed as first argument)
995 // optionally using indentation (indentation string or true passed as second argument)
996 static int json_export(lua_State *L) {
997 int pretty; // pretty printing on? (i.e. printing with indentation)
998 luaL_Buffer buf; // Lua buffer containing result string
999 lua_Number num; // number to encode
1000 char numstr[21]; // encoded number (sign, zero, point, 17 significant digits, and terminating NULL byte)
1001 const char *str; // string to encode
1002 size_t strlen; // length of string to encode
1003 size_t strpos ; // position in string or position of current key
1004 unsigned char c; // character to encode (unsigned!)
1005 char hexcode[7]; // store for unicode hex escape sequence
1006 // NOTE: 7 bytes due to backslash, character 'u', 4 hex digits, and terminating NULL byte
1007 int tabletype; // table type: unknown, JSON object, or JSON array
1008 size_t keycount = 0; // number of string keys in object
1009 json_key_t *key; // pointer to C structure containing a string key
1010 int level = 0; // current depth level
1011 int i; // iteration variable for level dependent repetitions
1012 int stackswapidx = 0; // elements in stack swap table
1013 int containerkey = 0; // temporarily set to 1, if a container key is being encoded
1014 json_container_t *container = NULL; // pointer to current C struct for container information
1015 // stack shall contain two function arguments:
1016 lua_settop(L, 2);
1017 // check if pretty printing (with indentation) is desired:
1018 if (lua_toboolean(L, json_export_indentstring_idx)) {
1019 // if yes,
1020 // set pretty variable to 1:
1021 pretty = 1;
1022 // check if second argument is a boolean (true):
1023 if (lua_isboolean(L, json_export_indentstring_idx)) {
1024 // if yes,
1025 // use default indentation if indentation argument is boolean true:
1026 lua_pushliteral(L, " ");
1027 lua_replace(L, json_export_indentstring_idx);
1028 } else {
1029 // if no,
1030 // require second argument to be a string:
1031 luaL_checktype(L, json_export_indentstring_idx, LUA_TSTRING);
1033 } else {
1034 // if no,
1035 // set pretty variable to 0:
1036 pretty = 0;
1038 // push objectmt onto stack position 3:
1039 json_regfetch(L, objectmt);
1040 // push arraymt onto stack position 4:
1041 json_regfetch(L, arraymt);
1042 // push table for stack swapping onto stack position 5:
1043 lua_newtable(L);
1044 // create placeholders on stack positions 6 through 7:
1045 lua_settop(L, json_export_buffer_idx);
1046 // create Lua string buffer:
1047 luaL_buffinit(L, &buf);
1048 // loop:
1049 while (1) {
1050 // if value to encode is the null-marker, then treat it the same as nil:
1051 if (json_isnullmark(L, json_export_value_idx)) {
1052 lua_pushnil(L);
1053 lua_replace(L, json_export_value_idx);
1055 // distinguish between different Lua types:
1056 switch (lua_type(L, json_export_value_idx)) {
1057 // value to encode is nil:
1058 case LUA_TNIL:
1059 // add string "null" to output buffer:
1060 luaL_addstring(&buf, "null");
1061 break;
1062 // value to encode is of type number:
1063 case LUA_TNUMBER:
1064 // convert value to double precision number:
1065 num = lua_tonumber(L, json_export_value_idx);
1066 // throw error if number is not-a-number:
1067 if (isnan(num)) return luaL_error(L, "JSON export not possible for NaN value");
1068 // throw error if number is positive or negative infinity:
1069 if (isinf(num)) return luaL_error(L, "JSON export not possible for infinite numbers");
1070 // determine necessary precision to represent double precision floating point number:
1071 sprintf(numstr, "%.16g", num);
1072 if (strtod(numstr, NULL) != num) sprintf(numstr, "%.17g", num);
1073 // add string encoding of the number to the output buffer:
1074 luaL_addstring(&buf, numstr);
1075 break;
1076 // value to encode is of type boolean:
1077 case LUA_TBOOLEAN:
1078 // add string "true" or "false" according to boolean value:
1079 luaL_addstring(&buf, lua_toboolean(L, json_export_value_idx) ? "true" : "false");
1080 break;
1081 // value to encode is of type string:
1082 case LUA_TSTRING:
1083 // add quoted and escaped string to output buffer:
1084 str = lua_tolstring(L, json_export_value_idx, &strlen);
1085 luaL_addchar(&buf, '"');
1086 strpos = 0;
1087 while (strpos < strlen) {
1088 c = str[strpos++];
1089 if (c == '"') luaL_addstring(&buf, "\\\"");
1090 else if (c == '\\') luaL_addstring(&buf, "\\\\");
1091 else if (c == 127) luaL_addstring(&buf, "\\u007F");
1092 else if (c >= 32) luaL_addchar(&buf, c);
1093 else if (c == '\b') luaL_addstring(&buf, "\\b");
1094 else if (c == '\f') luaL_addstring(&buf, "\\f");
1095 else if (c == '\n') luaL_addstring(&buf, "\\n");
1096 else if (c == '\r') luaL_addstring(&buf, "\\r");
1097 else if (c == '\t') luaL_addstring(&buf, "\\t");
1098 else if (c == '\v') luaL_addstring(&buf, "\\v");
1099 else {
1100 sprintf(hexcode, "\\u%04X", c);
1101 luaL_addstring(&buf, hexcode);
1104 luaL_addchar(&buf, '"');
1105 break;
1106 // value to encode is of type table (this includes JSON objects and JSON arrays):
1107 case LUA_TTABLE:
1108 // use table's metatable to try to determine type of table:
1109 tabletype = JSON_TABLETYPE_UNKNOWN;
1110 if (lua_getmetatable(L, json_export_value_idx)) {
1111 if (lua_rawequal(L, -1, json_export_objectmt_idx)) {
1112 tabletype = JSON_TABLETYPE_OBJECT;
1113 } else {
1114 if (lua_rawequal(L, -1, json_export_arraymt_idx)) {
1115 tabletype = JSON_TABLETYPE_ARRAY;
1116 } else {
1117 return luaL_error(L, "JSON export not possible for tables with nonsupported metatable");
1120 // reset stack (pop metatable from stack):
1121 lua_pop(L, 1);
1123 // replace table with its shadow table if existent:
1124 json_getshadow(L, json_export_value_idx);
1125 if (lua_isnil(L, -1)) lua_pop(L, 1);
1126 else lua_replace(L, json_export_value_idx);
1127 // check if type of table is still undetermined
1128 // and optionally calculate number of string keys (keycount)
1129 // or set keycount to zero:
1130 keycount = 0;
1131 if (tabletype == JSON_TABLETYPE_UNKNOWN) {
1132 // if type of table is undetermined,
1133 // iterate over all keys:
1134 for (lua_pushnil(L); lua_next(L, json_export_value_idx); lua_pop(L, 1)) {
1135 switch (lua_type(L, -2)) {
1136 case LUA_TSTRING:
1137 // for string keys,
1138 // increase keycount (may avoid another iteration):
1139 keycount++;
1140 // if type of table was unknown, then type of table is a JSON object now:
1141 if (tabletype == JSON_TABLETYPE_UNKNOWN) tabletype = JSON_TABLETYPE_OBJECT;
1142 // if type of table was a JSON array, then the type of table is ambiguous now
1143 // and an error is thrown:
1144 else if (tabletype == JSON_TABLETYPE_ARRAY) goto json_export_tabletype_error;
1145 break;
1146 case LUA_TNUMBER:
1147 // for numeric keys,
1148 // if type of table was unknown, then type of table is a JSON array now:
1149 if (tabletype == JSON_TABLETYPE_UNKNOWN) tabletype = JSON_TABLETYPE_ARRAY;
1150 // if type of table was a JSON object, then the type of table is ambiguous now
1151 // and an error is thrown:
1152 else if (tabletype == JSON_TABLETYPE_OBJECT) goto json_export_tabletype_error;
1153 break;
1157 // raise error if too many nested levels:
1158 if (level >= JSON_MAXDEPTH) {
1159 return luaL_error(L, "More than %d nested JSON levels", JSON_MAXDEPTH);
1161 // store previous container information (if existent) on stack swap
1162 // and increase level variable:
1163 if (level++) {
1164 lua_pushvalue(L, json_export_luacontainer_idx);
1165 lua_rawseti(L, json_export_stackswap_idx, ++stackswapidx);
1166 lua_pushvalue(L, json_export_ccontainer_idx);
1167 lua_rawseti(L, json_export_stackswap_idx, ++stackswapidx);
1169 // use value as current container:
1170 lua_pushvalue(L, json_export_value_idx);
1171 lua_replace(L, json_export_luacontainer_idx);
1172 // distinguish between JSON objects and JSON arrays:
1173 switch (tabletype) {
1174 // JSON object:
1175 case JSON_TABLETYPE_OBJECT:
1176 // calculate count of string keys unless it has been calculated before:
1177 if (!keycount) {
1178 for (lua_pushnil(L); lua_next(L, json_export_luacontainer_idx); lua_pop(L, 1)) {
1179 if (lua_type(L, -2) == LUA_TSTRING) keycount++;
1182 // allocate memory for C structure containing string keys and container iteration state:
1183 container = lua_newuserdata(L, sizeof(json_container_t) + (keycount-1) * sizeof(json_key_t));
1184 // store reference to C structure on designated stack position:
1185 lua_replace(L, json_export_ccontainer_idx);
1186 // initialize C structure for container state:
1187 container->type = JSON_TABLETYPE_OBJECT;
1188 container->count = keycount;
1189 container->pos = 0;
1190 // check if object contains any keys:
1191 if (keycount) {
1192 // if yes,
1193 // copy all string keys to the C structure (and reset container->pos again):
1194 for (lua_pushnil(L); lua_next(L, json_export_luacontainer_idx); lua_pop(L, 1)) {
1195 if (lua_type(L, -2) == LUA_TSTRING) {
1196 json_key_t *key = &container->keys[container->pos++];
1197 key->data = lua_tolstring(L, -2, &key->length);
1200 container->pos = 0;
1201 // sort C array using quicksort:
1202 qsort(container->keys, keycount, sizeof(json_key_t), (void *)json_key_cmp);
1204 // add opening bracket to output buffer:
1205 luaL_addchar(&buf, '{');
1206 break;
1207 // JSON array:
1208 case JSON_TABLETYPE_ARRAY:
1209 // allocate memory for C structure for container iteration state:
1210 container = lua_newuserdata(L, sizeof(json_container_t) - sizeof(json_key_t));
1211 // store reference to C structure on designated stack position:
1212 lua_replace(L, json_export_ccontainer_idx);
1213 // initialize C structure for container state:
1214 container->type = JSON_TABLETYPE_ARRAY;
1215 container->pos = 0;
1216 // add opening bracket to output buffer:
1217 luaL_addchar(&buf, '[');
1218 break;
1219 default:
1220 // throw error if table type is unknown:
1221 json_export_tabletype_error:
1222 return luaL_error(L, "JSON export not possible for ambiguous table (cannot decide whether it is an object or array)");
1224 break;
1225 default:
1226 // all other datatypes are considered an error:
1227 return luaL_error(L, "JSON export not possible for values of type \"%s\"", lua_typename(L, lua_type(L, json_export_value_idx)));
1229 // check if a container is being processed:
1230 if (container) {
1231 // if yes,
1232 // execute code for container iteration:
1233 json_export_container:
1234 // distinguish between JSON objects and JSON arrays:
1235 switch (container->type) {
1236 // JSON object:
1237 case JSON_TABLETYPE_OBJECT:
1238 // finish iteration if all string keys have been processed:
1239 if (container->pos == container->count) goto json_export_close;
1240 // push current string key on top of stack:
1241 key = &container->keys[container->pos];
1242 lua_pushlstring(L, key->data, key->length);
1243 // check if the key has already been exported:
1244 if (!containerkey) {
1245 // if no,
1246 // add a comma to the output buffer if necessary:
1247 if (container->pos) luaL_addchar(&buf, ',');
1248 // set containerkey variable to true:
1249 containerkey = 1;
1250 } else {
1251 // if a key has already been exported,
1252 // add a colon to the output buffer:
1253 luaL_addchar(&buf, ':');
1254 // add a space to the output buffer for pretty results:
1255 if (pretty) luaL_addchar(&buf, ' ');
1256 // replace string key on top of stack with corresponding value:
1257 lua_rawget(L, json_export_luacontainer_idx);
1258 // reset containerkey variable
1259 containerkey = 0;
1260 // increase number of processed key value pairs:
1261 container->pos++;
1263 // store key or value on top of stack in designated stack position:
1264 lua_replace(L, json_export_value_idx);
1265 break;
1266 // JSON array:
1267 case JSON_TABLETYPE_ARRAY:
1268 // store next value in designated stack position:
1269 lua_rawgeti(L, json_export_luacontainer_idx, container->pos+1);
1270 lua_replace(L, json_export_value_idx);
1271 // finish iteration if value is nil:
1272 if (lua_isnil(L, json_export_value_idx)) goto json_export_close;
1273 // add a comma to the output buffer if necessary:
1274 if (container->pos) luaL_addchar(&buf, ',');
1275 // increase number of processed values:
1276 container->pos++;
1277 break;
1278 // common code for closing JSON objects or JSON arrays:
1279 json_export_close:
1280 // decrement level variable:
1281 level--;
1282 // handle indentation for pretty results:
1283 if (pretty && container->pos) {
1284 luaL_addchar(&buf, '\n');
1285 for (i=0; i<level; i++) {
1286 lua_pushvalue(L, json_export_indentstring_idx);
1287 luaL_addvalue(&buf);
1290 // add closing bracket to output buffer:
1291 luaL_addchar(&buf, container->type == JSON_TABLETYPE_OBJECT ? '}' : ']');
1292 // finish export if last level has been closed:
1293 if (!level) goto json_export_finish;
1294 // otherwise,
1295 // recall previous container information from stack swap
1296 // and set C pointer to corresponding C struct:
1297 lua_rawgeti(L, json_export_stackswap_idx, stackswapidx--);
1298 lua_replace(L, json_export_ccontainer_idx);
1299 container = lua_touserdata(L, json_export_ccontainer_idx);
1300 lua_rawgeti(L, json_export_stackswap_idx, stackswapidx--);
1301 lua_replace(L, json_export_luacontainer_idx);
1302 // repeat code for container iteration:
1303 goto json_export_container;
1305 // handle indentation for pretty results:
1306 if (pretty && (containerkey || container->type == JSON_TABLETYPE_ARRAY)) {
1307 luaL_addchar(&buf, '\n');
1308 for (i=0; i<level; i++) {
1309 lua_pushvalue(L, json_export_indentstring_idx);
1310 luaL_addvalue(&buf);
1313 } else {
1314 // if no container is being processed,
1315 // finish export:
1316 json_export_finish:
1317 // for pretty results, add final newline character if outermost container is processed:
1318 if (pretty) luaL_addchar(&buf, '\n');
1319 // create and return Lua string from buffer contents
1320 luaL_pushresult(&buf);
1321 return 1;
1326 // functions in library module:
1327 static const struct luaL_Reg json_module_functions[] = {
1328 {"object", json_object},
1329 {"array", json_array},
1330 {"import", json_import},
1331 {"export", json_export},
1332 {"get", json_get},
1333 {"type", json_type},
1334 {"set", json_set},
1335 {NULL, NULL}
1336 };
1338 // metamethods for JSON objects, JSON arrays, and unknown JSON collections (object or array):
1339 static const struct luaL_Reg json_metatable_functions[] = {
1340 {"__len", json_len},
1341 {"__index", json_index},
1342 {"__newindex", json_newindex},
1343 {"__pairs", json_pairs},
1344 {"__ipairs", json_ipairs},
1345 {"__tostring", json_export},
1346 {NULL, NULL}
1347 };
1349 // metamethods for JSON null marker:
1350 static const struct luaL_Reg json_nullmark_metamethods[] = {
1351 {"__tostring", json_nullmark_tostring},
1352 {NULL, NULL}
1353 };
1355 // initializes json library:
1356 int luaopen_json(lua_State *L) {
1357 // empty stack:
1358 lua_settop(L, 0);
1359 // push library module onto stack position 1:
1360 lua_newtable(L);
1361 // register library functions:
1362 luaL_setfuncs(L, json_module_functions, 0);
1363 // create and store objectmt:
1364 lua_newtable(L);
1365 luaL_setfuncs(L, json_metatable_functions, 0);
1366 json_regstore(L, objectmt);
1367 // create and store arraymt:
1368 lua_newtable(L);
1369 luaL_setfuncs(L, json_metatable_functions, 0);
1370 json_regstore(L, arraymt);
1371 // set metatable of null marker and make it available through library module:
1372 json_pushnullmark(L);
1373 lua_newtable(L);
1374 luaL_setfuncs(L, json_nullmark_metamethods, 0);
1375 lua_setmetatable(L, -2);
1376 lua_setfield(L, 1, "null");
1377 // return library module (that's expected on top of stack):
1378 return 1;

Impressum / About Us