Browse Source

Merge pull request #453 from darjankrijan/master

Fixed misalignment in JSON string due to space after \n being printed...
tags/json-c-0.14-20200419
Eric Haszlakiewicz GitHub 7 years ago
parent
commit
ee4691c9fb
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 255 additions and 4 deletions
  1. +4
    -4
      json_object.c
  2. +1
    -0
      tests/parse_flags.c
  3. +16
    -0
      tests/test1.test
  4. +86
    -0
      tests/test1Formatted_spaced_pretty.expected
  5. +86
    -0
      tests/test1Formatted_spaced_pretty_pretty_tab.expected
  6. +16
    -0
      tests/test2.test
  7. +23
    -0
      tests/test2Formatted_spaced_pretty.expected
  8. +23
    -0
      tests/test2Formatted_spaced_pretty_pretty_tab.expected

+ 4
- 4
json_object.c View File

@@ -395,7 +395,7 @@ static int json_object_object_to_json_string(struct json_object* jso,
printbuf_strappend(pb, "\n"); printbuf_strappend(pb, "\n");
} }
had_children = 1; had_children = 1;
if (flags & JSON_C_TO_STRING_SPACED)
if (flags & JSON_C_TO_STRING_SPACED && !(flags & JSON_C_TO_STRING_PRETTY))
printbuf_strappend(pb, " "); printbuf_strappend(pb, " ");
indent(pb, level+1, flags); indent(pb, level+1, flags);
printbuf_strappend(pb, "\""); printbuf_strappend(pb, "\"");
@@ -416,7 +416,7 @@ static int json_object_object_to_json_string(struct json_object* jso,
printbuf_strappend(pb, "\n"); printbuf_strappend(pb, "\n");
indent(pb,level,flags); indent(pb,level,flags);
} }
if (flags & JSON_C_TO_STRING_SPACED)
if (flags & JSON_C_TO_STRING_SPACED && !(flags & JSON_C_TO_STRING_PRETTY))
return printbuf_strappend(pb, /*{*/ " }"); return printbuf_strappend(pb, /*{*/ " }");
else else
return printbuf_strappend(pb, /*{*/ "}"); return printbuf_strappend(pb, /*{*/ "}");
@@ -1134,7 +1134,7 @@ static int json_object_array_to_json_string(struct json_object* jso,
printbuf_strappend(pb, "\n"); printbuf_strappend(pb, "\n");
} }
had_children = 1; had_children = 1;
if (flags & JSON_C_TO_STRING_SPACED)
if (flags & JSON_C_TO_STRING_SPACED && !(flags&JSON_C_TO_STRING_PRETTY))
printbuf_strappend(pb, " "); printbuf_strappend(pb, " ");
indent(pb, level + 1, flags); indent(pb, level + 1, flags);
val = json_object_array_get_idx(jso, ii); val = json_object_array_get_idx(jso, ii);
@@ -1151,7 +1151,7 @@ static int json_object_array_to_json_string(struct json_object* jso,
indent(pb,level,flags); indent(pb,level,flags);
} }


if (flags & JSON_C_TO_STRING_SPACED)
if (flags & JSON_C_TO_STRING_SPACED && !(flags&JSON_C_TO_STRING_PRETTY))
return printbuf_strappend(pb, " ]"); return printbuf_strappend(pb, " ]");
return printbuf_strappend(pb, "]"); return printbuf_strappend(pb, "]");
} }


+ 1
- 0
tests/parse_flags.c View File

@@ -19,6 +19,7 @@ static struct {
{ "plain", JSON_C_TO_STRING_PLAIN }, { "plain", JSON_C_TO_STRING_PLAIN },
{ "spaced", JSON_C_TO_STRING_SPACED }, { "spaced", JSON_C_TO_STRING_SPACED },
{ "pretty", JSON_C_TO_STRING_PRETTY }, { "pretty", JSON_C_TO_STRING_PRETTY },
{ "pretty_tab", JSON_C_TO_STRING_PRETTY_TAB },
}; };


#ifndef NELEM #ifndef NELEM


+ 16
- 0
tests/test1.test View File

@@ -19,4 +19,20 @@ for flag in plain spaced pretty ; do
fi fi
done done


# Spaced and pretty JSON string
run_output_test -o test1Formatted_spaced_pretty \
test1Formatted spaced pretty
_err2=$?
if [ $_err -eq 0 ] ; then
_err=$_err2
fi

# Spaced and pretty JSON string using tabs
run_output_test -o test1Formatted_spaced_pretty_pretty_tab \
test1Formatted spaced pretty pretty_tab
_err2=$?
if [ $_err -eq 0 ] ; then
_err=$_err2
fi

exit $_err exit $_err

+ 86
- 0
tests/test1Formatted_spaced_pretty.expected View File

