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