summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--README.md13
-rw-r--r--lua_cjson.c18
-rw-r--r--tests/agentzh.t19
3 files changed, 50 insertions, 0 deletions
diff --git a/README.md b/README.md
index 83bfd76..ace23d1 100644
--- a/README.md
+++ b/README.md
@@ -14,6 +14,7 @@ Table of Contents
14 * [array_mt](#array_mt) 14 * [array_mt](#array_mt)
15 * [empty_array_mt](#empty_array_mt) 15 * [empty_array_mt](#empty_array_mt)
16 * [encode_number_precision](#encode_number_precision) 16 * [encode_number_precision](#encode_number_precision)
17 * [encode_escape_forward_slash](#encode_escape_forward_slash)
17 * [decode_array_with_array_mt](#decode_array_with_array_mt) 18 * [decode_array_with_array_mt](#decode_array_with_array_mt)
18 19
19Description 20Description
@@ -158,6 +159,18 @@ This fork allows encoding of numbers with a `precision` up to 16 decimals (vs. 1
158 159
159[Back to TOC](#table-of-contents) 160[Back to TOC](#table-of-contents)
160 161
162encode_escape_forward_slash
163---------------------------
164**syntax:** `cjson.encode_escape_forward_slash(enabled)`
165
166**default:** true
167
168If enabled, forward slash '/' will be encoded as '\/'.
169
170If disabled, forward slash '/' will be encoded as '/' (no escape is applied).
171
172[Back to TOC](#table-of-contents)
173
161decode_array_with_array_mt 174decode_array_with_array_mt
162-------------------------- 175--------------------------
163**syntax:** `cjson.decode_array_with_array_mt(enabled)` 176**syntax:** `cjson.decode_array_with_array_mt(enabled)`
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 @@
81#define DEFAULT_ENCODE_NUMBER_PRECISION 14 81#define DEFAULT_ENCODE_NUMBER_PRECISION 14
82#define DEFAULT_ENCODE_EMPTY_TABLE_AS_OBJECT 1 82#define DEFAULT_ENCODE_EMPTY_TABLE_AS_OBJECT 1
83#define DEFAULT_DECODE_ARRAY_WITH_ARRAY_MT 0 83#define DEFAULT_DECODE_ARRAY_WITH_ARRAY_MT 0
84#define DEFAULT_ENCODE_ESCAPE_FORWARD_SLASH 1
84 85
85#ifdef DISABLE_INVALID_NUMBERS 86#ifdef DISABLE_INVALID_NUMBERS
86#undef DEFAULT_DECODE_INVALID_NUMBERS 87#undef DEFAULT_DECODE_INVALID_NUMBERS
@@ -155,6 +156,7 @@ typedef struct {
155 int encode_number_precision; 156 int encode_number_precision;
156 int encode_keep_buffer; 157 int encode_keep_buffer;
157 int encode_empty_table_as_object; 158 int encode_empty_table_as_object;
159 int encode_escape_forward_slash;
158 160
159 int decode_invalid_numbers; 161 int decode_invalid_numbers;
160 int decode_max_depth; 162 int decode_max_depth;
@@ -406,6 +408,20 @@ static int json_cfg_decode_invalid_numbers(lua_State *l)
406 return 1; 408 return 1;
407} 409}
408 410
411static 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
409static int json_destroy_config(lua_State *l) 425static int json_destroy_config(lua_State *l)
410{ 426{
411 json_config_t *cfg; 427 json_config_t *cfg;
@@ -442,6 +458,7 @@ static void json_create_config(lua_State *l)
442 cfg->encode_number_precision = DEFAULT_ENCODE_NUMBER_PRECISION; 458 cfg->encode_number_precision = DEFAULT_ENCODE_NUMBER_PRECISION;
443 cfg->encode_empty_table_as_object = DEFAULT_ENCODE_EMPTY_TABLE_AS_OBJECT; 459 cfg->encode_empty_table_as_object = DEFAULT_ENCODE_EMPTY_TABLE_AS_OBJECT;
444 cfg->decode_array_with_array_mt = DEFAULT_DECODE_ARRAY_WITH_ARRAY_MT; 460 cfg->decode_array_with_array_mt = DEFAULT_DECODE_ARRAY_WITH_ARRAY_MT;
461 cfg->encode_escape_forward_slash = DEFAULT_ENCODE_ESCAPE_FORWARD_SLASH;
445 462
446#if DEFAULT_ENCODE_KEEP_BUFFER > 0 463#if DEFAULT_ENCODE_KEEP_BUFFER > 0
447 strbuf_init(&cfg->encode_buf, 0); 464 strbuf_init(&cfg->encode_buf, 0);
@@ -1457,6 +1474,7 @@ static int lua_cjson_new(lua_State *l)
1457 { "encode_keep_buffer", json_cfg_encode_keep_buffer }, 1474 { "encode_keep_buffer", json_cfg_encode_keep_buffer },
1458 { "encode_invalid_numbers", json_cfg_encode_invalid_numbers }, 1475 { "encode_invalid_numbers", json_cfg_encode_invalid_numbers },
1459 { "decode_invalid_numbers", json_cfg_decode_invalid_numbers }, 1476 { "decode_invalid_numbers", json_cfg_decode_invalid_numbers },
1477 { "encode_escape_forward_slash", json_cfg_encode_escape_forward_slash },
1460 { "new", lua_cjson_new }, 1478 { "new", lua_cjson_new },
1461 { NULL, NULL } 1479 { NULL, NULL }
1462 }; 1480 };
diff --git a/tests/agentzh.t b/tests/agentzh.t
index 7967337..7591902 100644
--- a/tests/agentzh.t
+++ b/tests/agentzh.t
@@ -284,3 +284,22 @@ print(string.format("%16.0f", cjson.decode("9007199254740992")))
2849.007199254741e+15 2849.007199254741e+15
2859007199254740992 2859007199254740992
2869007199254740992 2869007199254740992
287
288
289
290=== TEST 21: / in string
291--- lua
292local cjson = require "cjson"
293local a={test = "http://google.com/google"}
294local b=cjson.encode(a)
295print(b)
296cjson.encode_escape_forward_slash(false)
297local b=cjson.encode(a)
298print(b)
299cjson.encode_escape_forward_slash(true)
300local b=cjson.encode(a)
301print(b)
302--- out
303{"test":"http:\/\/google.com\/google"}
304{"test":"http://google.com/google"}
305{"test":"http:\/\/google.com\/google"}