]> granicus.if.org Git - json-c/commitdiff
Fix issue #221: JSON_C_TO_STRING_NOSLASHESCAPE works incorrectly
authorchenha0 <gary.haochen@gmail.com>
Thu, 21 Jan 2016 12:11:40 +0000 (20:11 +0800)
committerchenha0 <gary.haochen@gmail.com>
Thu, 21 Jan 2016 12:12:48 +0000 (20:12 +0800)
Tests added.

json_object.c
tests/test1.c
tests/test1.expected
tests/test1Formatted_plain.expected
tests/test1Formatted_pretty.expected
tests/test1Formatted_spaced.expected

index e611103ee4808fa3090ac61c2f94d1c0f4dbee93..3ee0817ffb40ebccdc9e9e4856bcf28c7f78cad5 100644 (file)
@@ -123,6 +123,12 @@ static int json_escape_str(struct printbuf *pb, const char *str, int len, int fl
                case '"':
                case '\\':
                case '/':
+                       if((flags & JSON_C_TO_STRING_NOSLASHESCAPE) && c == '/')
+                       {
+                               pos++;
+                               break;
+                       }
+
                        if(pos - start_offset > 0)
                                printbuf_memappend(pb, str + start_offset, pos - start_offset);
 
@@ -133,12 +139,6 @@ static int json_escape_str(struct printbuf *pb, const char *str, int len, int fl
                        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;
index 180dedb8abf33447d4243d6c933ae430613da697..c047c30adc49cc740e300ad0f6df2bd2172f946b 100644 (file)
@@ -63,6 +63,12 @@ int main(int argc, char **argv)
        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/bar/baz");
+       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 c126a475b703a2b3ab496aaac9fe9f1c1fb92d04..e9be1f28dda8ad5a9bf67a6d488739508bc6fe58 100644 (file)
@@ -5,6 +5,9 @@ my_string.to_string()="\\"
 my_string=/
 my_string.to_string()="\/"
 my_string.to_string(NOSLASHESCAPE)="/"
+my_string=/foo/bar/baz
+my_string.to_string()="\/foo\/bar\/baz"
+my_string.to_string(NOSLASHESCAPE)="/foo/bar/baz"
 my_string=foo
 my_string.to_string()="foo"
 my_int=9
index c011d373ab8fc6c7e2e75a4e8d1f38aa11deee1d..6b93eb729fae5ff110cc46c27827a69cbddca99d 100644 (file)
@@ -5,6 +5,9 @@ my_string.to_string()="\\"
 my_string=/
 my_string.to_string()="\/"
 my_string.to_string(NOSLASHESCAPE)="/"
+my_string=/foo/bar/baz
+my_string.to_string()="\/foo\/bar\/baz"
+my_string.to_string(NOSLASHESCAPE)="/foo/bar/baz"
 my_string=foo
 my_string.to_string()="foo"
 my_int=9
index b57a8e2f343d7d529bacf2ec718cf91da98adb29..0f94b4c119cdb72c62aeba11a78474e6ea6ca9f8 100644 (file)
@@ -5,6 +5,9 @@ my_string.to_string()="\\"
 my_string=/
 my_string.to_string()="\/"
 my_string.to_string(NOSLASHESCAPE)="/"
+my_string=/foo/bar/baz
+my_string.to_string()="\/foo\/bar\/baz"
+my_string.to_string(NOSLASHESCAPE)="/foo/bar/baz"
 my_string=foo
 my_string.to_string()="foo"
 my_int=9
index c126a475b703a2b3ab496aaac9fe9f1c1fb92d04..e9be1f28dda8ad5a9bf67a6d488739508bc6fe58 100644 (file)
@@ -5,6 +5,9 @@ my_string.to_string()="\\"
 my_string=/
 my_string.to_string()="\/"
 my_string.to_string(NOSLASHESCAPE)="/"
+my_string=/foo/bar/baz
+my_string.to_string()="\/foo\/bar\/baz"
+my_string.to_string(NOSLASHESCAPE)="/foo/bar/baz"
 my_string=foo
 my_string.to_string()="foo"
 my_int=9