@@ -0,0 +1,86 @@
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/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
my_int.to_string()=9
my_array=
[0]=1
[1]=2
[2]=3
[3]=null
[4]=5
my_array.to_string()=[
1,
2,
3,
null,
5
]
my_array=
[0]=1
[1]=2
[2]=3
[3]=4
[4]=5
[5]=null
[6]=7
my_array.to_string()=[1,2,3,4,5,null,7]
after del_idx(0,1)=0, my_array.to_string()=[2,3,4,5,null,7]
after del_idx(0,1)=0, my_array.to_string()=[3,4,5,null,7]
after del_idx(0,1)=0, my_array.to_string()=[4,5,null,7]
after del_idx(0,1)=0, my_array.to_string()=[5,null,7]
after del_idx(0,1)=0, my_array.to_string()=[null,7]
after del_idx(0,1)=0, my_array.to_string()=[7]
after del_idx(0,1)=0, my_array.to_string()=[]
after del_idx(0,1)=-1, my_array.to_string()=[]
after del_idx(0,7)=0, my_array.to_string()=[]
after del_idx(0,8)=-1, my_array.to_string()=[1,2,3,4,5,null,7]
after del_idx(0,6)=0, my_array.to_string()=[7]
after adding more entries, my_array.to_string()=[7,"s1","s2","s3"]
my_array=
[0]=3
[1]=1
[2]=2
[3]=null
[4]=0
my_array.to_string()=[
3,
1,
2,
null,
0
]
my_array=
[0]=null
[1]=0
[2]=1
[3]=2
[4]=3
my_array.to_string()=[
null,
0,
1,
2,
3
]
baz_obj.to_string()="fark"
my_object=
abc: 12
foo: "bar"
bool0: false
bool1: true
my_object.to_string()={
"abc": 12,
"foo": "bar",
"bool0": false,
"bool1": true
}

+ 86
- 0
tests/test1Formatted_spaced_pretty_pretty_tab.expected View File

@@ -0,0 +1,86 @@
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/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
my_int.to_string()=9
my_array=
[0]=1
[1]=2
[2]=3
[3]=null
[4]=5
my_array.to_string()=[
1,
2,
3,
null,
5
]
my_array=
[0]=1
[1]=2
[2]=3
[3]=4
[4]=5
[5]=null
[6]=7
my_array.to_string()=[1,2,3,4,5,null,7]
after del_idx(0,1)=0, my_array.to_string()=[2,3,4,5,null,7]
after del_idx(0,1)=0, my_array.to_string()=[3,4,5,null,7]
after del_idx(0,1)=0, my_array.to_string()=[4,5,null,7]
after del_idx(0,1)=0, my_array.to_string()=[5,null,7]
after del_idx(0,1)=0, my_array.to_string()=[null,7]
after del_idx(0,1)=0, my_array.to_string()=[7]
after del_idx(0,1)=0, my_array.to_string()=[]
after del_idx(0,1)=-1, my_array.to_string()=[]
after del_idx(0,7)=0, my_array.to_string()=[]
after del_idx(0,8)=-1, my_array.to_string()=[1,2,3,4,5,null,7]
after del_idx(0,6)=0, my_array.to_string()=[7]
after adding more entries, my_array.to_string()=[7,"s1","s2","s3"]
my_array=
[0]=3
[1]=1
[2]=2
[3]=null
[4]=0
my_array.to_string()=[
3,
1,
2,
null,
0
]
my_array=
[0]=null
[1]=0
[2]=1
[3]=2
[4]=3
my_array.to_string()=[
null,
0,
1,
2,
3
]
baz_obj.to_string()="fark"
my_object=
abc: 12
foo: "bar"
bool0: false
bool1: true
my_object.to_string()={
"abc": 12,
"foo": "bar",
"bool0": false,
"bool1": true
}

+ 16
- 0
tests/test2.test View File

@@ -19,4 +19,20 @@ for flag in plain spaced pretty ; do
fi fi
done done


# Spaced and pretty JSON string
run_output_test -o test2Formatted_spaced_pretty \
test2Formatted spaced pretty
_err2=$?
if [ $_err -eq 0 ] ; then
_err=$_err2
fi

# Spaced and pretty JSON string using tabs
run_output_test -o test2Formatted_spaced_pretty_pretty_tab \
test2Formatted spaced pretty pretty_tab
_err2=$?
if [ $_err -eq 0 ] ; then
_err=$_err2
fi

exit $_err exit $_err

+ 23
- 0
tests/test2Formatted_spaced_pretty.expected View File

@@ -0,0 +1,23 @@
new_obj.to_string()={
"glossary": {
"title": "example glossary",
"GlossDiv": {
"title": "S",
"GlossList": [
{
"ID": "SGML",
"SortAs": "SGML",
"GlossTerm": "Standard Generalized Markup Language",
"Acronym": "SGML",
"Abbrev": "ISO 8879:1986",
"GlossDef": "A meta-markup language, used to create markup languages such as DocBook.",
"GlossSeeAlso": [
"GML",
"XML",
"markup"
]
}
]
}
}
}

+ 23
- 0
tests/test2Formatted_spaced_pretty_pretty_tab.expected View File

@@ -0,0 +1,23 @@
new_obj.to_string()={
"glossary": {
"title": "example glossary",
"GlossDiv": {
"title": "S",
"GlossList": [
{
"ID": "SGML",
"SortAs": "SGML",
"GlossTerm": "Standard Generalized Markup Language",
"Acronym": "SGML",
"Abbrev": "ISO 8879:1986",
"GlossDef": "A meta-markup language, used to create markup languages such as DocBook.",
"GlossSeeAlso": [
"GML",
"XML",
"markup"
]
}
]
}
}
}

Loading…
Cancel
Save