webmcp

view libraries/json/json.c @ 167:84497222db4e

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

Impressum / About Us