Skip to content

Commit 0df4888

Browse files
Jesper Lundgrenspacewander
authored andcommitted
feature: add option to disable forward slash escaping
Thanks @spacewander for optimization and documentation.
1 parent 9931667 commit 0df4888

3 files changed

Lines changed: 50 additions & 0 deletions

File tree

README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ Table of Contents
1414
* [array_mt](#array_mt)
1515
* [empty_array_mt](#empty_array_mt)
1616
* [encode_number_precision](#encode_number_precision)
17+
* [encode_escape_forward_slash](#encode_escape_forward_slash)
1718
* [decode_array_with_array_mt](#decode_array_with_array_mt)
1819

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

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

162+
encode_escape_forward_slash
163+
---------------------------
164+
**syntax:** `cjson.encode_escape_forward_slash(enabled)`
165+
166+
**default:** true
167+
168+
If enabled, forward slash '/' will be encoded as '\/'.
169+
170+
If disabled, forward slash '/' will be encoded as '/' (no escape is applied).
171+
172+
[Back to TOC](#table-of-contents)
173+
161174
decode_array_with_array_mt
162175
--------------------------
163176
**syntax:** `cjson.decode_array_with_array_mt(enabled)`

lua_cjson.c

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@
8181
#define DEFAULT_ENCODE_NUMBER_PRECISION 14
8282
#define DEFAULT_ENCODE_EMPTY_TABLE_AS_OBJECT 1
8383
#define DEFAULT_DECODE_ARRAY_WITH_ARRAY_MT 0
84+
#define DEFAULT_ENCODE_ESCAPE_FORWARD_SLASH 1
8485

8586
#ifdef DISABLE_INVALID_NUMBERS
8687
#undef DEFAULT_DECODE_INVALID_NUMBERS
@@ -155,6 +156,7 @@ typedef struct {
155156
int encode_number_precision;
156157
int encode_keep_buffer;
157158
int encode_empty_table_as_object;
159+
int encode_escape_forward_slash;
158160

159161
int decode_invalid_numbers;
160162
int decode_max_depth;
@@ -406,6 +408,20 @@ static int json_cfg_decode_invalid_numbers(lua_State *l)
406408
return 1;
407409
}
408410

411+
static int json_cfg_encode_escape_forward_slash(lua_State *l)
412+
{
413+
int ret;
414+
json_config_t *cfg = json_arg_init(l, 1);
415+
416+
ret = json_enum_option(l, 1, &cfg->encode_escape_forward_slash, NULL, 1);
417+
if (cfg->encode_escape_forward_slash) {
418+
char2escape['/'] = "\\/";
419+
} else {
420+
char2escape['/'] = NULL;
421+
}
422+
return ret;
423+
}
424+
409425
static int json_destroy_config(lua_State *l)
410426
{
411427
json_config_t *cfg;
@@ -442,6 +458,7 @@ static void json_create_config(lua_State *l)
442458
cfg->encode_number_precision = DEFAULT_ENCODE_NUMBER_PRECISION;
443459
cfg->encode_empty_table_as_object = DEFAULT_ENCODE_EMPTY_TABLE_AS_OBJECT;
444460
cfg->decode_array_with_array_mt = DEFAULT_DECODE_ARRAY_WITH_ARRAY_MT;
461+
cfg->encode_escape_forward_slash = DEFAULT_ENCODE_ESCAPE_FORWARD_SLASH;
445462

446463
#if DEFAULT_ENCODE_KEEP_BUFFER > 0
447464
strbuf_init(&cfg->encode_buf, 0);
@@ -1457,6 +1474,7 @@ static int lua_cjson_new(lua_State *l)
14571474
{ "encode_keep_buffer", json_cfg_encode_keep_buffer },
14581475
{ "encode_invalid_numbers", json_cfg_encode_invalid_numbers },
14591476
{ "decode_invalid_numbers", json_cfg_decode_invalid_numbers },
1477+
{ "encode_escape_forward_slash", json_cfg_encode_escape_forward_slash },
14601478
{ "new", lua_cjson_new },
14611479
{ NULL, NULL }
14621480
};

tests/agentzh.t

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,3 +284,22 @@ print(string.format("%16.0f", cjson.decode("9007199254740992")))
284284
9.007199254741e+15
285285
9007199254740992
286286
9007199254740992
287+
288+
289+
290+
=== TEST 21: / in string
291+
--- lua
292+
local cjson = require "cjson"
293+
local a={test = "http://google.com/google"}
294+
local b=cjson.encode(a)
295+
print(b)
296+
cjson.encode_escape_forward_slash(false)
297+
local b=cjson.encode(a)
298+
print(b)
299+
cjson.encode_escape_forward_slash(true)
300+
local b=cjson.encode(a)
301+
print(b)
302+
--- out
303+
{"test":"http:\/\/google.com\/google"}
304+
{"test":"http://google.com/google"}
305+
{"test":"http:\/\/google.com\/google"}

0 commit comments

Comments
 (0)