Skip to content

Commit

Permalink
feature: add option to disable forward slash escaping
Browse files Browse the repository at this point in the history
Thanks @spacewander for optimization and documentation.
  • Loading branch information
Jesper Lundgren authored and spacewander committed Mar 24, 2020
1 parent 9931667 commit 0df4888
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 0 deletions.
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ Table of Contents
* [array_mt](#array_mt)
* [empty_array_mt](#empty_array_mt)
* [encode_number_precision](#encode_number_precision)
* [encode_escape_forward_slash](#encode_escape_forward_slash)
* [decode_array_with_array_mt](#decode_array_with_array_mt)

Description
Expand Down Expand Up @@ -158,6 +159,18 @@ This fork allows encoding of numbers with a `precision` up to 16 decimals (vs. 1

[Back to TOC](#table-of-contents)

encode_escape_forward_slash
---------------------------
**syntax:** `cjson.encode_escape_forward_slash(enabled)`

**default:** true

If enabled, forward slash '/' will be encoded as '\/'.

If disabled, forward slash '/' will be encoded as '/' (no escape is applied).

[Back to TOC](#table-of-contents)

decode_array_with_array_mt
--------------------------
**syntax:** `cjson.decode_array_with_array_mt(enabled)`
Expand Down
18 changes: 18 additions & 0 deletions 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_ENCODE_ESCAPE_FORWARD_SLASH 1

#ifdef DISABLE_INVALID_NUMBERS
#undef DEFAULT_DECODE_INVALID_NUMBERS
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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 }
};
Expand Down
19 changes: 19 additions & 0 deletions tests/agentzh.t
Original file line number Diff line number Diff line change
Expand Up @@ -284,3 +284,22 @@ print(string.format("%16.0f", cjson.decode("9007199254740992")))
9.007199254741e+15
9007199254740992
9007199254740992
=== TEST 21: / in string
--- lua
local cjson = require "cjson"
local a={test = "http://google.com/google"}
local b=cjson.encode(a)
print(b)
cjson.encode_escape_forward_slash(false)
local b=cjson.encode(a)
print(b)
cjson.encode_escape_forward_slash(true)
local b=cjson.encode(a)
print(b)
--- out
{"test":"http:\/\/google.com\/google"}
{"test":"http://google.com/google"}
{"test":"http:\/\/google.com\/google"}

0 comments on commit 0df4888

Please sign in to comment.