webmcp

view libraries/json/json.c @ 199:9fd7e1bf9fe3

Require 3 arguments for json.set(...); Completed autodoc documentation for JSON library
author jbe
date Tue Aug 12 01:01:54 2014 +0200 (2014-08-12)
parents 654ddbcc49d0
children 035b58aa430a
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 // push special null-marker onto stack:
555 json_pushnullmark(L);
556 } else {
557 // all other cases are a syntax error:
558 goto json_import_syntax_error;
559 }
560 // process a decoded value or key value pair (expected on top of Lua stack):
561 json_import_process_value:
562 switch (mode) {
563 // an object key has been read:
564 case JSON_STATE_OBJECT_KEY:
565 // if an object key is not a string, then this is a syntax error:
566 if (lua_type(L, -1) != LUA_TSTRING) goto json_import_syntax_error;
567 // expect key terminator to follow:
568 mode = JSON_STATE_OBJECT_KEY_TERMINATOR;
569 // continue with loop:
570 goto json_import_loop;
571 // a key value pair has been read:
572 case JSON_STATE_OBJECT_VALUE:
573 // store key value pair in outer shadow table:
574 lua_rawset(L, -3);
575 // expect value terminator (or end of object) to follow:
576 mode = JSON_STATE_OBJECT_SEPARATOR;
577 // continue with loop:
578 goto json_import_loop;
579 // an array value has been read:
580 case JSON_STATE_ARRAY_VALUE:
581 // get current array length:
582 arraylen = lua_rawlen(L, -3);
583 // throw error if array would exceed INT_MAX elements:
584 // TODO: Lua 5.3 may support more elements
585 if (arraylen >= INT_MAX) {
586 lua_pushnil(L);
587 lua_pushfstring(L, "Array exceeded length of %d elements", INT_MAX);
588 }
589 // store value in outer shadow table:
590 lua_rawseti(L, -3, arraylen + 1);
591 // expect value terminator (or end of object) to follow:
592 mode = JSON_STATE_ARRAY_SEPARATOR;
593 // continue with loop
594 goto json_import_loop;
595 // a single value has been read:
596 case JSON_STATE_VALUE:
597 // leave value on top of stack, expect end of JSON document, and continue with loop:
598 mode = JSON_STATE_END;
599 goto json_import_loop;
600 }
601 // syntax error handling (reachable by goto statement):
602 json_import_syntax_error:
603 lua_pushnil(L);
604 lua_pushliteral(L, "Syntax error in JSON document");
605 return 2;
606 }
608 // gets a value or its type from a JSON document (passed as first argument)
609 // using a path (passed as variable number of keys after the first argument):
610 static int json_path(lua_State *L, int type_mode) {
611 int stacktop; // number of arguments
612 int idx = 2; // stack index of current argument to process
613 // require at least one argument:
614 luaL_checkany(L, 1);
615 // store stack index of top of stack (number of arguments):
616 stacktop = lua_gettop(L);
617 // use first argument as "current value" (stored on top of stack):
618 lua_pushvalue(L, 1);
619 // process each "path key" (2nd argument and following arguments):
620 while (idx <= stacktop) {
621 // if "current value" (on top of stack) is nil, then the path cannot be walked and nil is returned:
622 if (lua_isnil(L, -1)) return 1;
623 // try to get shadow table of "current value":
624 json_getshadow(L, -1);
625 if (lua_isnil(L, -1)) {
626 // if no shadow table is found,
627 if (lua_type(L, -2) == LUA_TTABLE) {
628 // and if "current value" is a table,
629 // pop nil from stack:
630 lua_pop(L, 1);
631 // get "next value" using the "path key":
632 lua_pushvalue(L, idx++);
633 lua_gettable(L, -2);
634 } else {
635 // if "current value" is not a table,
636 // then the path cannot be walked and nil (already on top of stack) is returned:
637 return 1;
638 }
639 } else {
640 // if a shadow table is found,
641 // set "current value" to its shadow table:
642 lua_replace(L, -2);
643 // get "next value" using the "path key":
644 lua_pushvalue(L, idx++);
645 lua_rawget(L, -2);
646 }
647 // the "next value" replaces the "current value":
648 lua_replace(L, -2);
649 }
650 if (!type_mode) {
651 // if a value (and not its type) was requested,
652 // check if value is the null-marker, and store nil on top of Lua stack in that case:
653 if (json_isnullmark(L, -1)) lua_pushnil(L);
654 } else {
655 // if the type was requested,
656 // check if value is the null-marker:
657 if (json_isnullmark(L, -1)) {
658 // if yes, store string "null" on top of Lua stack:
659 lua_pushliteral(L, "null");
660 } else {
661 // otherwise,
662 // check if metatable indicates "object" or "array":
663 if (lua_getmetatable(L, -1)) {
664 json_regfetch(L, objectmt);
665 if (lua_rawequal(L, -2, -1)) {
666 // if value has metatable for JSON objects,
667 // return string "object":
668 lua_pushliteral(L, "object");
669 return 1;
670 }
671 json_regfetch(L, arraymt);
672 if (lua_rawequal(L, -3, -1)) {
673 // if value has metatable for JSON arrays,
674 // return string "object":
675 lua_pushliteral(L, "array");
676 return 1;
677 }
678 // remove 3 metatables (one of the value, two for comparison) from stack:
679 lua_pop(L, 3);
680 }
681 // otherwise, get the Lua type:
682 lua_pushstring(L, lua_typename(L, lua_type(L, -1)));
683 }
684 }
685 // return the top most value on the Lua stack:
686 return 1;
687 }
689 // gets a value from a JSON document (passed as first argument)
690 // using a path (passed as variable number of keys after the first argument):
691 static int json_get(lua_State *L) {
692 return json_path(L, 0);
693 }
695 // gets a value's type from a JSON document (passed as first argument)
696 // using a path (passed as variable number of keys after first the argument):
697 static int json_type(lua_State *L) {
698 return json_path(L, 1);
699 }
701 // special Lua stack indicies for json_set function:
702 #define json_set_objectmt_idx 1
703 #define json_set_arraymt_idx 2
705 // stack offset of arguments to json_set function:
706 #define json_set_idxshift 2
708 // sets a value (passed as second argument) in a JSON document (passed as first argument)
709 // using a path (passed as variable number of keys starting at third argument):
710 static int json_set(lua_State *L) {
711 int stacktop; // stack index of top of stack (after shifting)
712 int idx; // stack index of current argument to process
713 // require at least three arguments:
714 luaL_checkany(L, 1);
715 luaL_checkany(L, 2);
716 luaL_checkany(L, 3);
717 // insert objectmt into stack at position 1 (shifting the arguments):
718 json_regfetch(L, objectmt);
719 lua_insert(L, 1);
720 // insert arraymt into stack at position 2 (shifting the arguments):
721 json_regfetch(L, arraymt);
722 lua_insert(L, 2);
723 // store stack index of top of stack:
724 stacktop = lua_gettop(L);
725 // use nil as initial "parent value":
726 lua_pushnil(L);
727 // use first argument as "current value":
728 lua_pushvalue(L, 1 + json_set_idxshift);
729 // set all necessary values in path:
730 for (idx = 3 + json_set_idxshift; idx<=stacktop; idx++) {
731 // push metatable of "current value" onto stack:
732 if (!lua_getmetatable(L, -1)) lua_pushnil(L);
733 // distinguish according to type of path key:
734 switch (lua_type(L, idx)) {
735 case LUA_TSTRING:
736 // if path key is a string,
737 // check if "current value" is a JSON object (or table without metatable):
738 if (
739 lua_rawequal(L, -1, json_set_objectmt_idx) ||
740 (lua_isnil(L, -1) && lua_type(L, -2) == LUA_TTABLE)
741 ) {
742 // if "current value" is acceptable,
743 // pop metatable and leave "current value" on top of stack:
744 lua_pop(L, 1);
745 } else {
746 // if "current value" is not acceptable:
747 // pop metatable and "current value":
748 lua_pop(L, 2);
749 // throw error if parent element does not exist:
750 if (lua_isnil(L, -1)) return luaL_error(L, "Root element is not a JSON object");
751 // push new JSON object as "current value" onto stack:
752 json_createproxy(L);
753 // create and register shadow table:
754 lua_newtable(L);
755 json_setshadow(L, -2);
756 // set metatable of JSON object:
757 lua_pushvalue(L, json_set_objectmt_idx);
758 lua_setmetatable(L, -2);
759 // set entry in "parent value":
760 lua_pushvalue(L, idx-1);
761 lua_pushvalue(L, -2);
762 lua_settable(L, -4);
763 }
764 break;
765 case LUA_TNUMBER:
766 // if path key is a number,
767 // check if "current value" is a JSON array (or table without metatable):
768 if (
769 lua_rawequal(L, -1, json_set_arraymt_idx) ||
770 (lua_isnil(L, -1) && lua_type(L, -2) == LUA_TTABLE)
771 ) {
772 // if "current value" is acceptable,
773 // pop metatable and leave "current value" on top of stack:
774 lua_pop(L, 1);
775 } else {
776 // if "current value" is not acceptable:
777 // pop metatable and "current value":
778 lua_pop(L, 2);
779 // throw error if parent element does not exist:
780 if (lua_isnil(L, -1)) return luaL_error(L, "Root element is not a JSON array");
781 // push new JSON array as "current value" onto stack:
782 json_createproxy(L);
783 // create and register shadow table:
784 lua_newtable(L);
785 json_setshadow(L, -2);
786 // set metatable of JSON array:
787 lua_pushvalue(L, json_set_arraymt_idx);
788 lua_setmetatable(L, -2);
789 // set entry in "parent value":
790 lua_pushvalue(L, idx-1);
791 lua_pushvalue(L, -2);
792 lua_settable(L, -4);
793 }
794 break;
795 default:
796 return luaL_error(L, "Invalid path key of type %s", lua_typename(L, lua_type(L, idx)));
797 }
798 // check if last path element is being processed:
799 if (idx == stacktop) {
800 // if the last path element is being processed,
801 // set last path value in "current value" container:
802 lua_pushvalue(L, idx);
803 lua_pushvalue(L, 2 + json_set_idxshift);
804 lua_settable(L, -3);
805 } else {
806 // if the processed path element is not the last,
807 // use old "current value" as new "parent value"
808 lua_remove(L, -2);
809 // push new "current value" onto stack by performing a lookup:
810 lua_pushvalue(L, idx);
811 lua_gettable(L, -2);
812 }
813 }
814 // return first argument for convenience:
815 lua_settop(L, 1 + json_set_idxshift);
816 return 1;
817 }
819 // returns the length of a JSON array (or zero for a table without numeric keys):
820 static int json_len(lua_State *L) {
821 // stack shall contain one function argument:
822 lua_settop(L, 1);
823 // push shadow table or nil onto stack:
824 json_getshadow(L, 1);
825 // pop nil from stack if no shadow table has been found:
826 if (lua_isnil(L, -1)) lua_pop(L, 1);
827 // return length of argument or shadow table:
828 lua_pushnumber(L, lua_rawlen(L, -1));
829 return 1;
830 }
832 // __index metamethod for JSON objects and JSON arrays:
833 static int json_index(lua_State *L) {
834 // stack shall contain two function arguments:
835 lua_settop(L, 2);
836 // replace first argument with its shadow table
837 // or throw error if no shadow table is found:
838 json_getshadow(L, 1);
839 if (lua_isnil(L, -1)) return luaL_error(L, "Shadow table not found");
840 lua_replace(L, 1);
841 // use key passed as second argument to lookup value in shadow table:
842 lua_rawget(L, 1);
843 // if value is null-marker, then push nil onto stack:
844 if (json_isnullmark(L, 2)) lua_pushnil(L);
845 // return either looked up value, or nil
846 return 1;
847 }
849 // __newindex metamethod for JSON objects and JSON arrays:
850 static int json_newindex(lua_State *L) {
851 // stack shall contain three function arguments:
852 lua_settop(L, 3);
853 // replace first argument with its shadow table
854 // or throw error if no shadow table is found:
855 json_getshadow(L, 1);
856 if (lua_isnil(L, -1)) return luaL_error(L, "Shadow table not found");
857 lua_replace(L, 1);
858 // second and third argument to write to shadow table:
859 lua_rawset(L, 1);
860 // return nothing:
861 return 0;
862 }
864 // function returned as first value by json_pairs function:
865 static int json_pairs_iterfunc(lua_State *L) {
866 // stack shall contain two function arguments:
867 lua_settop(L, 2);
868 // replace first argument with its shadow table
869 // or throw error if no shadow table is found:
870 json_getshadow(L, 1);
871 if (lua_isnil(L, -1)) return luaL_error(L, "Shadow table not found");
872 lua_replace(L, 1);
873 // get next key value pair from shadow table (using previous key from argument 2)
874 // and return nothing if there is no next pair:
875 if (!lua_next(L, 1)) return 0;
876 // replace null-marker with nil:
877 if (json_isnullmark(L, -1)) {
878 lua_pop(L, 1);
879 lua_pushnil(L);
880 }
881 // return key and value (or key and nil, if null-marker was found):
882 return 2;
883 }
885 // returns a triple such that 'for key, value in pairs(obj) do ... end'
886 // iterates through all key value pairs (including JSON null values represented as Lua nil):
887 static int json_pairs(lua_State *L) {
888 // require one argument to function
889 luaL_checkany(L, 1);
890 // return triple of function json_pairs_iterfunc, first argument, and nil:
891 lua_pushcfunction(L, json_pairs_iterfunc);
892 lua_pushvalue(L, 1);
893 lua_pushnil(L);
894 return 3;
895 }
897 // function returned as first value by json_ipairs function:
898 static int json_ipairs_iterfunc(lua_State *L) {
899 lua_Integer idx;
900 // stack shall contain two function arguments:
901 lua_settop(L, 2);
902 // calculate new index by incrementing second argument:
903 idx = lua_tointeger(L, 2) + 1;
904 // push shadow table onto stack position 3
905 // or throw error if no shadow table is found:
906 json_getshadow(L, 1);
907 if (lua_isnil(L, -1)) return luaL_error(L, "Shadow table not found");
908 // do integer lookup in shadow table and store result on stack position 4:
909 lua_rawgeti(L, 3, idx);
910 // return nothing if there was no value:
911 if (lua_isnil(L, 4)) return 0;
912 // return new index and
913 // either the looked up value if it is not equal to the null-marker
914 // or nil instead of null-marker:
915 lua_pushinteger(L, idx);
916 if (json_isnullmark(L, 4)) lua_pushnil(L);
917 else lua_pushvalue(L, 4);
918 return 2;
919 }
921 // returns a triple such that 'for idx, value in ipairs(ary) do ... end'
922 // iterates through all values (including JSON null values represented as Lua nil):
923 static int json_ipairs(lua_State *L) {
924 // require one argument to function
925 luaL_checkany(L, 1);
926 // return triple of function json_ipairs_iterfunc, first argument, and zero:
927 lua_pushcfunction(L, json_ipairs_iterfunc);
928 lua_pushvalue(L, 1);
929 lua_pushinteger(L, 0);
930 return 3;
931 }
933 // datatype representing a table key:
934 // (used for sorting)
935 typedef struct {
936 size_t length;
937 const char *data;
938 } json_key_t;
940 // comparation function for table keys to be passed to qsort function:
941 static int json_key_cmp(json_key_t *key1, json_key_t *key2) {
942 size_t pos = 0;
943 unsigned char c1, c2;
944 while (1) {
945 if (key1->length > pos) {
946 if (key2->length > pos) {
947 c1 = key1->data[pos];
948 c2 = key2->data[pos];
949 if (c1 < c2) return -1;
950 else if (c1 > c2) return 1;
951 } else {
952 return 1;
953 }
954 } else {
955 if (key2->length > pos) {
956 return -1;
957 } else {
958 return 0;
959 }
960 }
961 pos++;
962 }
963 }
965 // constants for type detection of ambiguous tables:
966 #define JSON_TABLETYPE_UNKNOWN 0
967 #define JSON_TABLETYPE_OBJECT 1
968 #define JSON_TABLETYPE_ARRAY 2
970 typedef struct {
971 int type;
972 int pos;
973 int count;
974 json_key_t keys[1]; // or more
975 } json_container_t;
977 // special Lua stack indicies for json_export function:
978 #define json_export_value_idx 1
979 #define json_export_indentstring_idx 2
980 #define json_export_objectmt_idx 3
981 #define json_export_arraymt_idx 4
982 #define json_export_stackswap_idx 5
983 #define json_export_luacontainer_idx 6
984 #define json_export_ccontainer_idx 7
985 #define json_export_buffer_idx 8
987 // encodes a JSON document (passed as first argument)
988 // optionally using indentation (indentation string or true passed as second argument)
989 static int json_export(lua_State *L) {
990 int pretty; // pretty printing on? (i.e. printing with indentation)
991 luaL_Buffer buf; // Lua buffer containing result string
992 lua_Number num; // number to encode
993 char numstr[21]; // encoded number (sign, zero, point, 17 significant digits, and terminating NULL byte)
994 const char *str; // string to encode
995 size_t strlen; // length of string to encode
996 size_t strpos ; // position in string or position of current key
997 unsigned char c; // character to encode (unsigned!)
998 char hexcode[7]; // store for unicode hex escape sequence
999 // NOTE: 7 bytes due to backslash, character 'u', 4 hex digits, and terminating NULL byte
1000 int tabletype; // table type: unknown, JSON object, or JSON array
1001 size_t keycount = 0; // number of string keys in object
1002 json_key_t *key; // pointer to C structure containing a string key
1003 int level = 0; // current depth level
1004 int i; // iteration variable for level dependent repetitions
1005 int stackswapidx = 0; // elements in stack swap table
1006 int containerkey = 0; // temporarily set to 1, if a container key is being encoded
1007 json_container_t *container = NULL; // pointer to current C struct for container information
1008 // stack shall contain two function arguments:
1009 lua_settop(L, 2);
1010 // check if pretty printing (with indentation) is desired:
1011 if (lua_toboolean(L, json_export_indentstring_idx)) {
1012 // if yes,
1013 // set pretty variable to 1:
1014 pretty = 1;
1015 // check if second argument is a boolean (true):
1016 if (lua_isboolean(L, json_export_indentstring_idx)) {
1017 // if yes,
1018 // use default indentation if indentation argument is boolean true:
1019 lua_pushliteral(L, " ");
1020 lua_replace(L, json_export_indentstring_idx);
1021 } else {
1022 // if no,
1023 // require second argument to be a string:
1024 luaL_checktype(L, json_export_indentstring_idx, LUA_TSTRING);
1026 } else {
1027 // if no,
1028 // set pretty variable to 0:
1029 pretty = 0;
1031 // push objectmt onto stack position 3:
1032 json_regfetch(L, objectmt);
1033 // push arraymt onto stack position 4:
1034 json_regfetch(L, arraymt);
1035 // push table for stack swapping onto stack position 5:
1036 lua_newtable(L);
1037 // create placeholders on stack positions 6 through 7:
1038 lua_settop(L, json_export_buffer_idx);
1039 // create Lua string buffer:
1040 luaL_buffinit(L, &buf);
1041 // loop:
1042 while (1) {
1043 // if value to encode is the null-marker, then treat it the same as nil:
1044 if (json_isnullmark(L, json_export_value_idx)) {
1045 lua_pushnil(L);
1046 lua_replace(L, json_export_value_idx);
1048 // distinguish between different Lua types:
1049 switch (lua_type(L, json_export_value_idx)) {
1050 // value to encode is nil:
1051 case LUA_TNIL:
1052 // add string "null" to output buffer:
1053 luaL_addstring(&buf, "null");
1054 break;
1055 // value to encode is of type number:
1056 case LUA_TNUMBER:
1057 // convert value to double precision number:
1058 num = lua_tonumber(L, json_export_value_idx);
1059 // throw error if number is not-a-number:
1060 if (isnan(num)) return luaL_error(L, "JSON export not possible for NaN value");
1061 // throw error if number is positive or negative infinity:
1062 if (isinf(num)) return luaL_error(L, "JSON export not possible for infinite numbers");
1063 // determine necessary precision to represent double precision floating point number:
1064 sprintf(numstr, "%.16g", num);
1065 if (strtod(numstr, NULL) != num) sprintf(numstr, "%.17g", num);
1066 // add string encoding of the number to the output buffer:
1067 luaL_addstring(&buf, numstr);
1068 break;
1069 // value to encode is of type boolean:
1070 case LUA_TBOOLEAN:
1071 // add string "true" or "false" according to boolean value:
1072 luaL_addstring(&buf, lua_toboolean(L, json_export_value_idx) ? "true" : "false");
1073 break;
1074 // value to encode is of type string:
1075 case LUA_TSTRING:
1076 // add quoted and escaped string to output buffer:
1077 str = lua_tolstring(L, json_export_value_idx, &strlen);
1078 luaL_addchar(&buf, '"');
1079 strpos = 0;
1080 while (strpos < strlen) {
1081 c = str[strpos++];
1082 if (c == '"') luaL_addstring(&buf, "\\\"");
1083 else if (c == '\\') luaL_addstring(&buf, "\\\\");
1084 else if (c == 127) luaL_addstring(&buf, "\\u007F");
1085 else if (c >= 32) luaL_addchar(&buf, c);
1086 else if (c == '\b') luaL_addstring(&buf, "\\b");
1087 else if (c == '\f') luaL_addstring(&buf, "\\f");
1088 else if (c == '\n') luaL_addstring(&buf, "\\n");
1089 else if (c == '\r') luaL_addstring(&buf, "\\r");
1090 else if (c == '\t') luaL_addstring(&buf, "\\t");
1091 else if (c == '\v') luaL_addstring(&buf, "\\v");
1092 else {
1093 sprintf(hexcode, "\\u%04X", c);
1094 luaL_addstring(&buf, hexcode);
1097 luaL_addchar(&buf, '"');
1098 break;
1099 // value to encode is of type table (this includes JSON objects and JSON arrays):
1100 case LUA_TTABLE:
1101 // use table's metatable to try to determine type of table:
1102 tabletype = JSON_TABLETYPE_UNKNOWN;
1103 if (lua_getmetatable(L, json_export_value_idx)) {
1104 if (lua_rawequal(L, -1, json_export_objectmt_idx)) {
1105 tabletype = JSON_TABLETYPE_OBJECT;
1106 } else {
1107 if (lua_rawequal(L, -1, json_export_arraymt_idx)) {
1108 tabletype = JSON_TABLETYPE_ARRAY;
1109 } else {
1110 return luaL_error(L, "JSON export not possible for tables with nonsupported metatable");
1113 // reset stack (pop metatable from stack):
1114 lua_pop(L, 1);
1116 // replace table with its shadow table if existent:
1117 json_getshadow(L, json_export_value_idx);
1118 if (lua_isnil(L, -1)) lua_pop(L, 1);
1119 else lua_replace(L, json_export_value_idx);
1120 // check if type of table is still undetermined
1121 // and optionally calculate number of string keys (keycount)
1122 // or set keycount to zero:
1123 keycount = 0;
1124 if (tabletype == JSON_TABLETYPE_UNKNOWN) {
1125 // if type of table is undetermined,
1126 // iterate over all keys:
1127 for (lua_pushnil(L); lua_next(L, json_export_value_idx); lua_pop(L, 1)) {
1128 switch (lua_type(L, -2)) {
1129 case LUA_TSTRING:
1130 // for string keys,
1131 // increase keycount (may avoid another iteration):
1132 keycount++;
1133 // if type of table was unknown, then type of table is a JSON object now:
1134 if (tabletype == JSON_TABLETYPE_UNKNOWN) tabletype = JSON_TABLETYPE_OBJECT;
1135 // if type of table was a JSON array, then the type of table is ambiguous now
1136 // and an error is thrown:
1137 else if (tabletype == JSON_TABLETYPE_ARRAY) goto json_export_tabletype_error;
1138 break;
1139 case LUA_TNUMBER:
1140 // for numeric keys,
1141 // if type of table was unknown, then type of table is a JSON array now:
1142 if (tabletype == JSON_TABLETYPE_UNKNOWN) tabletype = JSON_TABLETYPE_ARRAY;
1143 // if type of table was a JSON object, then the type of table is ambiguous now
1144 // and an error is thrown:
1145 else if (tabletype == JSON_TABLETYPE_OBJECT) goto json_export_tabletype_error;
1146 break;
1150 // raise error if too many nested levels:
1151 if (level >= JSON_MAXDEPTH) {
1152 return luaL_error(L, "More than %d nested JSON levels", JSON_MAXDEPTH);
1154 // store previous container information (if existent) on stack swap
1155 // and increase level variable:
1156 if (level++) {
1157 lua_pushvalue(L, json_export_luacontainer_idx);
1158 lua_rawseti(L, json_export_stackswap_idx, ++stackswapidx);
1159 lua_pushvalue(L, json_export_ccontainer_idx);
1160 lua_rawseti(L, json_export_stackswap_idx, ++stackswapidx);
1162 // use value as current container:
1163 lua_pushvalue(L, json_export_value_idx);
1164 lua_replace(L, json_export_luacontainer_idx);
1165 // distinguish between JSON objects and JSON arrays:
1166 switch (tabletype) {
1167 // JSON object:
1168 case JSON_TABLETYPE_OBJECT:
1169 // calculate count of string keys unless it has been calculated before:
1170 if (!keycount) {
1171 for (lua_pushnil(L); lua_next(L, json_export_luacontainer_idx); lua_pop(L, 1)) {
1172 if (lua_type(L, -2) == LUA_TSTRING) keycount++;
1175 // allocate memory for C structure containing string keys and container iteration state:
1176 container = lua_newuserdata(L, sizeof(json_container_t) + (keycount-1) * sizeof(json_key_t));
1177 // store reference to C structure on designated stack position:
1178 lua_replace(L, json_export_ccontainer_idx);
1179 // initialize C structure for container state:
1180 container->type = JSON_TABLETYPE_OBJECT;
1181 container->count = keycount;
1182 container->pos = 0;
1183 // check if object contains any keys:
1184 if (keycount) {
1185 // if yes,
1186 // copy all string keys to the C structure (and reset container->pos again):
1187 for (lua_pushnil(L); lua_next(L, json_export_luacontainer_idx); lua_pop(L, 1)) {
1188 if (lua_type(L, -2) == LUA_TSTRING) {
1189 json_key_t *key = &container->keys[container->pos++];
1190 key->data = lua_tolstring(L, -2, &key->length);
1193 container->pos = 0;
1194 // sort C array using quicksort:
1195 qsort(container->keys, keycount, sizeof(json_key_t), (void *)json_key_cmp);
1197 // add opening bracket to output buffer:
1198 luaL_addchar(&buf, '{');
1199 break;
1200 // JSON array:
1201 case JSON_TABLETYPE_ARRAY:
1202 // allocate memory for C structure for container iteration state:
1203 container = lua_newuserdata(L, sizeof(json_container_t) - sizeof(json_key_t));
1204 // store reference to C structure on designated stack position:
1205 lua_replace(L, json_export_ccontainer_idx);
1206 // initialize C structure for container state:
1207 container->type = JSON_TABLETYPE_ARRAY;
1208 container->pos = 0;
1209 // add opening bracket to output buffer:
1210 luaL_addchar(&buf, '[');
1211 break;
1212 default:
1213 // throw error if table type is unknown:
1214 json_export_tabletype_error:
1215 return luaL_error(L, "JSON export not possible for ambiguous table (cannot decide whether it is an object or array)");
1217 break;
1218 default:
1219 // all other datatypes are considered an error:
1220 return luaL_error(L, "JSON export not possible for values of type \"%s\"", lua_typename(L, lua_type(L, json_export_value_idx)));
1222 // check if a container is being processed:
1223 if (container) {
1224 // if yes,
1225 // execute code for container iteration:
1226 json_export_container:
1227 // distinguish between JSON objects and JSON arrays:
1228 switch (container->type) {
1229 // JSON object:
1230 case JSON_TABLETYPE_OBJECT:
1231 // finish iteration if all string keys have been processed:
1232 if (container->pos == container->count) goto json_export_close;
1233 // push current string key on top of stack:
1234 key = &container->keys[container->pos];
1235 lua_pushlstring(L, key->data, key->length);
1236 // check if the key has already been exported:
1237 if (!containerkey) {
1238 // if no,
1239 // add a comma to the output buffer if necessary:
1240 if (container->pos) luaL_addchar(&buf, ',');
1241 // set containerkey variable to true:
1242 containerkey = 1;
1243 } else {
1244 // if a key has already been exported,
1245 // add a colon to the output buffer:
1246 luaL_addchar(&buf, ':');
1247 // add a space to the output buffer for pretty results:
1248 if (pretty) luaL_addchar(&buf, ' ');
1249 // replace string key on top of stack with corresponding value:
1250 lua_rawget(L, json_export_luacontainer_idx);
1251 // reset containerkey variable
1252 containerkey = 0;
1253 // increase number of processed key value pairs:
1254 container->pos++;
1256 // store key or value on top of stack in designated stack position:
1257 lua_replace(L, json_export_value_idx);
1258 break;
1259 // JSON array:
1260 case JSON_TABLETYPE_ARRAY:
1261 // store next value in designated stack position:
1262 lua_rawgeti(L, json_export_luacontainer_idx, container->pos+1);
1263 lua_replace(L, json_export_value_idx);
1264 // finish iteration if value is nil:
1265 if (lua_isnil(L, json_export_value_idx)) goto json_export_close;
1266 // add a comma to the output buffer if necessary:
1267 if (container->pos) luaL_addchar(&buf, ',');
1268 // increase number of processed values:
1269 container->pos++;
1270 break;
1271 // common code for closing JSON objects or JSON arrays:
1272 json_export_close:
1273 // decrement level variable:
1274 level--;
1275 // handle indentation for pretty results:
1276 if (pretty && container->pos) {
1277 luaL_addchar(&buf, '\n');
1278 for (i=0; i<level; i++) {
1279 lua_pushvalue(L, json_export_indentstring_idx);
1280 luaL_addvalue(&buf);
1283 // add closing bracket to output buffer:
1284 luaL_addchar(&buf, container->type == JSON_TABLETYPE_OBJECT ? '}' : ']');
1285 // finish export if last level has been closed:
1286 if (!level) goto json_export_finish;
1287 // otherwise,
1288 // recall previous container information from stack swap
1289 // and set C pointer to corresponding C struct:
1290 lua_rawgeti(L, json_export_stackswap_idx, stackswapidx--);
1291 lua_replace(L, json_export_ccontainer_idx);
1292 container = lua_touserdata(L, json_export_ccontainer_idx);
1293 lua_rawgeti(L, json_export_stackswap_idx, stackswapidx--);
1294 lua_replace(L, json_export_luacontainer_idx);
1295 // repeat code for container iteration:
1296 goto json_export_container;
1298 // handle indentation for pretty results:
1299 if (pretty && (containerkey || container->type == JSON_TABLETYPE_ARRAY)) {
1300 luaL_addchar(&buf, '\n');
1301 for (i=0; i<level; i++) {
1302 lua_pushvalue(L, json_export_indentstring_idx);
1303 luaL_addvalue(&buf);
1306 } else {
1307 // if no container is being processed,
1308 // finish export:
1309 json_export_finish:
1310 // for pretty results, add final newline character if outermost container is processed:
1311 if (pretty) luaL_addchar(&buf, '\n');
1312 // create and return Lua string from buffer contents
1313 luaL_pushresult(&buf);
1314 return 1;
1319 // functions in library module:
1320 static const struct luaL_Reg json_module_functions[] = {
1321 {"object", json_object},
1322 {"array", json_array},
1323 {"import", json_import},
1324 {"export", json_export},
1325 {"get", json_get},
1326 {"type", json_type},
1327 {"set", json_set},
1328 {NULL, NULL}
1329 };
1331 // metamethods for JSON objects, JSON arrays, and unknown JSON collections (object or array):
1332 static const struct luaL_Reg json_metatable_functions[] = {
1333 {"__len", json_len},
1334 {"__index", json_index},
1335 {"__newindex", json_newindex},
1336 {"__pairs", json_pairs},
1337 {"__ipairs", json_ipairs},
1338 {"__tostring", json_export},
1339 {NULL, NULL}
1340 };
1342 // metamethods for JSON null marker:
1343 static const struct luaL_Reg json_nullmark_metamethods[] = {
1344 {"__tostring", json_nullmark_tostring},
1345 {NULL, NULL}
1346 };
1348 // initializes json library:
1349 int luaopen_json(lua_State *L) {
1350 // empty stack:
1351 lua_settop(L, 0);
1352 // push library module onto stack position 1:
1353 lua_newtable(L);
1354 // register library functions:
1355 luaL_setfuncs(L, json_module_functions, 0);
1356 // create and store objectmt:
1357 lua_newtable(L);
1358 luaL_setfuncs(L, json_metatable_functions, 0);
1359 json_regstore(L, objectmt);
1360 // create and store arraymt:
1361 lua_newtable(L);
1362 luaL_setfuncs(L, json_metatable_functions, 0);
1363 json_regstore(L, arraymt);
1364 // set metatable of null marker and make it available through library module:
1365 json_pushnullmark(L);
1366 lua_newtable(L);
1367 luaL_setfuncs(L, json_nullmark_metamethods, 0);
1368 lua_setmetatable(L, -2);
1369 lua_setfield(L, 1, "null");
1370 // return library module (that's expected on top of stack):
1371 return 1;

Impressum / About Us