From e8cec5c9e47676ebc51f1ab0af904fc4417b2166 Mon Sep 17 00:00:00 2001 From: Darjan Krijan Date: Tue, 20 Nov 2018 22:21:27 +0100 Subject: [PATCH 1/4] Fixed misalignment in JSON string due to space after \n being printed when choosing JSON_C_TO_STRING_SPACED together with JSON_C_TO_STRING_PRETTY in json_object_array_to_json_string --- json_object.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/json_object.c b/json_object.c index 8a86bc6..19f9c83 100644 --- a/json_object.c +++ b/json_object.c @@ -395,7 +395,7 @@ static int json_object_object_to_json_string(struct json_object* jso, printbuf_strappend(pb, "\n"); } 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, " "); indent(pb, level+1, flags); printbuf_strappend(pb, "\""); @@ -416,7 +416,7 @@ static int json_object_object_to_json_string(struct json_object* jso, printbuf_strappend(pb, "\n"); 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, /*{*/ " }"); else return printbuf_strappend(pb, /*{*/ "}"); From 3943960874e92de9bbf9975b97265184547bf1b8 Mon Sep 17 00:00:00 2001 From: Darjan Krijan Date: Wed, 21 Nov 2018 22:34:01 +0100 Subject: [PATCH 2/4] Removed spaces after \n for arrays as well --- json_object.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/json_object.c b/json_object.c index 19f9c83..39ac236 100644 --- a/json_object.c +++ b/json_object.c @@ -1127,7 +1127,7 @@ static int json_object_array_to_json_string(struct json_object* jso, printbuf_strappend(pb, "\n"); } 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, " "); indent(pb, level + 1, flags); val = json_object_array_get_idx(jso, ii); @@ -1144,7 +1144,7 @@ static int json_object_array_to_json_string(struct json_object* jso, 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, "]"); } From b0bceaa8bf3f687d78b88dbae694ae67a3095cba Mon Sep 17 00:00:00 2001 From: Darjan Krijan Date: Wed, 21 Nov 2018 22:41:13 +0100 Subject: [PATCH 3/4] Added a test for the space after \n issue with flags=JSON_C_TO_STRING_SPACED|JSON_C_TO_STRING_PRETTY|(JSON_C_TO_STRING_PRETTY_TAB) used in json_object_array_to_json_string --- tests/Makefile.am | 1 + tests/test_string_spaced_pretty.c | 34 +++++++++++++++ tests/test_string_spaced_pretty.expected | 55 ++++++++++++++++++++++++ tests/test_string_spaced_pretty.test | 1 + 4 files changed, 91 insertions(+) create mode 100644 tests/test_string_spaced_pretty.c create mode 100644 tests/test_string_spaced_pretty.expected create mode 120000 tests/test_string_spaced_pretty.test diff --git a/tests/Makefile.am b/tests/Makefile.am index ea57bda..80ae468 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -27,6 +27,7 @@ TESTS+= test_set_value.test TESTS+= test_visit.test TESTS+= test_json_pointer.test TESTS+= test_int_add.test +TESTS+= test_string_spaced_pretty.test check_PROGRAMS= check_PROGRAMS += $(TESTS:.test=) diff --git a/tests/test_string_spaced_pretty.c b/tests/test_string_spaced_pretty.c new file mode 100644 index 0000000..59da6a7 --- /dev/null +++ b/tests/test_string_spaced_pretty.c @@ -0,0 +1,34 @@ +#include +#include +#include +#include +#include + +#include "json.h" + +int main(int argc, char **argv) +{ + json_object *j; + + j = json_tokener_parse("/* more difficult test case */" + "{ \"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\"] } ] } } }"); + + printf("flags = 0:\n%s\n\n", + json_object_to_json_string_ext(j, 0)); + + printf("flags = JSON_C_TO_STRING_SPACED:\n%s\n\n", + json_object_to_json_string_ext(j, JSON_C_TO_STRING_SPACED)); + + printf("flags = JSON_C_TO_STRING_SPACED|JSON_C_TO_STRING_PRETTY:\n%s\n\n", + json_object_to_json_string_ext(j, JSON_C_TO_STRING_SPACED + |JSON_C_TO_STRING_PRETTY)); + + printf("flags = JSON_C_TO_STRING_SPACED|JSON_C_TO_STRING_PRETTY|JSON_C_TO_STRING_PRETTY_TAB:\n%s\n", + json_object_to_json_string_ext(j, JSON_C_TO_STRING_SPACED + |JSON_C_TO_STRING_PRETTY + |JSON_C_TO_STRING_PRETTY_TAB)); + + json_object_put(j); + + return EXIT_SUCCESS; +} diff --git a/tests/test_string_spaced_pretty.expected b/tests/test_string_spaced_pretty.expected new file mode 100644 index 0000000..193002e --- /dev/null +++ b/tests/test_string_spaced_pretty.expected @@ -0,0 +1,55 @@ +flags = 0: +{"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"]}]}}} + +flags = JSON_C_TO_STRING_SPACED: +{ "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" ] } ] } } } + +flags = JSON_C_TO_STRING_SPACED|JSON_C_TO_STRING_PRETTY: +{ + "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" + ] + } + ] + } + } +} + +flags = JSON_C_TO_STRING_SPACED|JSON_C_TO_STRING_PRETTY|JSON_C_TO_STRING_PRETTY_TAB: +{ + "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" + ] + } + ] + } + } +} diff --git a/tests/test_string_spaced_pretty.test b/tests/test_string_spaced_pretty.test new file mode 120000 index 0000000..58a13f4 --- /dev/null +++ b/tests/test_string_spaced_pretty.test @@ -0,0 +1 @@ +test_basic.test \ No newline at end of file From 7a9075c16b21322afb6d93533ba9f22e03673314 Mon Sep 17 00:00:00 2001 From: Darjan Krijan Date: Fri, 7 Dec 2018 17:30:15 +0100 Subject: [PATCH 4/4] Added test cases for spaced pretty and pretty_tab used together --- tests/parse_flags.c | 1 + tests/test1.test | 16 ++++ tests/test1Formatted_spaced_pretty.expected | 86 +++++++++++++++++++ ...ormatted_spaced_pretty_pretty_tab.expected | 86 +++++++++++++++++++ tests/test2.test | 16 ++++ tests/test2Formatted_spaced_pretty.expected | 23 +++++ ...ormatted_spaced_pretty_pretty_tab.expected | 23 +++++ 7 files changed, 251 insertions(+) create mode 100644 tests/test1Formatted_spaced_pretty.expected create mode 100644 tests/test1Formatted_spaced_pretty_pretty_tab.expected create mode 100644 tests/test2Formatted_spaced_pretty.expected create mode 100644 tests/test2Formatted_spaced_pretty_pretty_tab.expected diff --git a/tests/parse_flags.c b/tests/parse_flags.c index b1f8337..17cd061 100644 --- a/tests/parse_flags.c +++ b/tests/parse_flags.c @@ -19,6 +19,7 @@ static struct { { "plain", JSON_C_TO_STRING_PLAIN }, { "spaced", JSON_C_TO_STRING_SPACED }, { "pretty", JSON_C_TO_STRING_PRETTY }, + { "pretty_tab", JSON_C_TO_STRING_PRETTY_TAB }, }; #ifndef NELEM diff --git a/tests/test1.test b/tests/test1.test index 79d2e09..0331f98 100755 --- a/tests/test1.test +++ b/tests/test1.test @@ -19,4 +19,20 @@ for flag in plain spaced pretty ; do fi 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 diff --git a/tests/test1Formatted_spaced_pretty.expected b/tests/test1Formatted_spaced_pretty.expected new file mode 100644 index 0000000..376da12 --- /dev/null +++ b/tests/test1Formatted_spaced_pretty.expected @@ -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 +} diff --git a/tests/test1Formatted_spaced_pretty_pretty_tab.expected b/tests/test1Formatted_spaced_pretty_pretty_tab.expected new file mode 100644 index 0000000..d237669 --- /dev/null +++ b/tests/test1Formatted_spaced_pretty_pretty_tab.expected @@ -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 +} diff --git a/tests/test2.test b/tests/test2.test index d4a4e79..b5330fe 100755 --- a/tests/test2.test +++ b/tests/test2.test @@ -19,4 +19,20 @@ for flag in plain spaced pretty ; do fi 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 diff --git a/tests/test2Formatted_spaced_pretty.expected b/tests/test2Formatted_spaced_pretty.expected new file mode 100644 index 0000000..49f9ef2 --- /dev/null +++ b/tests/test2Formatted_spaced_pretty.expected @@ -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" + ] + } + ] + } + } +} diff --git a/tests/test2Formatted_spaced_pretty_pretty_tab.expected b/tests/test2Formatted_spaced_pretty_pretty_tab.expected new file mode 100644 index 0000000..0296b9c --- /dev/null +++ b/tests/test2Formatted_spaced_pretty_pretty_tab.expected @@ -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" + ] + } + ] + } + } +}