webmcp

annotate libraries/json/json.c @ 164:8e969519f7c7

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

Impressum / About Us