[tarantool-patches] [PATCH] json: added options to json.encode()

  • From: Roman Khabibov <roman.habibov1@xxxxxxxxx>
  • To: tarantool-patches@xxxxxxxxxxxxx
  • Date: Sun, 8 Jul 2018 03:57:34 +0300

Added an ability to pass encoder options to json.encode().

Closes: #2888.
---
 test/app-tap/json.test.lua        | 27 +++++++++++-
 third_party/lua-cjson/lua_cjson.c | 90 +++++++++++++++++++++++++++++++++------
 2 files changed, 104 insertions(+), 13 deletions(-)

diff --git a/test/app-tap/json.test.lua b/test/app-tap/json.test.lua
index 3884b41..1252ad0 100755
--- a/test/app-tap/json.test.lua
+++ b/test/app-tap/json.test.lua
@@ -22,7 +22,32 @@ end
 
 tap.test("json", function(test)
     local serializer = require('json')
-    test:plan(9)
+    test:plan(18)
+
+-- gh-2888 Added opions to encode().
+
+    local sub = {a = 1, { b = {c = 1, d = {e = 1}}}}
+    serializer.cfg({encode_max_depth = 1})
+    test:ok(serializer.encode(sub) == '{"1":null,"a":1}', 'sub == 
{"1":null,"a":1}')
+    serializer.cfg({encode_max_depth = 2})
+    test:ok(serializer.encode(sub) == '{"1":{"b":null},"a":1}', 'sub == 
{"1":{"b":null},"a":1}')
+    serializer.cfg({encode_max_depth = 2})
+    test:ok(serializer.encode(sub, {encode_max_depth = 1}) == 
'{"1":null,"a":1}', 'sub == {"1":null,"a":1}')
+
+    local nan = 1/0
+    test:ok(serializer.encode({a = nan}) == '{"a":inf}', 'a = nan == 
{"a":inf}')
+    serializer.cfg({encode_invalid_numbers = false})
+    test:ok(pcall(serializer.encode, {a = nan}) == false, 'error when 
"encode_invalid_numbers = false" with NaN')
+    serializer.cfg({encode_invalid_numbers = true})
+    test:ok(pcall(serializer.encode, {a = nan}, {encode_invalid_numbers = 
false}) == false, 'error when "encode_invalid_numbers = false" with NaN')
+
+    local number = 0.12345
+    test:ok(serializer.encode({a = number}) == '{"a":0.12345}', 'precision 
more than 5')
+    serializer.cfg({encode_number_precision = 3})
+    test:ok(serializer.encode({a = number}) == '{"a":0.123}', 'precision is 3')
+    serializer.cfg({encode_number_precision = 14})
+    test:ok(serializer.encode({a = number}, {encode_number_precision = 3}) == 
'{"a":0.123}', 'precision is 3')
+
     test:test("unsigned", common.test_unsigned, serializer)
     test:test("signed", common.test_signed, serializer)
     test:test("double", common.test_double, serializer)
diff --git a/third_party/lua-cjson/lua_cjson.c 
b/third_party/lua-cjson/lua_cjson.c
index aa8217d..bdfda1e 100644
--- a/third_party/lua-cjson/lua_cjson.c
+++ b/third_party/lua-cjson/lua_cjson.c
@@ -417,23 +417,89 @@ static void json_append_data(lua_State *l, struct 
luaL_serializer *cfg,
     }
 }
 
-static int json_encode(lua_State *l)
-{
-    struct luaL_serializer *cfg = luaL_checkserializer(l);
-    char *json;
-    int len;
+#define OPTION(type, name, defvalue) { #name, \
+       offsetof(struct luaL_serializer, name), type, defvalue}
+
+static struct {
+       const char *name;
+       size_t offset; /* offset in structure */
+       int type;
+       int defvalue;
+} OPTIONS[] = {
+       OPTION(LUA_TBOOLEAN, encode_sparse_convert, 1),
+       OPTION(LUA_TNUMBER,  encode_sparse_ratio, 2),
+       OPTION(LUA_TNUMBER,  encode_sparse_safe, 10),
+       OPTION(LUA_TNUMBER,  encode_max_depth, 32),
+       OPTION(LUA_TBOOLEAN, encode_invalid_numbers, 1),
+       OPTION(LUA_TNUMBER,  encode_number_precision, 14),
+       OPTION(LUA_TBOOLEAN, encode_load_metatables, 1),
+       OPTION(LUA_TBOOLEAN, encode_use_tostring, 0),
+       OPTION(LUA_TBOOLEAN, encode_invalid_as_nil, 0),
+       OPTION(LUA_TBOOLEAN, decode_invalid_numbers, 1),
+       OPTION(LUA_TBOOLEAN, decode_save_metatables, 1),
+       OPTION(LUA_TNUMBER,  decode_max_depth, 32),
+       { NULL, 0, 0, 0},
+};
 
-    luaL_argcheck(l, lua_gettop(l) == 1, 1, "expected 1 argument");
+/*Serialize Lua table of options to luaL_serializer struct*/
+static int parse_options(lua_State *l, struct luaL_serializer* cfg) {
+    for (int i = 0; OPTIONS[i].name != NULL; i++) {
+               lua_getfield(l, 2, OPTIONS[i].name);
+               if (lua_isnil(l, -1)) {
+                       lua_pop(l, 1); /* key hasn't changed */
+                       continue;
+               }
+               /*
+                * Update struct luaL_serializer using pointer to a
+                * configuration value (all values must be `int` for that).
+                */
+               int *pval = (int *) ((char *) cfg + OPTIONS[i].offset);
+               /* Update struct luaL_serializer structure */
+               switch (OPTIONS[i].type) {
+               case LUA_TBOOLEAN:
+                       *pval = lua_toboolean(l, -1);
+                       lua_pop(l, 1);
+                       break;
+               case LUA_TNUMBER:
+                       *pval = lua_tointeger(l, -1);
+                       lua_pop(l, 1);
+                       break;
+               default:
+                       unreachable();
+               }
+       }
+       lua_pop(l, 1);
+       return 0;
+}
 
-    /* Reuse existing buffer */
-    strbuf_reset(&encode_buf);
+static int json_encode(lua_State *l) {
+       luaL_argcheck(l, (lua_gettop(l) == 2) || (lua_gettop(l) == 1), 1, 
"expected 1 or 2 arguments");
 
-    json_append_data(l, cfg, 0, &encode_buf);
-    json = strbuf_string(&encode_buf, &len);
+       char *json;
+       int len;
 
-    lua_pushlstring(l, json, len);
+       /* Reuse existing buffer */
+       strbuf_reset(&encode_buf);
+       struct luaL_serializer *cfg = luaL_checkserializer(l);
 
-    return 1;
+       /* If have second arg */
+       if (lua_gettop(l) == 2) {
+               struct luaL_serializer user_cfg = *cfg;
+
+               parse_options(l, &user_cfg);
+
+               json_append_data(l, &user_cfg, 0, &encode_buf);
+       }
+       /* If have not second arg */
+       else {
+
+               json_append_data(l, cfg, 0, &encode_buf);
+       }
+
+       json = strbuf_string(&encode_buf, &len);
+       lua_pushlstring(l, json, len);
+
+       return 1;
 }
 
 /* ===== DECODING ===== */
-- 
2.7.4


Other related posts: