mirror of
https://github.com/json-c/json-c.git
synced 2026-04-05 05:19:07 +08:00
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. Issue #778.
This commit is contained in:
17
ChangeLog
17
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)
|
0.16 (up to commit 66dcdf5, 2022-04-13)
|
||||||
========================================
|
========================================
|
||||||
|
|
||||||
|
|||||||
@@ -460,16 +460,14 @@ static int json_object_object_to_json_string(struct json_object *jso, struct pri
|
|||||||
struct json_object_iter iter;
|
struct json_object_iter iter;
|
||||||
|
|
||||||
printbuf_strappend(pb, "{" /*}*/);
|
printbuf_strappend(pb, "{" /*}*/);
|
||||||
if (flags & JSON_C_TO_STRING_PRETTY)
|
|
||||||
printbuf_strappend(pb, "\n");
|
|
||||||
json_object_object_foreachC(jso, iter)
|
json_object_object_foreachC(jso, iter)
|
||||||
{
|
{
|
||||||
if (had_children)
|
if (had_children)
|
||||||
{
|
{
|
||||||
printbuf_strappend(pb, ",");
|
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;
|
had_children = 1;
|
||||||
if (flags & JSON_C_TO_STRING_SPACED && !(flags & JSON_C_TO_STRING_PRETTY))
|
if (flags & JSON_C_TO_STRING_SPACED && !(flags & JSON_C_TO_STRING_PRETTY))
|
||||||
printbuf_strappend(pb, " ");
|
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)
|
else if (iter.val->_to_json_string(iter.val, pb, level + 1, flags) < 0)
|
||||||
return -1;
|
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);
|
indent(pb, level, flags);
|
||||||
}
|
}
|
||||||
if (flags & JSON_C_TO_STRING_SPACED && !(flags & JSON_C_TO_STRING_PRETTY))
|
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;
|
size_t ii;
|
||||||
|
|
||||||
printbuf_strappend(pb, "[");
|
printbuf_strappend(pb, "[");
|
||||||
if (flags & JSON_C_TO_STRING_PRETTY)
|
|
||||||
printbuf_strappend(pb, "\n");
|
|
||||||
for (ii = 0; ii < json_object_array_length(jso); ii++)
|
for (ii = 0; ii < json_object_array_length(jso); ii++)
|
||||||
{
|
{
|
||||||
struct json_object *val;
|
struct json_object *val;
|
||||||
if (had_children)
|
if (had_children)
|
||||||
{
|
{
|
||||||
printbuf_strappend(pb, ",");
|
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;
|
had_children = 1;
|
||||||
if (flags & JSON_C_TO_STRING_SPACED && !(flags & JSON_C_TO_STRING_PRETTY))
|
if (flags & JSON_C_TO_STRING_SPACED && !(flags & JSON_C_TO_STRING_PRETTY))
|
||||||
printbuf_strappend(pb, " ");
|
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)
|
else if (val->_to_json_string(val, pb, level + 1, flags) < 0)
|
||||||
return -1;
|
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);
|
indent(pb, level, flags);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -311,6 +311,11 @@ int main(int argc, char **argv)
|
|||||||
{
|
{
|
||||||
printf("\t%s: %s\n", key, json_object_to_json_string(val));
|
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));
|
printf("my_object.to_string()=%s\n", json_object_to_json_string(my_object));
|
||||||
|
|
||||||
json_object_put(my_array);
|
json_object_put(my_array);
|
||||||
|
|||||||
@@ -75,4 +75,4 @@ my_object=
|
|||||||
foo: "bar"
|
foo: "bar"
|
||||||
bool0: false
|
bool0: false
|
||||||
bool1: true
|
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": { } }
|
||||||
|
|||||||
@@ -75,4 +75,4 @@ my_object=
|
|||||||
foo: "bar"
|
foo: "bar"
|
||||||
bool0: false
|
bool0: false
|
||||||
bool1: true
|
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":{}}
|
||||||
|
|||||||
@@ -97,5 +97,7 @@ my_object.to_string()={
|
|||||||
"abc":12,
|
"abc":12,
|
||||||
"foo":"bar",
|
"foo":"bar",
|
||||||
"bool0":false,
|
"bool0":false,
|
||||||
"bool1":true
|
"bool1":true,
|
||||||
|
"empty_array":[],
|
||||||
|
"empty_obj":{}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -75,4 +75,4 @@ my_object=
|
|||||||
foo: "bar"
|
foo: "bar"
|
||||||
bool0: false
|
bool0: false
|
||||||
bool1: true
|
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": { } }
|
||||||
|
|||||||
@@ -97,5 +97,7 @@ my_object.to_string()={
|
|||||||
"abc": 12,
|
"abc": 12,
|
||||||
"foo": "bar",
|
"foo": "bar",
|
||||||
"bool0": false,
|
"bool0": false,
|
||||||
"bool1": true
|
"bool1": true,
|
||||||
|
"empty_array": [],
|
||||||
|
"empty_obj": {}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -97,5 +97,7 @@ my_object.to_string()={
|
|||||||
"abc": 12,
|
"abc": 12,
|
||||||
"foo": "bar",
|
"foo": "bar",
|
||||||
"bool0": false,
|
"bool0": false,
|
||||||
"bool1": true
|
"bool1": true,
|
||||||
|
"empty_array": [],
|
||||||
|
"empty_obj": {}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user