# HG changeset patch # User jbe # Date 1407687368 -7200 # Node ID 26132a017925f6d9b2616f5125425416bf5ee60b # Parent 1f925cb34299e2f0c8bf780c0589a608539c8461 Work on json.export(...) function diff -r 1f925cb34299 -r 26132a017925 libraries/json/json.c --- a/libraries/json/json.c Sat Aug 09 20:07:12 2014 +0200 +++ b/libraries/json/json.c Sun Aug 10 18:16:08 2014 +0200 @@ -1028,6 +1028,7 @@ // NOTE: 7 bytes due to backslash, character 'u', 4 hex digits, and terminating NULL byte int tabletype; // table type: unknown, JSON object, or JSON array size_t keycount = 0; // number of string keys in object + json_key_t *key; // pointer to C structure containing a string key int level = 0; // current depth level int i; // iteration variable for level dependent repetitions int stackswapidx = 0; // elements in stack swap table @@ -1237,71 +1238,63 @@ return luaL_error(L, "JSON export not possible for values of type \"%s\"", lua_typename(L, lua_type(L, json_export_value_idx))); } if (container) { - // add colon or comma where necessary: - if (containerkey) luaL_addchar(&buf, ':'); json_export_container: switch (container->type) { case JSON_TABLETYPE_OBJECT: - if (container->pos < container->count) { - json_key_t *key; - key = &container->keys[container->pos]; - lua_pushlstring(L, key->data, key->length); - if (!containerkey) { - containerkey = 1; - } else { - lua_rawget(L, json_export_luacontainer_idx); - containerkey = 0; - container->pos++; - } - lua_replace(L, json_export_value_idx); + if (container->pos == container->count) goto json_export_close; + key = &container->keys[container->pos]; + lua_pushlstring(L, key->data, key->length); + if (!containerkey) { + if (container->pos) luaL_addchar(&buf, ','); + containerkey = 1; } else { - luaL_addchar(&buf, '}'); - goto json_export_close; + luaL_addchar(&buf, ':'); + if (pretty) luaL_addchar(&buf, ' '); + lua_rawget(L, json_export_luacontainer_idx); + containerkey = 0; + container->pos++; } + lua_replace(L, json_export_value_idx); break; case JSON_TABLETYPE_ARRAY: - lua_rawgeti(L, json_export_luacontainer_idx, ++container->pos); + lua_rawgeti(L, json_export_luacontainer_idx, container->pos+1); lua_replace(L, json_export_value_idx); - if (lua_isnil(L, json_export_value_idx)) { - luaL_addchar(&buf, ']'); - goto json_export_close; - } + if (lua_isnil(L, json_export_value_idx)) goto json_export_close; + if (container->pos) luaL_addchar(&buf, ','); + container->pos++; break; json_export_close: - if (--level) { - lua_rawgeti(L, json_export_stackswap_idx, stackswapidx--); - lua_replace(L, json_export_ccontainer_idx); - container = lua_touserdata(L, json_export_ccontainer_idx); - lua_rawgeti(L, json_export_stackswap_idx, stackswapidx--); - lua_replace(L, json_export_luacontainer_idx); - goto json_export_container; - } else { - // for pretty results, add final newline character if outermost container is processed: - if (pretty) luaL_addchar(&buf, '\n'); - luaL_pushresult(&buf); - return 1; + level--; + if (pretty) { + if (container->pos) { + luaL_addchar(&buf, '\n'); + for (i=0; itype == JSON_TABLETYPE_OBJECT ? '}' : ']'); + if (!level) goto json_export_finish; + lua_rawgeti(L, json_export_stackswap_idx, stackswapidx--); + lua_replace(L, json_export_ccontainer_idx); + container = lua_touserdata(L, json_export_ccontainer_idx); + lua_rawgeti(L, json_export_stackswap_idx, stackswapidx--); + lua_replace(L, json_export_luacontainer_idx); + goto json_export_container; } - if ( - container->type == JSON_TABLETYPE_OBJECT ? ( - container->pos && containerkey - ) : ( - container->pos > 1 - ) - ) luaL_addchar(&buf, ','); // handle indentation for pretty results: - if (pretty) { - if (containerkey) { - luaL_addchar(&buf, ' '); - } else { - luaL_addchar(&buf, '\n'); - for (i=0; itype == JSON_TABLETYPE_ARRAY)) { + luaL_addchar(&buf, '\n'); + for (i=0; i