webmcp

changeset 185:26132a017925

Work on json.export(...) function
author jbe
date Sun Aug 10 18:16:08 2014 +0200 (2014-08-10)
parents 1f925cb34299
children fbfdd4f979d5
files libraries/json/json.c
line diff
     1.1 --- a/libraries/json/json.c	Sat Aug 09 20:07:12 2014 +0200
     1.2 +++ b/libraries/json/json.c	Sun Aug 10 18:16:08 2014 +0200
     1.3 @@ -1028,6 +1028,7 @@
     1.4                          // NOTE: 7 bytes due to backslash, character 'u', 4 hex digits, and terminating NULL byte
     1.5    int tabletype;        // table type: unknown, JSON object, or JSON array
     1.6    size_t keycount = 0;  // number of string keys in object
     1.7 +  json_key_t *key;      // pointer to C structure containing a string key
     1.8    int level = 0;        // current depth level
     1.9    int i;                // iteration variable for level dependent repetitions
    1.10    int stackswapidx = 0; // elements in stack swap table
    1.11 @@ -1237,71 +1238,63 @@
    1.12      return luaL_error(L, "JSON export not possible for values of type \"%s\"", lua_typename(L, lua_type(L, json_export_value_idx)));
    1.13      }
    1.14      if (container) {
    1.15 -      // add colon or comma where necessary:
    1.16 -      if (containerkey) luaL_addchar(&buf, ':');
    1.17        json_export_container:
    1.18        switch (container->type) {
    1.19        case JSON_TABLETYPE_OBJECT:
    1.20 -        if (container->pos < container->count) {
    1.21 -          json_key_t *key;
    1.22 -          key = &container->keys[container->pos];
    1.23 -          lua_pushlstring(L, key->data, key->length);
    1.24 -          if (!containerkey) {
    1.25 -            containerkey = 1;
    1.26 -          } else {
    1.27 -            lua_rawget(L, json_export_luacontainer_idx);
    1.28 -            containerkey = 0;
    1.29 -            container->pos++;
    1.30 -          }
    1.31 -          lua_replace(L, json_export_value_idx);
    1.32 +        if (container->pos == container->count) goto json_export_close;
    1.33 +        key = &container->keys[container->pos];
    1.34 +        lua_pushlstring(L, key->data, key->length);
    1.35 +        if (!containerkey) {
    1.36 +          if (container->pos) luaL_addchar(&buf, ',');
    1.37 +          containerkey = 1;
    1.38          } else {
    1.39 -          luaL_addchar(&buf, '}');
    1.40 -          goto json_export_close;
    1.41 +          luaL_addchar(&buf, ':');
    1.42 +          if (pretty) luaL_addchar(&buf, ' ');
    1.43 +          lua_rawget(L, json_export_luacontainer_idx);
    1.44 +          containerkey = 0;
    1.45 +          container->pos++;
    1.46          }
    1.47 +        lua_replace(L, json_export_value_idx);
    1.48          break;
    1.49        case JSON_TABLETYPE_ARRAY:
    1.50 -        lua_rawgeti(L, json_export_luacontainer_idx, ++container->pos);
    1.51 +        lua_rawgeti(L, json_export_luacontainer_idx, container->pos+1);
    1.52          lua_replace(L, json_export_value_idx);
    1.53 -        if (lua_isnil(L, json_export_value_idx)) {
    1.54 -          luaL_addchar(&buf, ']');
    1.55 -          goto json_export_close;
    1.56 -        }
    1.57 +        if (lua_isnil(L, json_export_value_idx)) goto json_export_close;
    1.58 +        if (container->pos) luaL_addchar(&buf, ',');
    1.59 +        container->pos++;
    1.60          break;
    1.61        json_export_close:
    1.62 -        if (--level) {
    1.63 -          lua_rawgeti(L, json_export_stackswap_idx, stackswapidx--);
    1.64 -          lua_replace(L, json_export_ccontainer_idx);
    1.65 -          container = lua_touserdata(L, json_export_ccontainer_idx);
    1.66 -          lua_rawgeti(L, json_export_stackswap_idx, stackswapidx--);
    1.67 -          lua_replace(L, json_export_luacontainer_idx);
    1.68 -          goto json_export_container;
    1.69 -        } else {
    1.70 -          // for pretty results, add final newline character if outermost container is processed:
    1.71 -          if (pretty) luaL_addchar(&buf, '\n');
    1.72 -          luaL_pushresult(&buf);
    1.73 -          return 1;
    1.74 +        level--;
    1.75 +        if (pretty) {
    1.76 +          if (container->pos) {
    1.77 +            luaL_addchar(&buf, '\n');
    1.78 +            for (i=0; i<level; i++) {
    1.79 +              lua_pushvalue(L, json_export_indentstring_idx);
    1.80 +              luaL_addvalue(&buf);
    1.81 +            }
    1.82 +          }
    1.83          }
    1.84 +        luaL_addchar(&buf, container->type == JSON_TABLETYPE_OBJECT ? '}' : ']');
    1.85 +        if (!level) goto json_export_finish;
    1.86 +        lua_rawgeti(L, json_export_stackswap_idx, stackswapidx--);
    1.87 +        lua_replace(L, json_export_ccontainer_idx);
    1.88 +        container = lua_touserdata(L, json_export_ccontainer_idx);
    1.89 +        lua_rawgeti(L, json_export_stackswap_idx, stackswapidx--);
    1.90 +        lua_replace(L, json_export_luacontainer_idx);
    1.91 +        goto json_export_container;
    1.92        }
    1.93 -      if (
    1.94 -        container->type == JSON_TABLETYPE_OBJECT ? (
    1.95 -          container->pos && containerkey
    1.96 -        ) : (
    1.97 -          container->pos > 1
    1.98 -        )
    1.99 -      ) luaL_addchar(&buf, ',');
   1.100        // handle indentation for pretty results:
   1.101 -      if (pretty) {
   1.102 -        if (containerkey) {
   1.103 -          luaL_addchar(&buf, ' ');
   1.104 -        } else {
   1.105 -          luaL_addchar(&buf, '\n');
   1.106 -          for (i=0; i<level; i++) {
   1.107 -            lua_pushvalue(L, json_export_indentstring_idx);
   1.108 -            luaL_addvalue(&buf);
   1.109 -          }
   1.110 +      if (pretty && (containerkey || container->type == JSON_TABLETYPE_ARRAY)) {
   1.111 +        luaL_addchar(&buf, '\n');
   1.112 +        for (i=0; i<level; i++) {
   1.113 +          lua_pushvalue(L, json_export_indentstring_idx);
   1.114 +          luaL_addvalue(&buf);
   1.115          }
   1.116        }
   1.117      } else {
   1.118 +      json_export_finish:
   1.119 +      // for pretty results, add final newline character if outermost container is processed:
   1.120 +      if (pretty) luaL_addchar(&buf, '\n');
   1.121        luaL_pushresult(&buf);
   1.122        return 1;
   1.123      }

Impressum / About Us