diff options
author | Yichun Zhang (agentzh) <agentzh@gmail.com> | 2014-02-18 14:33:37 -0800 |
---|---|---|
committer | Yichun Zhang (agentzh) <agentzh@gmail.com> | 2014-02-18 14:33:37 -0800 |
commit | cdb1a73615415e88ac8ef1b2eeec216fe72b9794 (patch) | |
tree | 6d87656c1852ab07dbf348d4dc340a08775eab17 /lua_cjson.c | |
parent | 5fd54895cebd6bdf1e53718ed0691014ce0d2794 (diff) | |
download | lua-cjson-2.1.0.1.tar.gz lua-cjson-2.1.0.1.tar.bz2 lua-cjson-2.1.0.1.zip |
feature: applied Jiale Zhi's patch to add the new config function encode_empty_table_as_object so that we can encode empty Lua tables into empty JSON arrays.2.1.0.1
Diffstat (limited to 'lua_cjson.c')
-rw-r--r-- | lua_cjson.c | 14 |
1 files changed, 13 insertions, 1 deletions
diff --git a/lua_cjson.c b/lua_cjson.c index c14a1c5..97f2350 100644 --- a/lua_cjson.c +++ b/lua_cjson.c | |||
@@ -68,6 +68,7 @@ | |||
68 | #define DEFAULT_DECODE_INVALID_NUMBERS 1 | 68 | #define DEFAULT_DECODE_INVALID_NUMBERS 1 |
69 | #define DEFAULT_ENCODE_KEEP_BUFFER 1 | 69 | #define DEFAULT_ENCODE_KEEP_BUFFER 1 |
70 | #define DEFAULT_ENCODE_NUMBER_PRECISION 14 | 70 | #define DEFAULT_ENCODE_NUMBER_PRECISION 14 |
71 | #define DEFAULT_ENCODE_EMPTY_TABLE_AS_OBJECT 1 | ||
71 | 72 | ||
72 | #ifdef DISABLE_INVALID_NUMBERS | 73 | #ifdef DISABLE_INVALID_NUMBERS |
73 | #undef DEFAULT_DECODE_INVALID_NUMBERS | 74 | #undef DEFAULT_DECODE_INVALID_NUMBERS |
@@ -124,6 +125,7 @@ typedef struct { | |||
124 | int encode_invalid_numbers; /* 2 => Encode as "null" */ | 125 | int encode_invalid_numbers; /* 2 => Encode as "null" */ |
125 | int encode_number_precision; | 126 | int encode_number_precision; |
126 | int encode_keep_buffer; | 127 | int encode_keep_buffer; |
128 | int encode_empty_table_as_object; | ||
127 | 129 | ||
128 | int decode_invalid_numbers; | 130 | int decode_invalid_numbers; |
129 | int decode_max_depth; | 131 | int decode_max_depth; |
@@ -300,6 +302,14 @@ static int json_cfg_encode_number_precision(lua_State *l) | |||
300 | return json_integer_option(l, 1, &cfg->encode_number_precision, 1, 14); | 302 | return json_integer_option(l, 1, &cfg->encode_number_precision, 1, 14); |
301 | } | 303 | } |
302 | 304 | ||
305 | /* Configures how to treat empty table when encode lua table */ | ||
306 | static int json_cfg_encode_empty_table_as_object(lua_State *l) | ||
307 | { | ||
308 | json_config_t *cfg = json_arg_init(l, 1); | ||
309 | |||
310 | return json_enum_option(l, 1, &cfg->encode_empty_table_as_object, NULL, 1); | ||
311 | } | ||
312 | |||
303 | /* Configures JSON encoding buffer persistence */ | 313 | /* Configures JSON encoding buffer persistence */ |
304 | static int json_cfg_encode_keep_buffer(lua_State *l) | 314 | static int json_cfg_encode_keep_buffer(lua_State *l) |
305 | { | 315 | { |
@@ -390,6 +400,7 @@ static void json_create_config(lua_State *l) | |||
390 | cfg->decode_invalid_numbers = DEFAULT_DECODE_INVALID_NUMBERS; | 400 | cfg->decode_invalid_numbers = DEFAULT_DECODE_INVALID_NUMBERS; |
391 | cfg->encode_keep_buffer = DEFAULT_ENCODE_KEEP_BUFFER; | 401 | cfg->encode_keep_buffer = DEFAULT_ENCODE_KEEP_BUFFER; |
392 | cfg->encode_number_precision = DEFAULT_ENCODE_NUMBER_PRECISION; | 402 | cfg->encode_number_precision = DEFAULT_ENCODE_NUMBER_PRECISION; |
403 | cfg->encode_empty_table_as_object = DEFAULT_ENCODE_EMPTY_TABLE_AS_OBJECT; | ||
393 | 404 | ||
394 | #if DEFAULT_ENCODE_KEEP_BUFFER > 0 | 405 | #if DEFAULT_ENCODE_KEEP_BUFFER > 0 |
395 | strbuf_init(&cfg->encode_buf, 0); | 406 | strbuf_init(&cfg->encode_buf, 0); |
@@ -685,7 +696,7 @@ static void json_append_data(lua_State *l, json_config_t *cfg, | |||
685 | current_depth++; | 696 | current_depth++; |
686 | json_check_encode_depth(l, cfg, current_depth, json); | 697 | json_check_encode_depth(l, cfg, current_depth, json); |
687 | len = lua_array_length(l, cfg, json); | 698 | len = lua_array_length(l, cfg, json); |
688 | if (len > 0) | 699 | if (len > 0 || (len == 0 && !cfg->encode_empty_table_as_object)) |
689 | json_append_array(l, cfg, current_depth, json, len); | 700 | json_append_array(l, cfg, current_depth, json, len); |
690 | else | 701 | else |
691 | json_append_object(l, cfg, current_depth, json); | 702 | json_append_object(l, cfg, current_depth, json); |
@@ -1352,6 +1363,7 @@ static int lua_cjson_new(lua_State *l) | |||
1352 | luaL_Reg reg[] = { | 1363 | luaL_Reg reg[] = { |
1353 | { "encode", json_encode }, | 1364 | { "encode", json_encode }, |
1354 | { "decode", json_decode }, | 1365 | { "decode", json_decode }, |
1366 | { "encode_empty_table_as_object", json_cfg_encode_empty_table_as_object }, | ||
1355 | { "encode_sparse_array", json_cfg_encode_sparse_array }, | 1367 | { "encode_sparse_array", json_cfg_encode_sparse_array }, |
1356 | { "encode_max_depth", json_cfg_encode_max_depth }, | 1368 | { "encode_max_depth", json_cfg_encode_max_depth }, |
1357 | { "decode_max_depth", json_cfg_decode_max_depth }, | 1369 | { "decode_max_depth", json_cfg_decode_max_depth }, |