webmcp

view libraries/json/json.c @ 144:0690fe79b673

Improved macros for handling Lua registry in JSON library
author jbe
date Wed Jul 30 19:24:06 2014 +0200 (2014-07-30)
parents 6f723e60acdc
children 0edc2f05c66a
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 #define JSON_MAXDEPTH 100
9 // macros for usage of Lua registry:
10 #define JSON_REGENT char
11 #define JSON_REGREF void *
12 #define json_regref(x) (&json_registry.x)
13 #define json_regfetchref(L, x) (lua_pushlightuserdata((L), (x)), lua_rawget((L), LUA_REGISTRYINDEX))
14 #define json_regfetch(L, x) (json_regfetchref(L, json_regref(x)))
15 #define json_regstore(L, x) (lua_pushlightuserdata(L, json_regref(x)), lua_pushvalue(L, -2), lua_rawset(L, LUA_REGISTRYINDEX));
17 // generate dummy memory addresses that represent Lua objects
18 // (via lightuserdata keys and LUA_REGISTRYINDEX):
19 static struct {
20 JSON_REGENT nullmark;
21 JSON_REGENT shadowtbl;
22 JSON_REGENT unknownmt;
23 JSON_REGENT objectmt;
24 JSON_REGENT arraymt;
25 } json_registry;
27 // marks a table as JSON object or JSON array:
28 // (returns its modified argument or a new table if argument is nil)
29 static int json_mark(lua_State *L, JSON_REGREF mt) {
30 // if argument is nil, then create new table:
31 if (lua_isnoneornil(L, 1)) {
32 lua_settop(L, 0);
33 lua_newtable(L);
34 // set shadow table:
35 json_regfetch(L, shadowtbl);
36 lua_pushvalue(L, 1);
37 lua_newtable(L);
38 lua_rawset(L, -3);
39 } else {
40 // check if shadow table already exists:
41 json_regfetch(L, shadowtbl);
42 lua_pushvalue(L, 1);
43 lua_rawget(L, -2);
44 if (lua_isnil(L, -1)) {
45 // set shadow table and leave it on top of stack:
46 lua_newtable(L);
47 lua_pushvalue(L, 1);
48 lua_pushvalue(L, -2);
49 lua_rawset(L, -5);
50 }
51 for(lua_pushnil(L); lua_next(L, 1); lua_pop(L, 1)) {
52 lua_pushvalue(L, -2);
53 lua_pushnil(L);
54 lua_rawset(L, 1);
55 lua_pushvalue(L, -2);
56 lua_pushvalue(L, -2);
57 lua_rawset(L, -5);
58 }
59 }
60 // discard everything but table to return:
61 lua_settop(L, 1);
62 // set metatable:
63 json_regfetchref(L, mt);
64 lua_setmetatable(L, 1);
65 // return table:
66 return 1;
67 }
69 // marks a table as JSON object:
70 // (returns its modified argument or a new table if argument is nil)
71 static int json_object(lua_State *L) {
72 return json_mark(L, json_regref(objectmt));
73 }
75 // marks a table as JSON array:
76 // (returns its modified argument or a new table if argument is nil)
77 static int json_array(lua_State *L) {
78 return json_mark(L, json_regref(arraymt));
79 }
81 #define JSON_STATE_VALUE 0
82 #define JSON_STATE_OBJECT_KEY 1
83 #define JSON_STATE_OBJECT_KEY_TERMINATOR 2
84 #define JSON_STATE_OBJECT_VALUE 3
85 #define JSON_STATE_OBJECT_SEPARATOR 4
86 #define JSON_STATE_ARRAY_VALUE 5
87 #define JSON_STATE_ARRAY_SEPARATOR 6
88 #define JSON_STATE_END 7
90 #define json_import_objectmt_idx 2
91 #define json_import_arraymt_idx 3
92 #define json_import_shadowtbl_idx 4
93 #define json_import_nullmark_idx 5
95 // decodes a JSON document:
96 static int json_import(lua_State *L) {
97 const char *str; // string to parse
98 size_t total; // total length of string to parse
99 size_t pos = 0; // current position in string to parse
100 size_t level = 0; // nested levels of objects/arrays currently being processed
101 int mode = JSON_STATE_VALUE; // state of parser
102 char c; // variable to store a single character to be processed
103 luaL_Buffer luabuf; // Lua buffer to decode (possibly escaped) strings
104 char *cbuf; // C buffer to decode (possibly escaped) strings
105 size_t writepos; // write position of decoded strings in C buffer
106 // limit stack to 1 element:
107 lua_settop(L, 1);
108 // push json_objectmt on stack position 2:
109 json_regfetch(L, objectmt);
110 // push json_arraymt on stack position 3:
111 json_regfetch(L, arraymt);
112 // push json_shadowtbl on stack position 4:
113 json_regfetch(L, shadowtbl);
114 // push json_nullmark on stack position 5:
115 json_regfetch(L, nullmark);
116 // require string as first argument:
117 str = luaL_checklstring(L, 1, &total);
118 // if string contains a NULL byte, this is a syntax error
119 if (strlen(str) != total) goto json_import_syntax_error;
120 // main loop of parser:
121 json_import_loop:
122 // skip whitespace and store next character in variable 'c':
123 while (c = str[pos], c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == '\f') pos++;
124 // switch statement to handle certain (single) characters:
125 switch (c) {
126 // handle end of JSON document:
127 case 0:
128 // if end of JSON document was expected, then return top element of stack as result:
129 if (mode == JSON_STATE_END) return 1;
130 // otherwise, the JSON document was malformed:
131 json_import_unexpected_eof:
132 lua_pushnil(L);
133 if (level == 0) lua_pushliteral(L, "Empty string");
134 else lua_pushliteral(L, "Unexpected end of JSON document");
135 return 2;
136 // new JSON object:
137 case '{':
138 // if a JSON object is not expected here, then return an error:
139 if (mode != JSON_STATE_VALUE && mode != JSON_STATE_OBJECT_VALUE && mode != JSON_STATE_ARRAY_VALUE)
140 goto json_import_syntax_error;
141 // create JSON object on stack:
142 lua_newtable(L);
143 // set metatable of JSON object:
144 lua_pushvalue(L, json_import_objectmt_idx);
145 lua_setmetatable(L, -2);
146 // create internal shadow table on stack:
147 lua_newtable(L);
148 // register internal shadow table (and cleanup stack afterwards):
149 lua_pushvalue(L, -2);
150 lua_pushvalue(L, -2);
151 lua_rawset(L, json_import_shadowtbl_idx);
152 // expect object key (or end of object) and continue with loop:
153 mode = JSON_STATE_OBJECT_KEY;
154 goto json_import_open;
155 // new JSON array:
156 case '[':
157 // if a JSON array is not expected here, then return an error:
158 if (mode != JSON_STATE_VALUE && mode != JSON_STATE_OBJECT_VALUE && mode != JSON_STATE_ARRAY_VALUE)
159 goto json_import_syntax_error;
160 // create JSON array on stack:
161 lua_newtable(L);
162 // set metatable of JSON array:
163 lua_pushvalue(L, json_import_arraymt_idx);
164 lua_setmetatable(L, -2);
165 // create internal shadow table on stack:
166 lua_newtable(L);
167 // register internal shadow table (and cleanup stack afterwards):
168 lua_pushvalue(L, -2);
169 lua_pushvalue(L, -2);
170 lua_rawset(L, json_import_shadowtbl_idx);
171 // add nil as key (needed to keep stack balance) and as magic to detect arrays:
172 lua_pushnil(L);
173 // expect array value (or end of array) and continue with loop:
174 mode = JSON_STATE_ARRAY_VALUE;
175 // continue with common code for opening JSON object and JSON array:
176 // commn code for opening JSON object or JSON array:
177 json_import_open:
178 // limit nested levels:
179 if (level >= JSON_MAXDEPTH) {
180 lua_pushnil(L);
181 lua_pushliteral(L, "Too many nested JSON levels");
182 return 2;
183 }
184 // additional buffer overflow protection:
185 if (!lua_checkstack(L, LUA_MINSTACK))
186 return luaL_error(L, "Caught stack overflow in JSON import function (too many nested levels and stack size too small)");
187 // increment level:
188 level++;
189 // consume input character:
190 pos++;
191 goto json_import_loop;
192 // end of JSON object:
193 case '}':
194 // if end of JSON object is not expected here, then return an error:
195 if (mode != JSON_STATE_OBJECT_KEY && mode != JSON_STATE_OBJECT_SEPARATOR)
196 goto json_import_syntax_error;
197 // jump to common code for end of JSON object and JSON array:
198 goto json_import_close;
199 // end of JSON array:
200 case ']':
201 // if end of JSON array is not expected here, then return an error:
202 if (mode != JSON_STATE_ARRAY_VALUE && mode != JSON_STATE_ARRAY_SEPARATOR)
203 goto json_import_syntax_error;
204 // pop nil key/magic:
205 lua_pop(L, 1);
206 // continue with common code for end of JSON object and JSON array:
207 // common code for end of JSON object or JSON array:
208 json_import_close:
209 // consume input character:
210 pos++;
211 // pop shadow table:
212 lua_pop(L, 1);
213 // check if nested:
214 if (--level) {
215 // if nested, then check if outer(!) structure is an array or object:
216 if (lua_isnil(L, -2)) {
217 // select array value processing:
218 mode = JSON_STATE_ARRAY_VALUE;
219 } else {
220 // select object value processing:
221 mode = JSON_STATE_OBJECT_VALUE;
222 }
223 // store value in outer structure:
224 goto json_import_process_value;
225 }
226 // if not nested, then expect end of JSON document and continue with loop:
227 mode = JSON_STATE_END;
228 goto json_import_loop;
229 // key terminator:
230 case ':':
231 // if key terminator is not expected here, then return an error:
232 if (mode != JSON_STATE_OBJECT_KEY_TERMINATOR)
233 goto json_import_syntax_error;
234 // consume input character:
235 pos++;
236 // set state of parser and continue with loop:
237 mode = JSON_STATE_OBJECT_VALUE;
238 goto json_import_loop;
239 // value terminator (NOTE: trailing comma at end of value or key-value list is tolerated by this parser)
240 case ',':
241 // change parser state accordingly:
242 if (mode == JSON_STATE_OBJECT_SEPARATOR) {
243 mode = JSON_STATE_OBJECT_KEY;
244 } else if (mode == JSON_STATE_ARRAY_SEPARATOR) {
245 mode = JSON_STATE_ARRAY_VALUE;
246 } else {
247 // if value terminator is not expected here, then return an error:
248 goto json_import_syntax_error;
249 }
250 // consume input character:
251 pos++;
252 // continue with loop:
253 goto json_import_loop;
254 // string literal:
255 case '"':
256 // prepare buffer to decode string (with maximum possible length) and set write position to zero:
257 cbuf = luaL_buffinitsize(L, &luabuf, total-pos);
258 writepos = 0;
259 // consume quote character:
260 pos++;
261 // read next character until encountering end quote:
262 while ((c = str[pos++]) != '"') {
263 if (c == 0) {
264 // handle unexpected end-of-string:
265 goto json_import_unexpected_eof;
266 } else if (c < 32 || c == 127) {
267 // do not allow ASCII control characters:
268 // NOTE: illegal UTF-8 sequences and extended control characters are not sanitized
269 // by this parser to allow different encodings than Unicode
270 lua_pushnil(L);
271 lua_pushliteral(L, "Unexpected control character in JSON string");
272 return 2;
273 } else if (c == '\\') {
274 // read next char after backslash escape:
275 c = str[pos++];
276 switch (c) {
277 // unexpected end-of-string:
278 case 0:
279 goto json_import_unexpected_eof;
280 // unescaping of quotation mark, slash, and backslash:
281 case '"':
282 case '/':
283 case '\\':
284 cbuf[writepos++] = c;
285 break;
286 // unescaping of backspace:
287 case 'b':
288 cbuf[writepos++] = '\b';
289 break;
290 // unescaping of form-feed:
291 case 'f':
292 cbuf[writepos++] = '\f';
293 break;
294 // unescaping of new-line:
295 case 'n':
296 cbuf[writepos++] = '\n';
297 break;
298 // unescaping of carriage-return:
299 case 'r':
300 cbuf[writepos++] = '\r';
301 break;
302 // unescaping of tabulator:
303 case 't':
304 cbuf[writepos++] = '\t';
305 break;
306 // unescaping of UTF-16 characters
307 case 'u':
308 lua_pushnil(L);
309 lua_pushliteral(L, "JSON unicode escape sequences are not implemented yet"); // TODO
310 return 2;
311 // unexpected escape sequence:
312 default:
313 lua_pushnil(L);
314 lua_pushliteral(L, "Unexpected string escape sequence in JSON document");
315 return 2;
316 }
317 } else {
318 // normal character:
319 cbuf[writepos++] = c;
320 }
321 }
322 // process buffer to Lua string:
323 luaL_pushresultsize(&luabuf, writepos);
324 // continue with processing of decoded string:
325 goto json_import_process_value;
326 }
327 // process values whose type is is not deducible from a single character:
328 if ((c >= '0' && c <= '9') || c == '-' || c == '+') {
329 // numbers:
330 char *endptr;
331 double numval;
332 numval = strtod(str+pos, &endptr);
333 if (endptr == str+pos) goto json_import_syntax_error;
334 pos += endptr - (str+pos);
335 lua_pushnumber(L, numval);
336 } else if (!strncmp(str+pos, "true", 4)) {
337 // consume 4 input characters for "true":
338 pos += 4;
339 // put Lua true value on stack:
340 lua_pushboolean(L, 1);
341 } else if (!strncmp(str+pos, "false", 5)) {
342 // consume 5 input characters for "false":
343 pos += 5;
344 // put Lua false value on stack:
345 lua_pushboolean(L, 0);
346 } else if (!strncmp(str+pos, "null", 4)) {
347 // consume 4 input characters for "null":
348 pos += 4;
349 // put special null-marker on stack:
350 lua_pushvalue(L, json_import_nullmark_idx);
351 } else {
352 // all other cases are a syntax error:
353 goto json_import_syntax_error;
354 }
355 // process a decoded value or key value pair (expected on top of Lua stack):
356 json_import_process_value:
357 switch (mode) {
358 // an object key has been read:
359 case JSON_STATE_OBJECT_KEY:
360 // if an object key is not a string, then this is a syntax error:
361 if (lua_type(L, -1) != LUA_TSTRING) goto json_import_syntax_error;
362 // expect key terminator and continue with loop:
363 mode = JSON_STATE_OBJECT_KEY_TERMINATOR;
364 goto json_import_loop;
365 // a key value pair has been read:
366 case JSON_STATE_OBJECT_VALUE:
367 // store key value pair in outer shadow table:
368 lua_rawset(L, -3);
369 // expect value terminator (or end of object) and continue with loop:
370 mode = JSON_STATE_OBJECT_SEPARATOR;
371 goto json_import_loop;
372 // an array value has been read:
373 case JSON_STATE_ARRAY_VALUE:
374 // store value in outer shadow table:
375 lua_rawseti(L, -3, lua_rawlen(L, -3) + 1);
376 // expect value terminator (or end of object) and continue with loop:
377 mode = JSON_STATE_ARRAY_SEPARATOR;
378 goto json_import_loop;
379 // a single value has been read:
380 case JSON_STATE_VALUE:
381 // leave value on top of stack, expect end of JSON document, and continue with loop:
382 mode = JSON_STATE_END;
383 goto json_import_loop;
384 }
385 // syntax error handling (only reachable by goto statement):
386 json_import_syntax_error:
387 lua_pushnil(L);
388 lua_pushliteral(L, "Syntax error in JSON document");
389 return 2;
390 }
392 #define json_path_shadowtbl_idx 1
393 #define json_path_nullmark_idx 2
394 #define json_path_idxshift 2
396 // gets a value or its type from a JSON document (first argument)
397 // optionally using a path (variable number of keys after first argument):
398 static int json_path(lua_State *L, int type_mode) {
399 int stacktop;
400 int idx = 2 + json_path_idxshift;
401 // insert json_shadowtbl on stack at position 1:
402 json_regfetch(L, shadowtbl);
403 lua_insert(L, 1);
404 // insert json_nullmark on stack at position 2:
405 json_regfetch(L, nullmark);
406 lua_insert(L, 2);
407 // store number of arguments:
408 stacktop = lua_gettop(L);
409 // follow path, starting with first argument as "current value":
410 lua_pushvalue(L, 1 + json_path_idxshift);
411 // process each "path key":
412 while (idx <= stacktop) {
413 // if "current value" is nil, then the path cannot be walked and nil is returned:
414 if (lua_isnil(L, -1)) return 1;
415 // try to get shadow table of "current value":
416 lua_pushvalue(L, -1);
417 lua_rawget(L, json_path_shadowtbl_idx);
418 if (lua_isnil(L, -1)) {
419 // if no shadow table is found,
420 // drop nil from stack:
421 lua_pop(L, 1);
422 if (lua_type(L, -1) == LUA_TTABLE) {
423 // if "current value" is a table,
424 // get "next value" using the "path key":
425 lua_pushvalue(L, idx++);
426 lua_gettable(L, -2);
427 } else {
428 // if "current value" is not a table,
429 // then the path cannot be walked and nil is returned:
430 lua_pushnil(L);
431 return 1;
432 }
433 } else {
434 // if a shadow table is found,
435 // set "current value" to its shadow table:
436 lua_replace(L, -2);
437 // get "next value" using the "path key":
438 lua_pushvalue(L, idx++);
439 lua_rawget(L, -2);
440 }
441 // the "next value" replaces the "current value":
442 lua_replace(L, -2);
443 }
444 if (!type_mode) {
445 // if a value (and not its type) was requested,
446 // check if value is the null-marker, and store nil on top of Lua stack in that case:
447 if (lua_rawequal(L, -1, json_path_nullmark_idx)) lua_pushnil(L);
448 } else {
449 // if the type was requested,
450 // check if value is the null-marker:
451 if (lua_rawequal(L, -1, json_path_nullmark_idx)) {
452 // if yes, store string "null" on top of Lua stack:
453 lua_pushliteral(L, "null");
454 } else {
455 // otherwise,
456 // check if metatable indicates "object" or "array":
457 if (lua_getmetatable(L, -1)) {
458 json_regfetch(L, objectmt);
459 if (lua_rawequal(L, -2, -1)) {
460 // return string "object":
461 lua_pushliteral(L, "object");
462 return 1;
463 }
464 json_regfetch(L, arraymt);
465 if (lua_rawequal(L, -3, -1)) {
466 // return string "array":
467 lua_pushliteral(L, "array");
468 return 1;
469 }
470 lua_pop(L, 3);
471 }
472 // otherwise, get the Lua type:
473 lua_pushstring(L, lua_typename(L, lua_type(L, -1)));
474 }
475 }
476 // return the top most value on the Lua stack:
477 return 1;
478 }
480 // gets a value from a JSON document (first argument)
481 // optionally using a path (variable number of keys after first argument):
482 static int json_get(lua_State *L) {
483 return json_path(L, 0);
484 }
486 // gets a value's type from a JSON document (first argument)
487 // optionally using a path (variable number of keys after first argument):
488 static int json_type(lua_State *L) {
489 return json_path(L, 1);
490 }
492 // checks if a value in a JSON document (first argument) is null:
493 static int json_isnull(lua_State *L) {
494 const char *jsontype;
495 lua_pushcfunction(L, json_type);
496 lua_insert(L, 1);
497 lua_call(L, lua_gettop(L) - 1, 1);
498 jsontype = lua_tostring(L, -1);
499 if (jsontype && !strcmp(jsontype, "null")) lua_pushboolean(L, 1);
500 else lua_pushboolean(L, 0);
501 return 1;
502 }
504 #define json_setnull_unknownmt_idx 3
505 #define json_setnull_objectmt_idx 4
506 #define json_setnull_arraymt_idx 5
507 #define json_setnull_shadowtbl_idx 6
509 static int json_setnull(lua_State *L) {
510 // truncate stack to two elements:
511 lua_settop(L, 2);
512 // push json_unknownmt to stack position 3:
513 json_regfetch(L, unknownmt);
514 // push json_objectmt to stack position 4:
515 json_regfetch(L, objectmt);
516 // push json_arraymt to stack position 5:
517 json_regfetch(L, arraymt);
518 // push json_shadowtbl to stack position 6:
519 json_regfetch(L, shadowtbl);
520 //
521 lua_getmetatable(L, 1);
522 if (
523 !lua_rawequal(L, -1, json_setnull_unknownmt_idx) &&
524 !lua_rawequal(L, -1, json_setnull_objectmt_idx) &&
525 !lua_rawequal(L, -1, json_setnull_arraymt_idx)
526 ) {
527 lua_pushvalue(L, json_setnull_unknownmt_idx);
528 lua_setmetatable(L, 1);
529 }
530 lua_pushvalue(L, 1);
531 lua_rawget(L, json_setnull_shadowtbl_idx);
532 if (lua_isnil(L, -1)) {
533 lua_newtable(L);
534 lua_pushvalue(L, 1);
535 lua_pushvalue(L, -2);
536 lua_rawset(L, json_setnull_shadowtbl_idx);
537 }
538 lua_pushvalue(L, 2);
539 json_regfetch(L, nullmark);
540 lua_rawset(L, -3);
541 return 0;
542 }
544 static int json_len(lua_State *L) {
545 lua_settop(L, 1);
546 json_regfetch(L, shadowtbl);
547 lua_pushvalue(L, 1);
548 lua_rawget(L, -2);
549 lua_pushinteger(L, lua_rawlen(L, lua_isnil(L, -1) ? 1 : -1));
550 return 1;
551 }
553 #define json_index_nullmark_idx 3
554 #define json_index_shadowtbl_idx 4
556 static int json_index(lua_State *L) {
557 lua_settop(L, 2);
558 json_regfetch(L, nullmark); // on stack position 3
559 json_regfetch(L, shadowtbl);
560 lua_pushvalue(L, 1);
561 lua_rawget(L, json_index_shadowtbl_idx);
562 if (lua_isnil(L, -1)) return luaL_error(L, "Shadow table not found");
563 lua_pushvalue(L, 2);
564 lua_rawget(L, -2);
565 if (lua_rawequal(L, -1, json_index_nullmark_idx)) lua_pushnil(L);
566 return 1;
567 }
569 static int json_newindex(lua_State *L) {
570 lua_settop(L, 3);
571 json_regfetch(L, shadowtbl);
572 lua_pushvalue(L, 1);
573 lua_rawget(L, -2);
574 if (lua_isnil(L, -1)) return luaL_error(L, "Shadow table not found");
575 lua_replace(L, 1);
576 lua_settop(L, 3);
577 lua_rawset(L, 1);
578 return 1;
579 }
581 #define json_pairs_iterfunc_nullmark_idx 3
582 #define json_pairs_iterfunc_shadowtbl_idx 4
584 static int json_pairs_iterfunc(lua_State *L) {
585 lua_settop(L, 2);
586 json_regfetch(L, nullmark); // on stack position 3
587 json_regfetch(L, shadowtbl);
588 lua_pushvalue(L, 1);
589 lua_rawget(L, json_pairs_iterfunc_shadowtbl_idx);
590 if (lua_isnil(L, -1)) return luaL_error(L, "Shadow table not found");
591 lua_pushvalue(L, 2);
592 if (!lua_next(L, -2)) return 0;
593 if (lua_rawequal(L, -1, json_pairs_iterfunc_nullmark_idx)) {
594 lua_pop(L, 1);
595 lua_pushnil(L);
596 }
597 return 2;
598 }
600 static int json_pairs(lua_State *L) {
601 lua_pushcfunction(L, json_pairs_iterfunc);
602 lua_pushvalue(L, 1);
603 lua_pushnil(L);
604 return 3;
605 }
607 #define json_ipairs_iterfunc_nullmark_idx 3
608 #define json_ipairs_iterfunc_shadowtbl_idx 4
610 static int json_ipairs_iterfunc(lua_State *L) {
611 int idx;
612 lua_settop(L, 2);
613 json_regfetch(L, nullmark); // on stack position 3
614 json_regfetch(L, shadowtbl);
615 idx = lua_tointeger(L, 2) + 1;
616 lua_pushvalue(L, 1);
617 lua_rawget(L, json_ipairs_iterfunc_shadowtbl_idx);
618 if (lua_isnil(L, -1)) return luaL_error(L, "Shadow table not found");
619 lua_rawgeti(L, -1, idx);
620 if (lua_isnil(L, -1)) return 0;
621 lua_pushinteger(L, idx);
622 if (lua_rawequal(L, -2, json_ipairs_iterfunc_nullmark_idx)) lua_pushnil(L);
623 else lua_pushvalue(L, -2);
624 return 2;
625 }
627 static int json_ipairs(lua_State *L) {
628 lua_pushcfunction(L, json_ipairs_iterfunc);
629 lua_pushvalue(L, 1);
630 lua_pushinteger(L, 0);
631 return 3;
632 }
634 static const struct luaL_Reg json_module_functions[] = {
635 {"object", json_object},
636 {"array", json_array},
637 {"import", json_import},
638 {"get", json_get},
639 {"type", json_type},
640 {"isnull", json_isnull},
641 {"setnull", json_setnull},
642 {NULL, NULL}
643 };
645 static const struct luaL_Reg json_metatable_functions[] = {
646 {"__len", json_len},
647 {"__index", json_index},
648 {"__newindex", json_newindex},
649 {"__pairs", json_pairs},
650 {"__ipairs", json_ipairs},
651 {NULL, NULL}
652 };
654 int luaopen_json(lua_State *L) {
655 lua_settop(L, 0);
656 lua_newtable(L); // library
657 lua_newtable(L);
658 luaL_setfuncs(L, json_metatable_functions, 0);
659 json_regstore(L, unknownmt);
660 lua_setfield(L, 1, "ambiguous_mt");
661 lua_newtable(L);
662 luaL_setfuncs(L, json_metatable_functions, 0);
663 json_regstore(L, objectmt);
664 lua_setfield(L, 1, "object_mt");
665 lua_newtable(L);
666 luaL_setfuncs(L, json_metatable_functions, 0);
667 json_regstore(L, arraymt);
668 lua_setfield(L, 1, "array_mt");
669 lua_newtable(L); // ephemeron table to store shadow tables for each JSON object/array to allow NULL values returned as nil
670 lua_newtable(L); // metatable for ephemeron table
671 lua_pushliteral(L, "__mode");
672 lua_pushliteral(L, "k");
673 lua_rawset(L, -3);
674 lua_setmetatable(L, -2);
675 json_regstore(L, shadowtbl);
676 lua_newtable(L);
677 json_regstore(L, nullmark);
678 lua_settop(L, 1);
679 luaL_setfuncs(L, json_module_functions, 0);
680 return 1;
681 }

Impressum / About Us