aboutsummaryrefslogtreecommitdiff
path: root/lua_cjson.c
diff options
context:
space:
mode:
Diffstat (limited to 'lua_cjson.c')
-rw-r--r--lua_cjson.c21
1 files changed, 21 insertions, 0 deletions
diff --git a/lua_cjson.c b/lua_cjson.c
index 8d6e313..6c7eb4e 100644
--- a/lua_cjson.c
+++ b/lua_cjson.c
@@ -79,6 +79,7 @@
79#define DEFAULT_ENCODE_KEEP_BUFFER 1 79#define DEFAULT_ENCODE_KEEP_BUFFER 1
80#define DEFAULT_ENCODE_NUMBER_PRECISION 14 80#define DEFAULT_ENCODE_NUMBER_PRECISION 14
81#define DEFAULT_ENCODE_EMPTY_TABLE_AS_OBJECT 1 81#define DEFAULT_ENCODE_EMPTY_TABLE_AS_OBJECT 1
82#define DEFAULT_DECODE_ARRAY_WITH_ARRAY_MT 0
82 83
83#ifdef DISABLE_INVALID_NUMBERS 84#ifdef DISABLE_INVALID_NUMBERS
84#undef DEFAULT_DECODE_INVALID_NUMBERS 85#undef DEFAULT_DECODE_INVALID_NUMBERS
@@ -148,6 +149,7 @@ typedef struct {
148 149
149 int decode_invalid_numbers; 150 int decode_invalid_numbers;
150 int decode_max_depth; 151 int decode_max_depth;
152 int decode_array_with_array_mt;
151} json_config_t; 153} json_config_t;
152 154
153typedef struct { 155typedef struct {
@@ -329,6 +331,16 @@ static int json_cfg_encode_empty_table_as_object(lua_State *l)
329 return json_enum_option(l, 1, &cfg->encode_empty_table_as_object, NULL, 1); 331 return json_enum_option(l, 1, &cfg->encode_empty_table_as_object, NULL, 1);
330} 332}
331 333
334/* Configures how to decode arrays */
335static int json_cfg_decode_array_with_array_mt(lua_State *l)
336{
337 json_config_t *cfg = json_arg_init(l, 1);
338
339 json_enum_option(l, 1, &cfg->decode_array_with_array_mt, NULL, 1);
340
341 return 1;
342}
343
332/* Configures JSON encoding buffer persistence */ 344/* Configures JSON encoding buffer persistence */
333static int json_cfg_encode_keep_buffer(lua_State *l) 345static int json_cfg_encode_keep_buffer(lua_State *l)
334{ 346{
@@ -420,6 +432,7 @@ static void json_create_config(lua_State *l)
420 cfg->encode_keep_buffer = DEFAULT_ENCODE_KEEP_BUFFER; 432 cfg->encode_keep_buffer = DEFAULT_ENCODE_KEEP_BUFFER;
421 cfg->encode_number_precision = DEFAULT_ENCODE_NUMBER_PRECISION; 433 cfg->encode_number_precision = DEFAULT_ENCODE_NUMBER_PRECISION;
422 cfg->encode_empty_table_as_object = DEFAULT_ENCODE_EMPTY_TABLE_AS_OBJECT; 434 cfg->encode_empty_table_as_object = DEFAULT_ENCODE_EMPTY_TABLE_AS_OBJECT;
435 cfg->decode_array_with_array_mt = DEFAULT_DECODE_ARRAY_WITH_ARRAY_MT;
423 436
424#if DEFAULT_ENCODE_KEEP_BUFFER > 0 437#if DEFAULT_ENCODE_KEEP_BUFFER > 0
425 strbuf_init(&cfg->encode_buf, 0); 438 strbuf_init(&cfg->encode_buf, 0);
@@ -1261,6 +1274,13 @@ static void json_parse_array_context(lua_State *l, json_parse_t *json)
1261 1274
1262 lua_newtable(l); 1275 lua_newtable(l);
1263 1276
1277 /* set array_mt on the table at the top of the stack */
1278 if (json->cfg->decode_array_with_array_mt) {
1279 lua_pushlightuserdata(l, &json_array);
1280 lua_rawget(l, LUA_REGISTRYINDEX);
1281 lua_setmetatable(l, -2);
1282 }
1283
1264 json_next_token(json, &token); 1284 json_next_token(json, &token);
1265 1285
1266 /* Handle empty arrays */ 1286 /* Handle empty arrays */
@@ -1415,6 +1435,7 @@ static int lua_cjson_new(lua_State *l)
1415 { "encode", json_encode }, 1435 { "encode", json_encode },
1416 { "decode", json_decode }, 1436 { "decode", json_decode },
1417 { "encode_empty_table_as_object", json_cfg_encode_empty_table_as_object }, 1437 { "encode_empty_table_as_object", json_cfg_encode_empty_table_as_object },
1438 { "decode_array_with_array_mt", json_cfg_decode_array_with_array_mt },
1418 { "encode_sparse_array", json_cfg_encode_sparse_array }, 1439 { "encode_sparse_array", json_cfg_encode_sparse_array },
1419 { "encode_max_depth", json_cfg_encode_max_depth }, 1440 { "encode_max_depth", json_cfg_encode_max_depth },
1420 { "decode_max_depth", json_cfg_decode_max_depth }, 1441 { "decode_max_depth", json_cfg_decode_max_depth },