/* 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;
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;
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
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;
}
*/
#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.
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));
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
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
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
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
{ "\"\\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 },
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
==================================