Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add decode option to ignore JSON null values #51

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
===========
Expand Down Expand Up @@ -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)
19 changes: 18 additions & 1 deletion lua_cjson.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
{
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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 },
Expand Down
16 changes: 16 additions & 0 deletions tests/agentzh.t
Original file line number Diff line number Diff line change
Expand Up @@ -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}