# HG changeset patch # User jbe # Date 1407691105 -7200 # Node ID 433fa98329ecc71ed1ec9005202eeb8ea147e94e # Parent 00fee67a0c2734cb878459ee7f0bd7fae990cbef Bugfix in json.export(...); Better argument checking for json.export(...) diff -r 00fee67a0c27 -r 433fa98329ec libraries/json/json.c --- a/libraries/json/json.c Sun Aug 10 19:06:18 2014 +0200 +++ b/libraries/json/json.c Sun Aug 10 19:18:25 2014 +0200 @@ -1037,16 +1037,27 @@ json_container_t *container = NULL; // pointer to current C struct for container information // stack shall contain two function arguments: lua_settop(L, 2); - // use default indentation if indentation argument is (boolean) true: - if ( - lua_isboolean(L, json_export_indentstring_idx) && - lua_toboolean(L, json_export_indentstring_idx) - ) { - lua_pushliteral(L, " "); - lua_replace(L, json_export_indentstring_idx); + // check if pretty printing (with indentation) is desired: + if (lua_toboolean(L, json_export_indentstring_idx)) { + // if yes, + // set pretty variable to 1: + pretty = 1; + // check if second argument is a boolean (true): + if (lua_isboolean(L, json_export_indentstring_idx)) { + // if yes, + // use default indentation if indentation argument is boolean true: + lua_pushliteral(L, " "); + lua_replace(L, json_export_indentstring_idx); + } else { + // if no, + // require second argument to be a string: + luaL_checktype(L, json_export_indentstring_idx, LUA_TSTRING); + } + } else { + // if no, + // set pretty variable to 0: + pretty = 0; } - // set pretty variable to 1 if pretty printing (with indentation) is desired: - pretty = lua_toboolean(L, json_export_indentstring_idx); // push objectmt onto stack position 3: json_regfetch(L, objectmt); // push arraymt onto stack position 4: @@ -1118,12 +1129,9 @@ break; // value to encode is of type table (this includes JSON objects and JSON arrays): case LUA_TTABLE: - // use value as container: - lua_pushvalue(L, json_export_value_idx); - lua_replace(L, json_export_luacontainer_idx); // use table's metatable to try to determine type of table: tabletype = JSON_TABLETYPE_UNKNOWN; - if (lua_getmetatable(L, json_export_luacontainer_idx)) { + if (lua_getmetatable(L, json_export_value_idx)) { if (lua_rawequal(L, -1, json_export_objectmt_idx)) { tabletype = JSON_TABLETYPE_OBJECT; } else { @@ -1137,10 +1145,10 @@ lua_pop(L, 1); } // replace table with its shadow table if existent, and reset stack: - lua_pushvalue(L, json_export_luacontainer_idx); + lua_pushvalue(L, json_export_value_idx); lua_rawget(L, json_export_shadowtbl_idx); if (lua_isnil(L, -1)) lua_pop(L, 1); - else lua_replace(L, json_export_luacontainer_idx); + else lua_replace(L, json_export_value_idx); // check if type of table is still undetermined // and optionally calculate number of string keys (keycount) // or set keycount to zero: @@ -1148,7 +1156,7 @@ if (tabletype == JSON_TABLETYPE_UNKNOWN) { // if type of table is undetermined, // iterate over all keys: - for (lua_pushnil(L); lua_next(L, json_export_luacontainer_idx); lua_pop(L, 1)) { + for (lua_pushnil(L); lua_next(L, json_export_value_idx); lua_pop(L, 1)) { switch (lua_type(L, -2)) { case LUA_TSTRING: // for string keys, @@ -1183,6 +1191,9 @@ lua_pushvalue(L, json_export_ccontainer_idx); lua_rawseti(L, json_export_stackswap_idx, ++stackswapidx); } + // use value as current container: + lua_pushvalue(L, json_export_value_idx); + lua_replace(L, json_export_luacontainer_idx); // distinguish between JSON objects and JSON arrays: switch (tabletype) { // JSON object: