webmcp

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

Impressum / About Us