Browse Source

Fix issue #221: JSON_C_TO_STRING_NOSLASHESCAPE works incorrectly

Tests added.
tags/json-c-0.13-20171207
chenha0 9 years ago
parent
commit
dffdee966f
6 changed files with 24 additions and 6 deletions
  1. +6
    -6
      json_object.c
  2. +6
    -0
      tests/test1.c
  3. +3
    -0
      tests/test1.expected
  4. +3
    -0
      tests/test1Formatted_plain.expected
  5. +3
    -0
      tests/test1Formatted_pretty.expected
  6. +3
    -0
      tests/test1Formatted_spaced.expected

+ 6
- 6
json_object.c View File

@@ -123,6 +123,12 @@ static int json_escape_str(struct printbuf *pb, const char *str, int len, int fl
case '"': case '"':
case '\\': case '\\':
case '/': case '/':
if((flags & JSON_C_TO_STRING_NOSLASHESCAPE) && c == '/')
{
pos++;
break;
}

if(pos - start_offset > 0) if(pos - start_offset > 0)
printbuf_memappend(pb, str + start_offset, pos - start_offset); 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 == '\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;


+ 6
- 0
tests/test1.c View 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)); 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); 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"); 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));


+ 3
- 0
tests/test1.expected View File

@@ -5,6 +5,9 @@ my_string.to_string()="\\"
my_string=/ my_string=/
my_string.to_string()="\/" my_string.to_string()="\/"
my_string.to_string(NOSLASHESCAPE)="/" 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=foo
my_string.to_string()="foo" my_string.to_string()="foo"
my_int=9 my_int=9


+ 3
- 0
tests/test1Formatted_plain.expected View File

@@ -5,6 +5,9 @@ my_string.to_string()="\\"
my_string=/ my_string=/
my_string.to_string()="\/" my_string.to_string()="\/"
my_string.to_string(NOSLASHESCAPE)="/" 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=foo
my_string.to_string()="foo" my_string.to_string()="foo"
my_int=9 my_int=9


+ 3
- 0
tests/test1Formatted_pretty.expected View File

@@ -5,6 +5,9 @@ my_string.to_string()="\\"
my_string=/ my_string=/
my_string.to_string()="\/" my_string.to_string()="\/"
my_string.to_string(NOSLASHESCAPE)="/" 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=foo
my_string.to_string()="foo" my_string.to_string()="foo"
my_int=9 my_int=9


+ 3
- 0
tests/test1Formatted_spaced.expected View File

@@ -5,6 +5,9 @@ my_string.to_string()="\\"
my_string=/ my_string=/
my_string.to_string()="\/" my_string.to_string()="\/"
my_string.to_string(NOSLASHESCAPE)="/" 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=foo
my_string.to_string()="foo" my_string.to_string()="foo"
my_int=9 my_int=9


Loading…
Cancel
Save