From 316da8581819726597592be6d8ac787120c979bf Mon Sep 17 00:00:00 2001 From: Eric Haszlakiewicz Date: Sat, 28 Nov 2015 20:00:30 -0600 Subject: [PATCH] Fix issue #201: add a JSON_C_TO_STRING_NOSLASHESCAPE flag to turn off escaping of forward slashes. --- json_object.c | 12 +++++++++--- json_object.h | 5 +++++ tests/test1.c | 6 ++++++ tests/test1.expected | 3 +++ tests/test1Formatted_plain.expected | 3 +++ tests/test1Formatted_pretty.expected | 3 +++ tests/test1Formatted_spaced.expected | 3 +++ tests/test_parse.c | 3 +++ tests/test_parse.expected | 4 +++- 9 files changed, 38 insertions(+), 4 deletions(-) diff --git a/json_object.c b/json_object.c index 9ac22a3..5a758d6 100644 --- a/json_object.c +++ b/json_object.c @@ -106,7 +106,7 @@ get_string_component(struct json_object *jso) /* string escaping */ -static int json_escape_str(struct printbuf *pb, const char *str, int len) +static int json_escape_str(struct printbuf *pb, const char *str, int len, int flags) { int pos = 0, start_offset = 0; unsigned char c; @@ -133,6 +133,12 @@ static int json_escape_str(struct printbuf *pb, const char *str, int len) else if(c == '\f') printbuf_memappend(pb, "\\f", 2); else if(c == '"') printbuf_memappend(pb, "\\\"", 2); else if(c == '\\') printbuf_memappend(pb, "\\\\", 2); + else if(c == '/' && + (flags & JSON_C_TO_STRING_NOSLASHESCAPE)) + { + pos++; + break; + } else if(c == '/') printbuf_memappend(pb, "\\/", 2); start_offset = ++pos; @@ -346,7 +352,7 @@ static int json_object_object_to_json_string(struct json_object* jso, sprintbuf(pb, " "); indent(pb, level+1, flags); sprintbuf(pb, "\""); - json_escape_str(pb, iter.key, strlen(iter.key)); + json_escape_str(pb, iter.key, strlen(iter.key), flags); if (flags & JSON_C_TO_STRING_SPACED) sprintbuf(pb, "\": "); else @@ -773,7 +779,7 @@ static int json_object_string_to_json_string(struct json_object* jso, int flags) { sprintbuf(pb, "\""); - json_escape_str(pb, get_string_component(jso), jso->o.c_string.len); + json_escape_str(pb, get_string_component(jso), jso->o.c_string.len, flags); sprintbuf(pb, "\""); return 0; } diff --git a/json_object.h b/json_object.h index 67137d8..6951019 100644 --- a/json_object.h +++ b/json_object.h @@ -63,6 +63,11 @@ extern "C" { */ #define JSON_C_TO_STRING_NOZERO (1<<2) +/** + * Don't escape forward slashes. + */ +#define JSON_C_TO_STRING_NOSLASHESCAPE (1<<4) + /** * A flag for the json_object_object_add_ex function which * causes the value to be added without a check if it already exists. diff --git a/tests/test1.c b/tests/test1.c index 9e5bb63..180dedb 100644 --- a/tests/test1.c +++ b/tests/test1.c @@ -57,6 +57,12 @@ int main(int argc, char **argv) printf("my_string.to_string()=%s\n", json_object_to_json_string(my_string)); json_object_put(my_string); + my_string = json_object_new_string("/"); + printf("my_string=%s\n", json_object_get_string(my_string)); + printf("my_string.to_string()=%s\n", json_object_to_json_string(my_string)); + printf("my_string.to_string(NOSLASHESCAPE)=%s\n", json_object_to_json_string_ext(my_string, JSON_C_TO_STRING_NOSLASHESCAPE)); + json_object_put(my_string); + my_string = json_object_new_string("foo"); printf("my_string=%s\n", json_object_get_string(my_string)); printf("my_string.to_string()=%s\n", json_object_to_json_string(my_string)); diff --git a/tests/test1.expected b/tests/test1.expected index e36aaa4..c126a47 100644 --- a/tests/test1.expected +++ b/tests/test1.expected @@ -2,6 +2,9 @@ my_string= my_string.to_string()="\t" my_string=\ my_string.to_string()="\\" +my_string=/ +my_string.to_string()="\/" +my_string.to_string(NOSLASHESCAPE)="/" my_string=foo my_string.to_string()="foo" my_int=9 diff --git a/tests/test1Formatted_plain.expected b/tests/test1Formatted_plain.expected index edcc1b9..c011d37 100644 --- a/tests/test1Formatted_plain.expected +++ b/tests/test1Formatted_plain.expected @@ -2,6 +2,9 @@ my_string= my_string.to_string()="\t" my_string=\ my_string.to_string()="\\" +my_string=/ +my_string.to_string()="\/" +my_string.to_string(NOSLASHESCAPE)="/" my_string=foo my_string.to_string()="foo" my_int=9 diff --git a/tests/test1Formatted_pretty.expected b/tests/test1Formatted_pretty.expected index 95e48ed..b57a8e2 100644 --- a/tests/test1Formatted_pretty.expected +++ b/tests/test1Formatted_pretty.expected @@ -2,6 +2,9 @@ my_string= my_string.to_string()="\t" my_string=\ my_string.to_string()="\\" +my_string=/ +my_string.to_string()="\/" +my_string.to_string(NOSLASHESCAPE)="/" my_string=foo my_string.to_string()="foo" my_int=9 diff --git a/tests/test1Formatted_spaced.expected b/tests/test1Formatted_spaced.expected index e36aaa4..c126a47 100644 --- a/tests/test1Formatted_spaced.expected +++ b/tests/test1Formatted_spaced.expected @@ -2,6 +2,9 @@ my_string= my_string.to_string()="\t" my_string=\ my_string.to_string()="\\" +my_string=/ +my_string.to_string()="\/" +my_string.to_string(NOSLASHESCAPE)="/" my_string=foo my_string.to_string()="foo" my_int=9 diff --git a/tests/test_parse.c b/tests/test_parse.c index bc5389b..129fc06 100644 --- a/tests/test_parse.c +++ b/tests/test_parse.c @@ -226,6 +226,9 @@ struct incremental_step { { "\"\\n\"", -1, -1, json_tokener_success, 0 }, { "\"\\r\"", -1, -1, json_tokener_success, 0 }, { "\"\\t\"", -1, -1, json_tokener_success, 0 }, + { "\"\\/\"", -1, -1, json_tokener_success, 0 }, + // Escaping a forward slash is optional + { "\"/\"", -1, -1, json_tokener_success, 0 }, { "[1,2,3]", -1, -1, json_tokener_success, 0 }, diff --git a/tests/test_parse.expected b/tests/test_parse.expected index 6606260..249b936 100644 --- a/tests/test_parse.expected +++ b/tests/test_parse.expected @@ -61,10 +61,12 @@ json_tokener_parse_ex(tok, "\f" , 4) ... OK: got object of type [string json_tokener_parse_ex(tok, "\n" , 4) ... OK: got object of type [string]: "\n" json_tokener_parse_ex(tok, "\r" , 4) ... OK: got object of type [string]: "\r" json_tokener_parse_ex(tok, "\t" , 4) ... OK: got object of type [string]: "\t" +json_tokener_parse_ex(tok, "\/" , 4) ... OK: got object of type [string]: "\/" +json_tokener_parse_ex(tok, "/" , 3) ... OK: got object of type [string]: "\/" json_tokener_parse_ex(tok, [1,2,3] , 7) ... OK: got object of type [array]: [ 1, 2, 3 ] json_tokener_parse_ex(tok, [1,2,3,] , 8) ... OK: got object of type [array]: [ 1, 2, 3 ] json_tokener_parse_ex(tok, [1,2,,3,] , 9) ... OK: got correct error: unexpected character json_tokener_parse_ex(tok, [1,2,3,] , 8) ... OK: got correct error: unexpected character json_tokener_parse_ex(tok, {"a":1,} , 8) ... OK: got correct error: unexpected character -End Incremental Tests OK=30 ERROR=0 +End Incremental Tests OK=32 ERROR=0 ================================== -- 2.40.0