webmcp
changeset 188:433fa98329ec
Bugfix in json.export(...); Better argument checking for json.export(...)
author | jbe |
---|---|
date | Sun Aug 10 19:18:25 2014 +0200 (2014-08-10) |
parents | 00fee67a0c27 |
children | 44344b4a52c1 |
files | libraries/json/json.c |
line diff
1.1 --- a/libraries/json/json.c Sun Aug 10 19:06:18 2014 +0200 1.2 +++ b/libraries/json/json.c Sun Aug 10 19:18:25 2014 +0200 1.3 @@ -1037,16 +1037,27 @@ 1.4 json_container_t *container = NULL; // pointer to current C struct for container information 1.5 // stack shall contain two function arguments: 1.6 lua_settop(L, 2); 1.7 - // use default indentation if indentation argument is (boolean) true: 1.8 - if ( 1.9 - lua_isboolean(L, json_export_indentstring_idx) && 1.10 - lua_toboolean(L, json_export_indentstring_idx) 1.11 - ) { 1.12 - lua_pushliteral(L, " "); 1.13 - lua_replace(L, json_export_indentstring_idx); 1.14 + // check if pretty printing (with indentation) is desired: 1.15 + if (lua_toboolean(L, json_export_indentstring_idx)) { 1.16 + // if yes, 1.17 + // set pretty variable to 1: 1.18 + pretty = 1; 1.19 + // check if second argument is a boolean (true): 1.20 + if (lua_isboolean(L, json_export_indentstring_idx)) { 1.21 + // if yes, 1.22 + // use default indentation if indentation argument is boolean true: 1.23 + lua_pushliteral(L, " "); 1.24 + lua_replace(L, json_export_indentstring_idx); 1.25 + } else { 1.26 + // if no, 1.27 + // require second argument to be a string: 1.28 + luaL_checktype(L, json_export_indentstring_idx, LUA_TSTRING); 1.29 + } 1.30 + } else { 1.31 + // if no, 1.32 + // set pretty variable to 0: 1.33 + pretty = 0; 1.34 } 1.35 - // set pretty variable to 1 if pretty printing (with indentation) is desired: 1.36 - pretty = lua_toboolean(L, json_export_indentstring_idx); 1.37 // push objectmt onto stack position 3: 1.38 json_regfetch(L, objectmt); 1.39 // push arraymt onto stack position 4: 1.40 @@ -1118,12 +1129,9 @@ 1.41 break; 1.42 // value to encode is of type table (this includes JSON objects and JSON arrays): 1.43 case LUA_TTABLE: 1.44 - // use value as container: 1.45 - lua_pushvalue(L, json_export_value_idx); 1.46 - lua_replace(L, json_export_luacontainer_idx); 1.47 // use table's metatable to try to determine type of table: 1.48 tabletype = JSON_TABLETYPE_UNKNOWN; 1.49 - if (lua_getmetatable(L, json_export_luacontainer_idx)) { 1.50 + if (lua_getmetatable(L, json_export_value_idx)) { 1.51 if (lua_rawequal(L, -1, json_export_objectmt_idx)) { 1.52 tabletype = JSON_TABLETYPE_OBJECT; 1.53 } else { 1.54 @@ -1137,10 +1145,10 @@ 1.55 lua_pop(L, 1); 1.56 } 1.57 // replace table with its shadow table if existent, and reset stack: 1.58 - lua_pushvalue(L, json_export_luacontainer_idx); 1.59 + lua_pushvalue(L, json_export_value_idx); 1.60 lua_rawget(L, json_export_shadowtbl_idx); 1.61 if (lua_isnil(L, -1)) lua_pop(L, 1); 1.62 - else lua_replace(L, json_export_luacontainer_idx); 1.63 + else lua_replace(L, json_export_value_idx); 1.64 // check if type of table is still undetermined 1.65 // and optionally calculate number of string keys (keycount) 1.66 // or set keycount to zero: 1.67 @@ -1148,7 +1156,7 @@ 1.68 if (tabletype == JSON_TABLETYPE_UNKNOWN) { 1.69 // if type of table is undetermined, 1.70 // iterate over all keys: 1.71 - for (lua_pushnil(L); lua_next(L, json_export_luacontainer_idx); lua_pop(L, 1)) { 1.72 + for (lua_pushnil(L); lua_next(L, json_export_value_idx); lua_pop(L, 1)) { 1.73 switch (lua_type(L, -2)) { 1.74 case LUA_TSTRING: 1.75 // for string keys, 1.76 @@ -1183,6 +1191,9 @@ 1.77 lua_pushvalue(L, json_export_ccontainer_idx); 1.78 lua_rawseti(L, json_export_stackswap_idx, ++stackswapidx); 1.79 } 1.80 + // use value as current container: 1.81 + lua_pushvalue(L, json_export_value_idx); 1.82 + lua_replace(L, json_export_luacontainer_idx); 1.83 // distinguish between JSON objects and JSON arrays: 1.84 switch (tabletype) { 1.85 // JSON object: