mirror of
https://github.com/json-c/json-c.git
synced 2026-04-11 16:29:06 +08:00
Fix issue #201: add a JSON_C_TO_STRING_NOSLASHESCAPE flag to turn off escaping of forward slashes.
This commit is contained in:
@@ -106,7 +106,7 @@ get_string_component(struct json_object *jso)
|
|||||||
|
|
||||||
/* string escaping */
|
/* 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;
|
int pos = 0, start_offset = 0;
|
||||||
unsigned char c;
|
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 == '\f') printbuf_memappend(pb, "\\f", 2);
|
||||||
else if(c == '"') printbuf_memappend(pb, "\\\"", 2);
|
else if(c == '"') printbuf_memappend(pb, "\\\"", 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);
|
else if(c == '/') printbuf_memappend(pb, "\\/", 2);
|
||||||
|
|
||||||
start_offset = ++pos;
|
start_offset = ++pos;
|
||||||
@@ -346,7 +352,7 @@ static int json_object_object_to_json_string(struct json_object* jso,
|
|||||||
sprintbuf(pb, " ");
|
sprintbuf(pb, " ");
|
||||||
indent(pb, level+1, flags);
|
indent(pb, level+1, flags);
|
||||||
sprintbuf(pb, "\"");
|
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)
|
if (flags & JSON_C_TO_STRING_SPACED)
|
||||||
sprintbuf(pb, "\": ");
|
sprintbuf(pb, "\": ");
|
||||||
else
|
else
|
||||||
@@ -773,7 +779,7 @@ static int json_object_string_to_json_string(struct json_object* jso,
|
|||||||
int flags)
|
int flags)
|
||||||
{
|
{
|
||||||
sprintbuf(pb, "\"");
|
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, "\"");
|
sprintbuf(pb, "\"");
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -63,6 +63,11 @@ extern "C" {
|
|||||||
*/
|
*/
|
||||||
#define JSON_C_TO_STRING_NOZERO (1<<2)
|
#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
|
* A flag for the json_object_object_add_ex function which
|
||||||
* causes the value to be added without a check if it already exists.
|
* causes the value to be added without a check if it already exists.
|
||||||
|
|||||||
@@ -57,6 +57,12 @@ int main(int argc, char **argv)
|
|||||||
printf("my_string.to_string()=%s\n", json_object_to_json_string(my_string));
|
printf("my_string.to_string()=%s\n", json_object_to_json_string(my_string));
|
||||||
json_object_put(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");
|
my_string = json_object_new_string("foo");
|
||||||
printf("my_string=%s\n", json_object_get_string(my_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()=%s\n", json_object_to_json_string(my_string));
|
||||||
|
|||||||
@@ -2,6 +2,9 @@ my_string=
|
|||||||
my_string.to_string()="\t"
|
my_string.to_string()="\t"
|
||||||
my_string=\
|
my_string=\
|
||||||
my_string.to_string()="\\"
|
my_string.to_string()="\\"
|
||||||
|
my_string=/
|
||||||
|
my_string.to_string()="\/"
|
||||||
|
my_string.to_string(NOSLASHESCAPE)="/"
|
||||||
my_string=foo
|
my_string=foo
|
||||||
my_string.to_string()="foo"
|
my_string.to_string()="foo"
|
||||||
my_int=9
|
my_int=9
|
||||||
|
|||||||
@@ -2,6 +2,9 @@ my_string=
|
|||||||
my_string.to_string()="\t"
|
my_string.to_string()="\t"
|
||||||
my_string=\
|
my_string=\
|
||||||
my_string.to_string()="\\"
|
my_string.to_string()="\\"
|
||||||
|
my_string=/
|
||||||
|
my_string.to_string()="\/"
|
||||||
|
my_string.to_string(NOSLASHESCAPE)="/"
|
||||||
my_string=foo
|
my_string=foo
|
||||||
my_string.to_string()="foo"
|
my_string.to_string()="foo"
|
||||||
my_int=9
|
my_int=9
|
||||||
|
|||||||
@@ -2,6 +2,9 @@ my_string=
|
|||||||
my_string.to_string()="\t"
|
my_string.to_string()="\t"
|
||||||
my_string=\
|
my_string=\
|
||||||
my_string.to_string()="\\"
|
my_string.to_string()="\\"
|
||||||
|
my_string=/
|
||||||
|
my_string.to_string()="\/"
|
||||||
|
my_string.to_string(NOSLASHESCAPE)="/"
|
||||||
my_string=foo
|
my_string=foo
|
||||||
my_string.to_string()="foo"
|
my_string.to_string()="foo"
|
||||||
my_int=9
|
my_int=9
|
||||||
|
|||||||
@@ -2,6 +2,9 @@ my_string=
|
|||||||
my_string.to_string()="\t"
|
my_string.to_string()="\t"
|
||||||
my_string=\
|
my_string=\
|
||||||
my_string.to_string()="\\"
|
my_string.to_string()="\\"
|
||||||
|
my_string=/
|
||||||
|
my_string.to_string()="\/"
|
||||||
|
my_string.to_string(NOSLASHESCAPE)="/"
|
||||||
my_string=foo
|
my_string=foo
|
||||||
my_string.to_string()="foo"
|
my_string.to_string()="foo"
|
||||||
my_int=9
|
my_int=9
|
||||||
|
|||||||
@@ -226,6 +226,9 @@ struct incremental_step {
|
|||||||
{ "\"\\n\"", -1, -1, json_tokener_success, 0 },
|
{ "\"\\n\"", -1, -1, json_tokener_success, 0 },
|
||||||
{ "\"\\r\"", -1, -1, json_tokener_success, 0 },
|
{ "\"\\r\"", -1, -1, json_tokener_success, 0 },
|
||||||
{ "\"\\t\"", -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 },
|
{ "[1,2,3]", -1, -1, json_tokener_success, 0 },
|
||||||
|
|
||||||
|
|||||||
@@ -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, "\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, "\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, "\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] , 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,] , 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,] , 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, [1,2,3,] , 8) ... OK: got correct error: unexpected character
|
||||||
json_tokener_parse_ex(tok, {"a":1,} , 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
|
||||||
==================================
|
==================================
|
||||||
|
|||||||
Reference in New Issue
Block a user