From e138dfdcce907e2abe47632f41146f9b13505b30 Mon Sep 17 00:00:00 2001 From: Jesper Lundgren Date: Thu, 26 Sep 2019 11:36:15 +0800 Subject: [PATCH 1/2] add decode_json_null option --- lua_cjson.c | 19 ++++++++++++++++++- tests/agentzh.t | 16 ++++++++++++++++ 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/lua_cjson.c b/lua_cjson.c index 2a69699..01e9a39 100644 --- a/lua_cjson.c +++ b/lua_cjson.c @@ -81,6 +81,7 @@ #define DEFAULT_ENCODE_NUMBER_PRECISION 14 #define DEFAULT_ENCODE_EMPTY_TABLE_AS_OBJECT 1 #define DEFAULT_DECODE_ARRAY_WITH_ARRAY_MT 0 +#define DEFAULT_DECODE_JSON_NULL 1 #ifdef DISABLE_INVALID_NUMBERS #undef DEFAULT_DECODE_INVALID_NUMBERS @@ -159,6 +160,7 @@ typedef struct { int decode_invalid_numbers; int decode_max_depth; int decode_array_with_array_mt; + int decode_json_null; } json_config_t; typedef struct { @@ -350,6 +352,16 @@ static int json_cfg_decode_array_with_array_mt(lua_State *l) return 1; } +/* Configures how to decode json null */ +static int json_cfg_decode_json_null(lua_State *l) +{ + json_config_t *cfg = json_arg_init(l, 1); + + json_enum_option(l, 1, &cfg->decode_json_null, NULL, 1); + + return 1; +} + /* Configures JSON encoding buffer persistence */ static int json_cfg_encode_keep_buffer(lua_State *l) { @@ -442,6 +454,7 @@ static void json_create_config(lua_State *l) cfg->encode_number_precision = DEFAULT_ENCODE_NUMBER_PRECISION; cfg->encode_empty_table_as_object = DEFAULT_ENCODE_EMPTY_TABLE_AS_OBJECT; cfg->decode_array_with_array_mt = DEFAULT_DECODE_ARRAY_WITH_ARRAY_MT; + cfg->decode_json_null = DEFAULT_DECODE_JSON_NULL; #if DEFAULT_ENCODE_KEEP_BUFFER > 0 strbuf_init(&cfg->encode_buf, 0); @@ -1341,7 +1354,10 @@ static void json_process_value(lua_State *l, json_parse_t *json, case T_NULL: /* In Lua, setting "t[k] = nil" will delete k from the table. * Hence a NULL pointer lightuserdata object is used instead */ - lua_pushlightuserdata(l, NULL); + if (json->cfg->decode_json_null) + lua_pushlightuserdata(l, NULL); + else + lua_pushnil(l); break;; default: json_throw_parse_error(l, json, "value", token); @@ -1450,6 +1466,7 @@ static int lua_cjson_new(lua_State *l) { "decode", json_decode }, { "encode_empty_table_as_object", json_cfg_encode_empty_table_as_object }, { "decode_array_with_array_mt", json_cfg_decode_array_with_array_mt }, + { "decode_json_null", json_cfg_decode_json_null }, { "encode_sparse_array", json_cfg_encode_sparse_array }, { "encode_max_depth", json_cfg_encode_max_depth }, { "decode_max_depth", json_cfg_decode_max_depth }, diff --git a/tests/agentzh.t b/tests/agentzh.t index 7967337..0684dd8 100644 --- a/tests/agentzh.t +++ b/tests/agentzh.t @@ -284,3 +284,19 @@ print(string.format("%16.0f", cjson.decode("9007199254740992"))) 9.007199254741e+15 9007199254740992 9007199254740992 + + + +=== TEST 21: optional json null +--- lua +local cjson = require "cjson" +local json = [[{"val1":"str","val2":null,"val3":{"val1":null,"val2":"str"}}]] +print(cjson.encode(cjson.decode(json))) +cjson.decode_json_null(false) +print(cjson.encode(cjson.decode(json))) +cjson.decode_json_null(true) +print(cjson.encode(cjson.decode(json))) +--- out +{"val3":{"val2":"str","val1":null},"val1":"str","val2":null} +{"val3":{"val2":"str"},"val1":"str"} +{"val3":{"val2":"str","val1":null},"val1":"str","val2":null} From dbd55f5d1a63a596fcf867111a69ab16d872642b Mon Sep 17 00:00:00 2001 From: Jesper Lundgren Date: Thu, 26 Sep 2019 11:36:35 +0800 Subject: [PATCH 2/2] update readme with decode_json_null option --- README.md | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/README.md b/README.md index 83bfd76..e69aef7 100644 --- a/README.md +++ b/README.md @@ -15,6 +15,7 @@ Table of Contents * [empty_array_mt](#empty_array_mt) * [encode_number_precision](#encode_number_precision) * [decode_array_with_array_mt](#decode_array_with_array_mt) + * [decode_json_null](#decode_json_null) Description =========== @@ -193,3 +194,13 @@ cjson.encode(t) -- {"my_array":[]} properly re-encoded as an array ``` [Back to TOC](#table-of-contents) + +decode_json_null +----------------------- +**syntax:** `cjson.decode_json_null(enabled)` + +**default:** true + +If disabled, JSON null values will be decoded to Lua `nil` (removed from table) instead of the default `cjson.null` lightuserdata value. + +[Back to TOC](#table-of-contents)