]> git.ipfire.org Git - thirdparty/json-c.git/commitdiff
When serializing with JSON_C_TO_STRING_PRETTY set, keep the opening and closing curly...
authorEric Haszlakiewicz <erh+git@nimenees.com>
Sat, 30 Jul 2022 19:27:14 +0000 (19:27 +0000)
committerEric Haszlakiewicz <erh+git@nimenees.com>
Sat, 30 Jul 2022 19:27:14 +0000 (19:27 +0000)
ChangeLog
json_object.c
tests/test1.c
tests/test1.expected
tests/test1Formatted_plain.expected
tests/test1Formatted_pretty.expected
tests/test1Formatted_spaced.expected
tests/test1Formatted_spaced_pretty.expected
tests/test1Formatted_spaced_pretty_pretty_tab.expected

index b03a77710e9af018fccea42b7c5f8aa2c79faa94..8cadae62623f1ed3e14af729bf5ac5cceeeab8d2 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,4 +1,21 @@
 
+0.17 (future release)
+========================================
+
+Deprecated and removed features:
+--------------------------------
+* ...
+
+New features
+------------
+* ...
+
+Significant changes and bug fixes
+---------------------------------
+* When serializing with JSON_C_TO_STRING_PRETTY set, keep the opening and 
+  closing curly or square braces on same line for empty objects or arrays.
+
+
 0.16 (up to commit 66dcdf5, 2022-04-13)
 ========================================
 
