From e111e9a648304c039939a6e9199aae637da43df1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Calio=20=28=E6=94=AF=E5=AE=B6=E4=B9=90=29?= Date: Tue, 29 Oct 2013 21:03:06 -0700 Subject: [PATCH] Add encode_empty_table_as_object which controls how to encode empty table --- lua_cjson.c | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) 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 @@ #define DEFAULT_DECODE_INVALID_NUMBERS 1 #define DEFAULT_ENCODE_KEEP_BUFFER 1 #define DEFAULT_ENCODE_NUMBER_PRECISION 14 +#define DEFAULT_ENCODE_EMPTY_TABLE_AS_OBJECT 1 #ifdef DISABLE_INVALID_NUMBERS #undef DEFAULT_DECODE_INVALID_NUMBERS @@ -124,6 +125,7 @@ typedef struct { int encode_invalid_numbers; /* 2 => Encode as "null" */ int encode_number_precision; int encode_keep_buffer; + int encode_empty_table_as_object; int decode_invalid_numbers; int decode_max_depth; @@ -300,6 +302,14 @@ static int json_cfg_encode_number_precision(lua_State *l) return json_integer_option(l, 1, &cfg->encode_number_precision, 1, 14); } +/* Configures how to treat empty table when encode lua table */ +static int json_cfg_encode_empty_table_as_object(lua_State *l) +{ + json_config_t *cfg = json_arg_init(l, 1); + + return json_enum_option(l, 1, &cfg->encode_empty_table_as_object, NULL, 1); +} + /* Configures JSON encoding buffer persistence */ static int json_cfg_encode_keep_buffer(lua_State *l) { @@ -390,6 +400,7 @@ static void json_create_config(lua_State *l) cfg->decode_invalid_numbers = DEFAULT_DECODE_INVALID_NUMBERS; cfg->encode_keep_buffer = DEFAULT_ENCODE_KEEP_BUFFER; cfg->encode_number_precision = DEFAULT_ENCODE_NUMBER_PRECISION; + cfg->encode_empty_table_as_object = DEFAULT_ENCODE_EMPTY_TABLE_AS_OBJECT; #if DEFAULT_ENCODE_KEEP_BUFFER > 0 strbuf_init(&cfg->encode_buf, 0); @@ -685,7 +696,7 @@ static void json_append_data(lua_State *l, json_config_t *cfg, current_depth++; json_check_encode_depth(l, cfg, current_depth, json); len = lua_array_length(l, cfg, json); - if (len > 0) + if (len > 0 || (len == 0 && !cfg->encode_empty_table_as_object)) json_append_array(l, cfg, current_depth, json, len); else json_append_object(l, cfg, current_depth, json); @@ -1352,6 +1363,7 @@ static int lua_cjson_new(lua_State *l) luaL_Reg reg[] = { { "encode", json_encode }, { "decode", json_decode }, + { "encode_empty_table_as_object", json_cfg_encode_empty_table_as_object }, { "encode_sparse_array", json_cfg_encode_sparse_array }, { "encode_max_depth", json_cfg_encode_max_depth }, { "decode_max_depth", json_cfg_decode_max_depth },