Fix issue #201: add a JSON_C_TO_STRING_NOSLASHESCAPE flag to turn off escaping of forward slashes.

This commit is contained in:
Eric Haszlakiewicz
2015-11-28 20:00:30 -06:00
parent 5a6a378725
commit 316da85818
9 changed files with 38 additions and 4 deletions

View File

@@ -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;
}

View File

@@ -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.

View File

@@ -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));

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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 },

View File

@@ -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
==================================