webmcp

view libraries/json/json.c @ 153:c8c91216255f

Correct treatment of top-level null values in JSON parser
author jbe
date Thu Jul 31 01:21:33 2014 +0200 (2014-07-31)
parents 7b5c13fdc2ec
children c8669dde9ce2
line source
1 #include <lua.h>
2 #include <lauxlib.h>
3 #include <stdlib.h>
4 #include <string.h>
6 // maximum number of nested JSON values (objects and arrays):
7 // NOTE: The Lua reference states that the stack may typically contain at least
8 // "a few thousand elements". Since every nested level consumes
9 // 3 elements on the Lua stack (the object/array, its shadow table,
10 // a string key or a placeholder), we limit the number of nested levels
11 // to 500. If a stack overflow would still happen in the import function,
12 // this is detected nevertheless and an error is thrown (instead of
13 // returning nil and an error string).
14 #define JSON_MAXDEPTH 500
16 // macros for usage of Lua registry:
17 #define JSON_REGENT char
18 #define JSON_REGPOINTER void *
19 #define json_pushlightref(L, x) lua_pushlightuserdata((L), &json_reference.x)
20 #define json_regpointer(x) (&json_registry.x)
21 #define json_regfetchpointer(L, x) lua_rawgetp((L), LUA_REGISTRYINDEX, (x))
22 #define json_regfetch(L, x) json_regfetchpointer(L, json_regpointer(x))
23 #define json_regstore(L, x) lua_rawsetp(L, LUA_REGISTRYINDEX, json_regpointer(x))
25 // generate dummy memory addresses that represent non-modifiable lightuserdata (dummy) objects:
26 static struct {
27 JSON_REGENT nullmark; // magic value to indicate JSON null value in shadow table
28 } json_reference;
31 // generate dummy memory addresses that represent Lua objects
32 // via lightuserdata keys and LUA_REGISTRYINDEX:
33 static struct {
34 JSON_REGENT shadowtbl; // ephemeron table that maps tables to their corresponding shadow table
35 JSON_REGENT unknownmt; // metatable for tables that may be either JSON objects or JSON arrays
36 JSON_REGENT objectmt; // metatable for JSON objects
37 JSON_REGENT arraymt; // metatable for JSON arrays
38 } json_registry;
40 // marks a Lua table as JSON object or JSON array:
41 // (returns its modified argument or a new table if argument is nil)
42 static int json_mark(lua_State *L, JSON_REGPOINTER mt) {
43 // check if argument is nil
44 if (lua_isnoneornil(L, 1)) {
45 // create new table at stack position 1:
46 lua_settop(L, 0);
47 lua_newtable(L);
48 // create shadow table (leaving previously created table on stack position 1):
49 json_regfetch(L, shadowtbl);
50 lua_pushvalue(L, 1);
51 lua_newtable(L);
52 lua_rawset(L, -3);
53 } else {
54 // push shadow table on top of stack:
55 json_regfetch(L, shadowtbl);
56 lua_pushvalue(L, 1);
57 lua_rawget(L, -2);
58 // if shadow table does not exist:
59 if (lua_isnil(L, -1)) {
60 // create shadow table and leave it on top of stack:
61 lua_newtable(L);
62 lua_pushvalue(L, 1);
63 lua_pushvalue(L, -2);
64 lua_rawset(L, -5);
65 }
66 // move elements from original table to shadow table (that's expected on top of stack):
67 for(lua_pushnil(L); lua_next(L, 1); lua_pop(L, 1)) {
68 lua_pushvalue(L, -2);
69 lua_pushnil(L);
70 lua_rawset(L, 1);
71 lua_pushvalue(L, -2);
72 lua_pushvalue(L, -2);
73 lua_rawset(L, -5);
74 }
75 }
76 // discard everything but table to return:
77 lua_settop(L, 1);
78 // set metatable:
79 json_regfetchpointer(L, mt);
80 lua_setmetatable(L, 1);
81 // return table:
82 return 1;
83 }
85 // marks a table as JSON object:
86 // (returns its modified argument or a new table if argument is nil)
87 static int json_object(lua_State *L) {
88 return json_mark(L, json_regpointer(objectmt));
89 }
91 // marks a table as JSON array:
92 // (returns its modified argument or a new table if argument is nil)
93 static int json_array(lua_State *L) {
94 return json_mark(L, json_regpointer(arraymt));
95 }
97 // internal states of JSON parser:
98 #define JSON_STATE_VALUE 0
99 #define JSON_STATE_OBJECT_KEY 1
100 #define JSON_STATE_OBJECT_KEY_TERMINATOR 2
101 #define JSON_STATE_OBJECT_VALUE 3
102 #define JSON_STATE_OBJECT_SEPARATOR 4
103 #define JSON_STATE_ARRAY_VALUE 5
104 #define JSON_STATE_ARRAY_SEPARATOR 6
105 #define JSON_STATE_END 7
107 // special Lua stack indicies for json_import function:
108 #define json_import_objectmt_idx 2
109 #define json_import_arraymt_idx 3
110 #define json_import_shadowtbl_idx 4
111 #define json_import_nullmark_idx 5
113 // decodes a JSON document:
114 static int json_import(lua_State *L) {
115 const char *str; // string to parse
116 size_t total; // total length of string to parse
117 size_t pos = 0; // current position in string to parse
118 size_t level = 0; // nested levels of objects/arrays currently being processed
119 int mode = JSON_STATE_VALUE; // state of parser (i.e. "what's expected next?")
120 char c; // variable to store a single character to be processed
121 luaL_Buffer luabuf; // Lua buffer to decode JSON string values
122 char *cbuf; // C buffer to decode JSON string values
123 size_t writepos; // write position of decoded strings in C buffer
124 size_t arraylen; // variable to temporarily store the array length
125 // stack shall contain one function argument:
126 lua_settop(L, 1);
127 // push objectmt onto stack position 2:
128 json_regfetch(L, objectmt);
129 // push arraymt onto stack position 3:
130 json_regfetch(L, arraymt);
131 // push shadowtbl onto stack position 4:
132 json_regfetch(L, shadowtbl);
133 // push nullmark onto stack position 5:
134 json_pushlightref(L, nullmark);
135 // require string as first argument:
136 str = luaL_checklstring(L, 1, &total);
137 // if string contains a NULL byte, this is a syntax error
138 if (strlen(str) != total) goto json_import_syntax_error;
139 // main loop of parser:
140 json_import_loop:
141 // skip whitespace and store next character in variable 'c':
142 while (c = str[pos],
143 c == ' ' ||
144 c == '\f' ||
145 c == '\n' ||
146 c == '\r' ||
147 c == '\t' ||
148 c == '\v'
149 ) pos++;
150 // switch statement to handle certain (single) characters:
151 switch (c) {
152 // handle end of JSON document:
153 case 0:
154 // if end of JSON document was expected, then return top element of stack as result:
155 if (mode == JSON_STATE_END) return 1;
156 // otherwise, the JSON document was malformed:
157 json_import_unexpected_eof:
158 lua_pushnil(L);
159 if (level == 0) lua_pushliteral(L, "Empty string");
160 else lua_pushliteral(L, "Unexpected end of JSON document");
161 return 2;
162 // new JSON object:
163 case '{':
164 // if a JSON object is not expected here, then return an error:
165 if (
166 mode != JSON_STATE_VALUE &&
167 mode != JSON_STATE_OBJECT_VALUE &&
168 mode != JSON_STATE_ARRAY_VALUE
169 ) goto json_import_syntax_error;
170 // create JSON object on stack:
171 lua_newtable(L);
172 // set metatable of JSON object:
173 lua_pushvalue(L, json_import_objectmt_idx);
174 lua_setmetatable(L, -2);
175 // create internal shadow table on stack:
176 lua_newtable(L);
177 // register internal shadow table:
178 lua_pushvalue(L, -2);
179 lua_pushvalue(L, -2);
180 lua_rawset(L, json_import_shadowtbl_idx);
181 // expect object key (or end of object) to follow:
182 mode = JSON_STATE_OBJECT_KEY;
183 // jump to common code for opening JSON object and JSON array:
184 goto json_import_open;
185 // new JSON array:
186 case '[':
187 // if a JSON array is not expected here, then return an error:
188 if (
189 mode != JSON_STATE_VALUE &&
190 mode != JSON_STATE_OBJECT_VALUE &&
191 mode != JSON_STATE_ARRAY_VALUE
192 ) goto json_import_syntax_error;
193 // create JSON array on stack:
194 lua_newtable(L);
195 // set metatable of JSON array:
196 lua_pushvalue(L, json_import_arraymt_idx);
197 lua_setmetatable(L, -2);
198 // create internal shadow table on stack:
199 lua_newtable(L);
200 // register internal shadow table:
201 lua_pushvalue(L, -2);
202 lua_pushvalue(L, -2);
203 lua_rawset(L, json_import_shadowtbl_idx);
204 // add nil as key (needed to keep stack balance) and as magic to detect arrays:
205 lua_pushnil(L);
206 // expect array value (or end of array) to follow:
207 mode = JSON_STATE_ARRAY_VALUE;
208 // continue with common code for opening JSON object and JSON array:
209 // common code for opening JSON object or JSON array:
210 json_import_open:
211 // limit nested levels:
212 if (level >= JSON_MAXDEPTH) {
213 lua_pushnil(L);
214 lua_pushliteral(L, "Too many nested JSON levels");
215 return 2;
216 }
217 // additional buffer overflow protection:
218 if (!lua_checkstack(L, LUA_MINSTACK))
219 return luaL_error(L, "Caught stack overflow in JSON import function (too many nested levels and stack size too small)");
220 // increment level:
221 level++;
222 // consume input character:
223 pos++;
224 goto json_import_loop;
225 // end of JSON object:
226 case '}':
227 // if end of JSON object is not expected here, then return an error:
228 if (
229 mode != JSON_STATE_OBJECT_KEY &&
230 mode != JSON_STATE_OBJECT_SEPARATOR
231 ) goto json_import_syntax_error;
232 // jump to common code for end of JSON object and JSON array:
233 goto json_import_close;
234 // end of JSON array:
235 case ']':
236 // if end of JSON array is not expected here, then return an error:
237 if (
238 mode != JSON_STATE_ARRAY_VALUE &&
239 mode != JSON_STATE_ARRAY_SEPARATOR
240 ) goto json_import_syntax_error;
241 // pop nil key/magic (that was needed to keep stack balance):
242 lua_pop(L, 1);
243 // continue with common code for end of JSON object and JSON array:
244 // common code for end of JSON object or JSON array:
245 json_import_close:
246 // consume input character:
247 pos++;
248 // pop shadow table:
249 lua_pop(L, 1);
250 // check if nested:
251 if (--level) {
252 // if nested,
253 // check if outer(!) structure is an array or object:
254 if (lua_isnil(L, -2)) {
255 // select array value processing:
256 mode = JSON_STATE_ARRAY_VALUE;
257 } else {
258 // select object value processing:
259 mode = JSON_STATE_OBJECT_VALUE;
260 }
261 // store value in outer structure:
262 goto json_import_process_value;
263 }
264 // if not nested, then expect end of JSON document and continue with loop:
265 mode = JSON_STATE_END;
266 goto json_import_loop;
267 // key terminator:
268 case ':':
269 // if key terminator is not expected here, then return an error:
270 if (mode != JSON_STATE_OBJECT_KEY_TERMINATOR)
271 goto json_import_syntax_error;
272 // consume input character:
273 pos++;
274 // expect object value to follow:
275 mode = JSON_STATE_OBJECT_VALUE;
276 // continue with loop:
277 goto json_import_loop;
278 // value terminator (NOTE: trailing comma at end of value or key-value list is tolerated by this parser)
279 case ',':
280 // branch according to parser state:
281 if (mode == JSON_STATE_OBJECT_SEPARATOR) {
282 // expect an object key to follow:
283 mode = JSON_STATE_OBJECT_KEY;
284 } else if (mode == JSON_STATE_ARRAY_SEPARATOR) {
285 // expect an array value to follow:
286 mode = JSON_STATE_ARRAY_VALUE;
287 } else {
288 // if value terminator is not expected here, then return an error:
289 goto json_import_syntax_error;
290 }
291 // consume input character:
292 pos++;
293 // continue with loop:
294 goto json_import_loop;
295 // string literal:
296 case '"':
297 // consume quote character:
298 pos++;
299 // prepare buffer to decode string (with maximum possible length) and set write position to zero:
300 cbuf = luaL_buffinitsize(L, &luabuf, total-pos);
301 writepos = 0;
302 // loop through the characters until encountering end quote:
303 while ((c = str[pos++]) != '"') {
304 if (c == 0) {
305 // handle unexpected end of JSON document:
306 goto json_import_unexpected_eof;
307 } else if (c < 32 || c == 127) {
308 // do not allow ASCII control characters:
309 // NOTE: illegal UTF-8 sequences and extended control characters are not sanitized
310 // by this parser to allow different encodings than Unicode
311 lua_pushnil(L);
312 lua_pushliteral(L, "Unexpected control character in JSON string");
313 return 2;
314 } else if (c == '\\') {
315 // read next char after backslash escape:
316 c = str[pos++];
317 switch (c) {
318 // unexpected end-of-string:
319 case 0:
320 goto json_import_unexpected_eof;
321 // unescaping of quotation mark, slash, and backslash:
322 case '"':
323 case '/':
324 case '\\':
325 cbuf[writepos++] = c;
326 break;
327 // unescaping of backspace:
328 case 'b': cbuf[writepos++] = '\b'; break;
329 // unescaping of form-feed:
330 case 'f': cbuf[writepos++] = '\f'; break;
331 // unescaping of new-line:
332 case 'n': cbuf[writepos++] = '\n'; break;
333 // unescaping of carriage-return:
334 case 'r': cbuf[writepos++] = '\r'; break;
335 // unescaping of tabulator:
336 case 't': cbuf[writepos++] = '\t'; break;
337 // unescaping of UTF-16 characters
338 case 'u':
339 lua_pushnil(L);
340 lua_pushliteral(L, "JSON unicode escape sequences are not implemented yet"); // TODO
341 return 2;
342 // unexpected escape sequence:
343 default:
344 lua_pushnil(L);
345 lua_pushliteral(L, "Unexpected string escape sequence in JSON document");
346 return 2;
347 }
348 } else {
349 // normal character:
350 cbuf[writepos++] = c;
351 }
352 }
353 // process buffer to Lua string:
354 luaL_pushresultsize(&luabuf, writepos);
355 // continue with processing of decoded string:
356 goto json_import_process_value;
357 }
358 // process values whose type is is not deducible from a single character:
359 if ((c >= '0' && c <= '9') || c == '-' || c == '+') {
360 // for numbers,
361 // use strtod() call to parse a (double precision) floating point number:
362 char *endptr;
363 double numval;
364 numval = strtod(str+pos, &endptr);
365 // catch parsing errors:
366 if (endptr == str+pos) goto json_import_syntax_error;
367 // consume characters that were parsed:
368 pos += endptr - (str+pos);
369 // push parsed (double precision) floating point number on Lua stack:
370 lua_pushnumber(L, numval);
371 } else if (!strncmp(str+pos, "true", 4)) {
372 // consume 4 input characters for "true":
373 pos += 4;
374 // put Lua true value onto stack:
375 lua_pushboolean(L, 1);
376 } else if (!strncmp(str+pos, "false", 5)) {
377 // consume 5 input characters for "false":
378 pos += 5;
379 // put Lua false value onto stack:
380 lua_pushboolean(L, 0);
381 } else if (!strncmp(str+pos, "null", 4)) {
382 // consume 4 input characters for "null":
383 pos += 4;
384 // different behavor for top-level and sub-levels:
385 if (level) {
386 // if sub-level,
387 // push special null-marker onto stack:
388 lua_pushvalue(L, json_import_nullmark_idx);
389 } else {
390 // if top-level,
391 // push nil onto stack:
392 lua_pushnil(L);
393 }
394 } else {
395 // all other cases are a syntax error:
396 goto json_import_syntax_error;
397 }
398 // process a decoded value or key value pair (expected on top of Lua stack):
399 json_import_process_value:
400 switch (mode) {
401 // an object key has been read:
402 case JSON_STATE_OBJECT_KEY:
403 // if an object key is not a string, then this is a syntax error:
404 if (lua_type(L, -1) != LUA_TSTRING) goto json_import_syntax_error;
405 // expect key terminator to follow:
406 mode = JSON_STATE_OBJECT_KEY_TERMINATOR;
407 // continue with loop:
408 goto json_import_loop;
409 // a key value pair has been read:
410 case JSON_STATE_OBJECT_VALUE:
411 // store key value pair in outer shadow table:
412 lua_rawset(L, -3);
413 // expect value terminator (or end of object) to follow:
414 mode = JSON_STATE_OBJECT_SEPARATOR;
415 // continue with loop:
416 goto json_import_loop;
417 // an array value has been read:
418 case JSON_STATE_ARRAY_VALUE:
419 // get current array length:
420 arraylen = lua_rawlen(L, -3);
421 // throw error if array would exceed INT_MAX elements:
422 // TODO: Lua 5.3 may support more elements
423 if (arraylen >= INT_MAX) {
424 lua_pushnil(L);
425 lua_pushfstring(L, "Array exceeded length of %d elements", INT_MAX);
426 }
427 // store value in outer shadow table:
428 lua_rawseti(L, -3, arraylen + 1);
429 // expect value terminator (or end of object) to follow:
430 mode = JSON_STATE_ARRAY_SEPARATOR;
431 // continue with loop
432 goto json_import_loop;
433 // a single value has been read:
434 case JSON_STATE_VALUE:
435 // leave value on top of stack, expect end of JSON document, and continue with loop:
436 mode = JSON_STATE_END;
437 goto json_import_loop;
438 }
439 // syntax error handling (reachable by goto statement):
440 json_import_syntax_error:
441 lua_pushnil(L);
442 lua_pushliteral(L, "Syntax error in JSON document");
443 return 2;
444 }
446 // special Lua stack indicies for json_path function:
447 #define json_path_shadowtbl_idx 1
448 #define json_path_nullmark_idx 2
450 // stack offset of arguments to json_path function:
451 #define json_path_idxshift 2
453 // gets a value or its type from a JSON document (passed as first argument)
454 // using a path (passed as variable number of keys after first argument):
455 static int json_path(lua_State *L, int type_mode) {
456 int stacktop; // stack index of top of stack (after shifting)
457 int idx = 2 + json_path_idxshift; // stack index of current argument to process
458 // insert shadowtbl into stack at position 1 (shifting the arguments):
459 json_regfetch(L, shadowtbl);
460 lua_insert(L, 1);
461 // insert nullmark into stack at position 2 (shifting the arguments):
462 json_pushlightref(L, nullmark);
463 lua_insert(L, 2);
464 // store stack index of top of stack:
465 stacktop = lua_gettop(L);
466 // use first argument as "current value" (stored on top of stack):
467 lua_pushvalue(L, 1 + json_path_idxshift);
468 // process each "path key" (2nd argument and following arguments):
469 while (idx <= stacktop) {
470 // if "current value" (on top of stack) is nil, then the path cannot be walked and nil is returned:
471 if (lua_isnil(L, -1)) return 1;
472 // try to get shadow table of "current value":
473 lua_pushvalue(L, -1);
474 lua_rawget(L, json_path_shadowtbl_idx);
475 if (lua_isnil(L, -1)) {
476 // if no shadow table is found,
477 if (lua_type(L, -1) == LUA_TTABLE) {
478 // and if "current value" is a table,
479 // drop nil from stack:
480 lua_pop(L, 1);
481 // get "next value" using the "path key":
482 lua_pushvalue(L, idx++);
483 lua_gettable(L, -2);
484 } else {
485 // if "current value" is not a table,
486 // then the path cannot be walked and nil (already on top of stack) is returned:
487 return 1;
488 }
489 } else {
490 // if a shadow table is found,
491 // set "current value" to its shadow table:
492 lua_replace(L, -2);
493 // get "next value" using the "path key":
494 lua_pushvalue(L, idx++);
495 lua_rawget(L, -2);
496 }
497 // the "next value" replaces the "current value":
498 lua_replace(L, -2);
499 }
500 if (!type_mode) {
501 // if a value (and not its type) was requested,
502 // check if value is the null-marker, and store nil on top of Lua stack in that case:
503 if (lua_rawequal(L, -1, json_path_nullmark_idx)) lua_pushnil(L);
504 } else {
505 // if the type was requested,
506 // check if value is the null-marker:
507 if (lua_rawequal(L, -1, json_path_nullmark_idx)) {
508 // if yes, store string "null" on top of Lua stack:
509 lua_pushliteral(L, "null");
510 } else {
511 // otherwise,
512 // check if metatable indicates "object" or "array":
513 if (lua_getmetatable(L, -1)) {
514 json_regfetch(L, objectmt);
515 if (lua_rawequal(L, -2, -1)) {
516 // if value has metatable for JSON objects,
517 // return string "object":
518 lua_pushliteral(L, "object");
519 return 1;
520 }
521 json_regfetch(L, arraymt);
522 if (lua_rawequal(L, -3, -1)) {
523 // if value has metatable for JSON arrays,
524 // return string "object":
525 lua_pushliteral(L, "array");
526 return 1;
527 }
528 // remove 3 metatables (one of the value, two for comparison) from stack:
529 lua_pop(L, 3);
530 }
531 // otherwise, get the Lua type:
532 lua_pushstring(L, lua_typename(L, lua_type(L, -1)));
533 }
534 }
535 // return the top most value on the Lua stack:
536 return 1;
537 }
539 // gets a value from a JSON document (passed as first argument)
540 // using a path (passed as variable number of keys after first argument):
541 static int json_get(lua_State *L) {
542 return json_path(L, 0);
543 }
545 // gets a value's type from a JSON document (passed as first argument)
546 // using a path (variable number of keys after first argument):
547 static int json_type(lua_State *L) {
548 return json_path(L, 1);
549 }
551 // checks if a value in a JSON document (first argument) is
552 // explicitly set to null:
553 static int json_isnull(lua_State *L) {
554 const char *jsontype;
555 // call json_type function with variable arguments:
556 lua_pushcfunction(L, json_type);
557 lua_insert(L, 1);
558 lua_call(L, lua_gettop(L) - 1, 1);
559 // return true if result equals to string "null", otherwise return false:
560 jsontype = lua_tostring(L, -1);
561 if (jsontype && !strcmp(jsontype, "null")) lua_pushboolean(L, 1);
562 else lua_pushboolean(L, 0);
563 return 1;
564 }
566 // special Lua stack indicies for json_setnull function:
567 #define json_setnull_unknownmt_idx 3
568 #define json_setnull_objectmt_idx 4
569 #define json_setnull_arraymt_idx 5
570 #define json_setnull_shadowtbl_idx 6
572 // sets a value in a JSON object or JSON array explicitly to null:
573 // NOTE: JSON null is different than absence of a key
574 static int json_setnull(lua_State *L) {
575 // stack shall contain two function arguments:
576 lua_settop(L, 2);
577 // push unknownmt onto stack position 3:
578 json_regfetch(L, unknownmt);
579 // push objectmt onto stack position 4:
580 json_regfetch(L, objectmt);
581 // push arraymt onto stack position 5:
582 json_regfetch(L, arraymt);
583 // push shadowtbl onto stack position 6:
584 json_regfetch(L, shadowtbl);
585 // set metatable if necessary (leaves unknown number of elements on stack):
586 if (
587 !lua_getmetatable(L, 1) || (
588 !lua_rawequal(L, -1, json_setnull_unknownmt_idx) &&
589 !lua_rawequal(L, -1, json_setnull_objectmt_idx) &&
590 !lua_rawequal(L, -1, json_setnull_arraymt_idx)
591 )
592 ) {
593 lua_pushvalue(L, json_setnull_unknownmt_idx);
594 lua_setmetatable(L, 1);
595 }
596 // try to get shadow table:
597 lua_pushvalue(L, 1);
598 lua_rawget(L, json_setnull_shadowtbl_idx);
599 if (lua_isnil(L, -1)) {
600 // if no shadow table is found,
601 // create new shadow table (and leave it on top of stack):
602 lua_newtable(L);
603 // register shadow table:
604 lua_pushvalue(L, 1);
605 lua_pushvalue(L, -2);
606 lua_rawset(L, json_setnull_shadowtbl_idx);
607 }
608 // push key (second argument) and null-marker after shadow table onto stack:
609 lua_pushvalue(L, 2);
610 json_pushlightref(L, nullmark);
611 // store key and null-marker in shadow table:
612 lua_rawset(L, -3);
613 // return nothing:
614 return 0;
615 }
617 // returns the length of a JSON array (or zero for a table without numeric keys):
618 static int json_len(lua_State *L) {
619 // stack shall contain one function argument:
620 lua_settop(L, 1);
621 // try to get corresponding shadow table for first argument:
622 json_regfetch(L, shadowtbl);
623 lua_pushvalue(L, 1);
624 lua_rawget(L, -2);
625 // if shadow table does not exist, return length of argument, else length of shadow table:
626 lua_pushnumber(L, lua_rawlen(L, lua_isnil(L, -1) ? 1 : -1));
627 return 1;
628 }
630 // special Lua stack indicies for json_index function:
631 #define json_index_nullmark_idx 3
632 #define json_index_shadowtbl_idx 4
634 static int json_index(lua_State *L) {
635 // stack shall contain two function arguments:
636 lua_settop(L, 2);
637 // push nullmark onto stack position 3:
638 json_pushlightref(L, nullmark);
639 // push shadowtbl onto stack position 4:
640 json_regfetch(L, shadowtbl);
641 // get corresponding shadow table for first argument:
642 lua_pushvalue(L, 1);
643 lua_rawget(L, json_index_shadowtbl_idx);
644 // throw error if no shadow table was found:
645 if (lua_isnil(L, -1)) return luaL_error(L, "Shadow table not found");
646 // use key passed as second argument to lookup value in shadow table:
647 lua_pushvalue(L, 2);
648 lua_rawget(L, -2);
649 // if value is null-marker, then push nil onto stack:
650 if (lua_rawequal(L, -1, json_index_nullmark_idx)) lua_pushnil(L);
651 // return either looked up value, or nil
652 return 1;
653 }
655 static int json_newindex(lua_State *L) {
656 // stack shall contain three function arguments:
657 lua_settop(L, 3);
658 // get corresponding shadow table for first argument:
659 json_regfetch(L, shadowtbl);
660 lua_pushvalue(L, 1);
661 lua_rawget(L, -2);
662 // throw error if no shadow table was found:
663 if (lua_isnil(L, -1)) return luaL_error(L, "Shadow table not found");
664 // replace first argument with shadow table:
665 lua_replace(L, 1);
666 // reset stack and use second and third argument to write to shadow table:
667 lua_settop(L, 3);
668 lua_rawset(L, 1);
669 // return nothing:
670 return 0;
671 }
673 // special Lua stack indicies for json_pairs_iterfunc function:
674 #define json_pairs_iterfunc_nullmark_idx 3
675 #define json_pairs_iterfunc_shadowtbl_idx 4
677 static int json_pairs_iterfunc(lua_State *L) {
678 // stack shall contain two function arguments:
679 lua_settop(L, 2);
680 // push nullmark onto stack position 3:
681 json_pushlightref(L, nullmark);
682 // push shadowtbl onto stack position 4:
683 json_regfetch(L, shadowtbl);
684 // get corresponding shadow table for first argument:
685 lua_pushvalue(L, 1);
686 lua_rawget(L, json_pairs_iterfunc_shadowtbl_idx);
687 // throw error if no shadow table was found:
688 if (lua_isnil(L, -1)) return luaL_error(L, "Shadow table not found");
689 // get next key value pair from shadow table (using previous key from argument 2)
690 // and return nothing if there is no next pair:
691 lua_pushvalue(L, 2);
692 if (!lua_next(L, -2)) return 0;
693 // replace null-marker with nil:
694 if (lua_rawequal(L, -1, json_pairs_iterfunc_nullmark_idx)) {
695 lua_pop(L, 1);
696 lua_pushnil(L);
697 }
698 // return key and value (or key and nil, if null-marker was found):
699 return 2;
700 }
702 // returns a triple such that 'for key, value in pairs(obj) do ... end'
703 // iterates through all key value pairs (including JSON null keys represented as Lua nil):
704 static int json_pairs(lua_State *L) {
705 // return triple of function json_pairs_iterfunc, first argument, and nil:
706 lua_pushcfunction(L, json_pairs_iterfunc);
707 lua_pushvalue(L, 1);
708 lua_pushnil(L);
709 return 3;
710 }
712 // special Lua stack indicies for json_ipairs_iterfunc function:
713 #define json_ipairs_iterfunc_nullmark_idx 3
714 #define json_ipairs_iterfunc_shadowtbl_idx 4
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 // push nullmark onto stack position 3:
721 json_pushlightref(L, nullmark);
722 // push shadowtbl onto stack position 4:
723 json_regfetch(L, shadowtbl);
724 // calculate new index by incrementing second argument:
725 idx = lua_tointeger(L, 2) + 1;
726 // get corresponding shadow table for first argument:
727 lua_pushvalue(L, 1);
728 lua_rawget(L, json_ipairs_iterfunc_shadowtbl_idx);
729 // throw error if no shadow table was found:
730 if (lua_isnil(L, -1)) return luaL_error(L, "Shadow table not found");
731 // do integer lookup in shadow table:
732 lua_rawgeti(L, -1, idx);
733 // return nothing if there was no value:
734 if (lua_isnil(L, -1)) return 0;
735 // return new index and
736 // either the looked up value if it is not equal to the null-marker
737 // or nil instead of null-marker:
738 lua_pushinteger(L, idx);
739 if (lua_rawequal(L, -2, json_ipairs_iterfunc_nullmark_idx)) lua_pushnil(L);
740 else lua_pushvalue(L, -2);
741 return 2;
742 }
744 // returns a triple such that 'for idx, value in ipairs(ary) do ... end'
745 // iterates through all values (including JSON null represented as Lua nil):
746 static int json_ipairs(lua_State *L) {
747 // return triple of function json_ipairs_iterfunc, first argument, and zero:
748 lua_pushcfunction(L, json_ipairs_iterfunc);
749 lua_pushvalue(L, 1);
750 lua_pushinteger(L, 0);
751 return 3;
752 }
754 // functions in library module:
755 static const struct luaL_Reg json_module_functions[] = {
756 {"object", json_object},
757 {"array", json_array},
758 {"import", json_import},
759 {"get", json_get},
760 {"type", json_type},
761 {"isnull", json_isnull},
762 {"setnull", json_setnull},
763 {NULL, NULL}
764 };
766 // metamethods for JSON objects, JSON arrays, and unknown JSON collections (object or array):
767 static const struct luaL_Reg json_metatable_functions[] = {
768 {"__len", json_len},
769 {"__index", json_index},
770 {"__newindex", json_newindex},
771 {"__pairs", json_pairs},
772 {"__ipairs", json_ipairs},
773 {NULL, NULL}
774 };
776 // initializes json library:
777 int luaopen_json(lua_State *L) {
778 // empty stack:
779 lua_settop(L, 0);
780 // push library module onto stack position 1:
781 lua_newtable(L);
782 // register library functions:
783 luaL_setfuncs(L, json_module_functions, 0);
784 // create and store unknownmt:
785 lua_newtable(L);
786 luaL_setfuncs(L, json_metatable_functions, 0);
787 json_regstore(L, unknownmt);
788 // create and store objectmt:
789 lua_newtable(L);
790 luaL_setfuncs(L, json_metatable_functions, 0);
791 json_regstore(L, objectmt);
792 // create and store arraymt:
793 lua_newtable(L);
794 luaL_setfuncs(L, json_metatable_functions, 0);
795 json_regstore(L, arraymt);
796 // create and store ephemeron table to store shadow tables for each JSON object/array
797 // to allow NULL values returned as nil
798 lua_newtable(L);
799 lua_newtable(L); // metatable for ephemeron table
800 lua_pushliteral(L, "__mode");
801 lua_pushliteral(L, "k");
802 lua_rawset(L, -3);
803 lua_setmetatable(L, -2);
804 json_regstore(L, shadowtbl);
805 // return library module stored on lowest stack position:
806 lua_settop(L, 1);
807 return 1;
808 }

Impressum / About Us