index 581b1e274810b2de7a98711d863003d6f554643f..14cd594446e352000ffe909c5b6275443a3dbc0e 100644 (file)
@@ -460,16 +460,14 @@ static int json_object_object_to_json_string(struct json_object *jso, struct pri
        struct json_object_iter iter;
 
        printbuf_strappend(pb, "{" /*}*/);
-       if (flags & JSON_C_TO_STRING_PRETTY)
-               printbuf_strappend(pb, "\n");
        json_object_object_foreachC(jso, iter)
        {
                if (had_children)
                {
                        printbuf_strappend(pb, ",");
-                       if (flags & JSON_C_TO_STRING_PRETTY)
-                               printbuf_strappend(pb, "\n");
                }
+               if (flags & JSON_C_TO_STRING_PRETTY)
+                       printbuf_strappend(pb, "\n");
                had_children = 1;
                if (flags & JSON_C_TO_STRING_SPACED && !(flags & JSON_C_TO_STRING_PRETTY))
                        printbuf_strappend(pb, " ");
@@ -485,10 +483,9 @@ static int json_object_object_to_json_string(struct json_object *jso, struct pri
                else if (iter.val->_to_json_string(iter.val, pb, level + 1, flags) < 0)
                        return -1;
        }
-       if (flags & JSON_C_TO_STRING_PRETTY)
+       if ((flags & JSON_C_TO_STRING_PRETTY) && had_children)
        {
-               if (had_children)
-                       printbuf_strappend(pb, "\n");
+               printbuf_strappend(pb, "\n");
                indent(pb, level, flags);
        }
        if (flags & JSON_C_TO_STRING_SPACED && !(flags & JSON_C_TO_STRING_PRETTY))
@@ -1381,17 +1378,15 @@ static int json_object_array_to_json_string(struct json_object *jso, struct prin
        size_t ii;
 
        printbuf_strappend(pb, "[");
-       if (flags & JSON_C_TO_STRING_PRETTY)
-               printbuf_strappend(pb, "\n");
        for (ii = 0; ii < json_object_array_length(jso); ii++)
        {
                struct json_object *val;
                if (had_children)
                {
                        printbuf_strappend(pb, ",");
-                       if (flags & JSON_C_TO_STRING_PRETTY)
-                               printbuf_strappend(pb, "\n");
                }
+               if (flags & JSON_C_TO_STRING_PRETTY)
+                       printbuf_strappend(pb, "\n");
                had_children = 1;
                if (flags & JSON_C_TO_STRING_SPACED && !(flags & JSON_C_TO_STRING_PRETTY))
                        printbuf_strappend(pb, " ");
@@ -1402,10 +1397,9 @@ static int json_object_array_to_json_string(struct json_object *jso, struct prin
                else if (val->_to_json_string(val, pb, level + 1, flags) < 0)
                        return -1;
        }
-       if (flags & JSON_C_TO_STRING_PRETTY)
+       if ((flags & JSON_C_TO_STRING_PRETTY) && had_children)
        {
-               if (had_children)
-                       printbuf_strappend(pb, "\n");
+               printbuf_strappend(pb, "\n");
                indent(pb, level, flags);
        }
 
index 01796ab3ee50a84f9dbce7c9fb83a9a26b514285..befd246cf18214580d3737e2464eb0143bbb8172 100644 (file)
@@ -311,6 +311,11 @@ int main(int argc, char **argv)
        {
                printf("\t%s: %s\n", key, json_object_to_json_string(val));
        }
+
+       json_object *empty_array = json_object_new_array();
+       json_object *empty_obj = json_object_new_object();
+       json_object_object_add(my_object, "empty_array", empty_array);
+       json_object_object_add(my_object, "empty_obj", empty_obj);
        printf("my_object.to_string()=%s\n", json_object_to_json_string(my_object));
 
        json_object_put(my_array);
index 4b6b25225a602a74da47ecb1494c31e37a399944..b473a8b4d6b2a8986969c2e73886228f5077089c 100644 (file)
@@ -75,4 +75,4 @@ my_object=
        foo: "bar"
        bool0: false
        bool1: true
-my_object.to_string()={ "abc": 12, "foo": "bar", "bool0": false, "bool1": true }
+my_object.to_string()={ "abc": 12, "foo": "bar", "bool0": false, "bool1": true, "empty_array": [ ], "empty_obj": { } }
index 128e2743ebb9d97f22640caf7397ec48a27253bd..ad12d9917d126c415a02a8f0a7c7f29397935150 100644 (file)
@@ -75,4 +75,4 @@ my_object=
        foo: "bar"
        bool0: false
        bool1: true
-my_object.to_string()={"abc":12,"foo":"bar","bool0":false,"bool1":true}
+my_object.to_string()={"abc":12,"foo":"bar","bool0":false,"bool1":true,"empty_array":[],"empty_obj":{}}
index b67185fd5aeb780be80dcb3bbdbf2ea24787a29e..ab2cc78405896285ba8d7e6fd38a9671896791de 100644 (file)
@@ -97,5 +97,7 @@ my_object.to_string()={
   "abc":12,
   "foo":"bar",
   "bool0":false,
-  "bool1":true
+  "bool1":true,
+  "empty_array":[],
+  "empty_obj":{}
 }
index fe6979d404b92b658e2ff049e1296d506d5980e7..f57bd05c0527a1e2b8b863ffccd8194f6f355f8d 100644 (file)
@@ -75,4 +75,4 @@ my_object=
        foo: "bar"
        bool0: false
        bool1: true
-my_object.to_string()={ "abc": 12, "foo": "bar", "bool0": false, "bool1": true }
+my_object.to_string()={ "abc": 12, "foo": "bar", "bool0": false, "bool1": true, "empty_array": [ ], "empty_obj": { } }
index 104a554fc2b5c79c0077a39730477bc36b85618b..c88729fabca4bbb55501d7a4e8f517d3a3566654 100644 (file)
@@ -97,5 +97,7 @@ my_object.to_string()={
   "abc": 12,
   "foo": "bar",
   "bool0": false,
-  "bool1": true
+  "bool1": true,
+  "empty_array": [],
+  "empty_obj": {}
 }
index f9a8e876fd4fa1659dc6fd56d9a61b5bf5eee6c2..bab239ff8462659a02fb53ece660eeecffddcc0e 100644 (file)
@@ -97,5 +97,7 @@ my_object.to_string()={
        "abc": 12,
        "foo": "bar",
        "bool0": false,
-       "bool1": true
+       "bool1": true,
+       "empty_array": [],
+       "empty_obj": {}
 }