]> granicus.if.org Git - json-c/commitdiff
Fix issue #201: add a JSON_C_TO_STRING_NOSLASHESCAPE flag to turn off escaping of...
authorEric Haszlakiewicz <erh+git@nimenees.com>
Sun, 29 Nov 2015 02:00:30 +0000 (20:00 -0600)
committerEric Haszlakiewicz <erh+git@nimenees.com>
Sun, 29 Nov 2015 02:00:30 +0000 (20:00 -0600)
json_object.c
json_object.h
tests/test1.c
tests/test1.expected
tests/test1Formatted_plain.expected
tests/test1Formatted_pretty.expected
tests/test1Formatted_spaced.expected
tests/test_parse.c
tests/test_parse.expected

index 9ac22a30f08f4dd58cc315c7f396ff7c9f3d6e82..5a758d6502fd1ff3edc183fc507670663b6e0dd5 100644 (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;
 }
index 67137d87125b16587a40956744d89af3be1faf54..695101984c82d2bb685de1a8a53a8f28616bf737 100644 (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.
index 9e5bb63dcf6a8da31f9baddd92871cdf2998d8ed..180dedb8abf33447d4243d6c933ae430613da697 100644 (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));
index e36aaa4c4f43630d82f505feb212fd2cf9190cb8..c126a475b703a2b3ab496aaac9fe9f1c1fb92d04 100644 (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
index edcc1b932812da7aa788c4b6815515ba9e56a18e..c011d373ab8fc6c7e2e75a4e8d1f38aa11deee1d 100644 (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
index 95e48ed0102a8b2fd82864af0731c3520f0d510c..b57a8e2f343d7d529bacf2ec718cf91da98adb29 100644 (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
index e36aaa4c4f43630d82f505feb212fd2cf9190cb8..c126a475b703a2b3ab496aaac9fe9f1c1fb92d04 100644 (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
index bc5389b44362446f8ebf073cf25f25fe02f3710a..129fc06c3c00e841436f81dc10973f839c41d873 100644 (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 },
 
index 660626012f4bf98dd87030988bb9fc58d62e2109..249b93695d6f8d9639ffefedff1fc8d5cdfb5b4a 100644 (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
 ==================================