From 0df488874f52a881d14b5876babaa780bb6200ee Mon Sep 17 00:00:00 2001 From: Jesper Lundgren Date: Mon, 9 Mar 2020 16:12:24 +0800 Subject: feature: add option to disable forward slash escaping Thanks @spacewander for optimization and documentation. --- lua_cjson.c | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) (limited to 'lua_cjson.c') diff --git a/lua_cjson.c b/lua_cjson.c index 2a69699..875bdaf 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_ENCODE_ESCAPE_FORWARD_SLASH 1 #ifdef DISABLE_INVALID_NUMBERS #undef DEFAULT_DECODE_INVALID_NUMBERS @@ -155,6 +156,7 @@ typedef struct { int encode_number_precision; int encode_keep_buffer; int encode_empty_table_as_object; + int encode_escape_forward_slash; int decode_invalid_numbers; int decode_max_depth; @@ -406,6 +408,20 @@ static int json_cfg_decode_invalid_numbers(lua_State *l) return 1; } +static int json_cfg_encode_escape_forward_slash(lua_State *l) +{ + int ret; + json_config_t *cfg = json_arg_init(l, 1); + + ret = json_enum_option(l, 1, &cfg->encode_escape_forward_slash, NULL, 1); + if (cfg->encode_escape_forward_slash) { + char2escape['/'] = "\\/"; + } else { + char2escape['/'] = NULL; + } + return ret; +} + static int json_destroy_config(lua_State *l) { json_config_t *cfg; @@ -442,6 +458,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->encode_escape_forward_slash = DEFAULT_ENCODE_ESCAPE_FORWARD_SLASH; #if DEFAULT_ENCODE_KEEP_BUFFER > 0 strbuf_init(&cfg->encode_buf, 0); @@ -1457,6 +1474,7 @@ static int lua_cjson_new(lua_State *l) { "encode_keep_buffer", json_cfg_encode_keep_buffer }, { "encode_invalid_numbers", json_cfg_encode_invalid_numbers }, { "decode_invalid_numbers", json_cfg_decode_invalid_numbers }, + { "encode_escape_forward_slash", json_cfg_encode_escape_forward_slash }, { "new", lua_cjson_new }, { NULL, NULL } }; -- cgit v1.2.3-55-g6feb