Hello. Thanks for the patch! My congratulations on your
second patch!
See and fix 10 comments below to make your patch better.
1. Please, use imperative in commit header.
Not 'json: added options to json.encode()', but
'json: add options to json.encode()'.
On 08/07/2018 03:57, Roman Khabibov wrote:
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
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 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 ===== */