json_writer: fix pretty printing of arrays
Subsystems (3)
PATCH 1/1 json_writer: fix pretty printing of arrays
When pretty printing, array elements were emitted on a single line and the closing bracket of an array of objects was glued to the last element, producing hard to read output such as:
[ {
"ifindex": 1,
"flags": [ "UP","LOWER_UP" ]
},{
...
} ]Move newline handling into jsonw_eor() so every element of an array or object starts on its own indented line, and let jsonw_end() emit the newline before the closing bracket for arrays as it already does for objects. A property name now sets sep to ':' so the following value is not separated from its name.
Output now looks like:
[
{
"ifindex": 1,
"flags": [
"UP",
"LOWER_UP"
]
},
{
...
}]
Empty arrays are now printed as "[]" instead of "[ ]", matching empty objects. Non-pretty output is unchanged.
--- lib/json_writer.c | 28 +++++++++++++++++----------- 1 file changed, 17 insertions(+), 11 deletions(-)
lib/json_writer.c +17 -11
@@ -22,7 +22,7 @@ struct json_writer {FILE *out;/* output file */ unsigned depth;/* nesting */ bool pretty;/* optional whitepace */ -char sep;/* either nul or comma */ +char sep;/* nul, comma, or colon after name */ };/* indentation for pretty print */
@@ -46,8 +46,20 @@ static void jsonw_eol(json_writer_t *self)/* If current object is not empty print a comma */ static void jsonw_eor (json_writer_t *self) { -if (self->sep !='\0' ) -putc (self->sep, self->out); +switch (self->sep) { +case ',' : +putc (',' , self->out); +jsonw_eol (self); +break ; +case ':' : +/* after property name, don't output comma or newline */ +break ; +case '\0' : +/* first element of a container, not start of document */ +if (self->depth) +jsonw_eol (self); +break ; + } self->sep =',' ; }
@@ -143,9 +155,8 @@ static void jsonw_end(json_writer_t *self, int c)/* Add a JSON property name */ void jsonw_name (json_writer_t *self,const char *name) { -jsonw_eor (self); -jsonw_eol (self); - self->sep ='\0' ; +jsonw_eor (self); + self->sep =':' ;jsonw_puts (self, name);putc (':' , self->out);if (self->pretty)
@@ -177,15 +188,10 @@ void jsonw_end_object(json_writer_t *self)void jsonw_start_array (json_writer_t *self) {jsonw_begin (self,'[' ); -if (self->pretty) -putc (' ' , self->out); }void jsonw_end_array (json_writer_t *self) { -if (self->pretty && self->sep) -putc (' ' , self->out); - self->sep ='\0' ;jsonw_end (self,']' ); }
--
2.56.0
Quoted 10 lines · On Thu, 22 Oct 2026 21:10:07 +0000 Hadrien Patte…
Output now looks like:
[
{ "ifindex": 1, "flags": [ "UP", "LOWER_UP" ] },
Putting all the array elements on oneline is easier to read and more compact. What does python do?
Quoted 2 lines · On Fri, 23 Oct 2026 18:13:02 -0700 Stephen Hemminger…
Putting all the array elements on oneline is easier to read and more compact. What does python do?
My current patch actually contains two separate changes:
1. A fix for arrays of objects. Scalar arrays render fine on a single line, but when the elements are objects, which contain newlines, the brackets and separators end up glued to them and the indentation levels of opening and closing brackets are off:
# ip -j -p addr show dev lo
[ {
"ifindex": 1,
"ifname": "lo",
"flags": [ "LOOPBACK","UP","LOWER_UP" ],
...
"addr_info": [ {
"family": "inet",
"local": "127.0.0.1",
...
},{
"family": "inet6",
"local": "::1",
...
} ]
} ]2. Putting each element of scalar arrays on its own line. This is a style change rather than a fix, though it is consistent with common JSON pretty-printers such as jq or python's json.dumps.
I'm happy to send a v2 that only keeps the first part, leaving scalar arrays in their current single-line form if you prefer.
Thanks,
Hadrien
Quoted 34 lines · On Sat, 24 Oct 2026 18:50:46 +0000 Hadrien Patte…
Quoted 2 lines · On Fri, 23 Oct 2026 18:13:02 -0700 Stephen Hemminger…
Putting all the array elements on oneline is easier to read and more compact. What does python do?
My current patch actually contains two separate changes:
1. A fix for arrays of objects. Scalar arrays render fine on a single line, but when the elements are objects, which contain newlines, the brackets and separators end up glued to them and the indentation levels of opening and closing brackets are off:
# ip -j -p addr show dev lo [ { "ifindex": 1, "ifname": "lo", "flags": [ "LOOPBACK","UP","LOWER_UP" ], ... "addr_info": [ { "family": "inet", "local": "127.0.0.1", ... },{ "family": "inet6", "local": "::1", ... } ] } ]2. Putting each element of scalar arrays on its own line. This is a style change rather than a fix, though it is consistent with common JSON pretty-printers such as jq or python's json.dumps.
Quoted 6 lines
I'm happy to send a v2 that only keeps the first part, leaving scalar arrays in their current single-line form if you prefer.
Thanks,
Hadrien
Looks ok, as is. more of an observation by me.
I don't in general look at json pretty print
Quoted 40 lines · Stephen Hemminger
Quoted 37 lines · On Sat, 24 Oct 2026 18:50:46 +0000 Hadrien Patte…
Quoted 2 lines · On Fri, 23 Oct 2026 18:13:02 -0700 Stephen Hemminger…
Putting all the array elements on oneline is easier to read and more compact. What does python do?
My current patch actually contains two separate changes:
1. A fix for arrays of objects. Scalar arrays render fine on a single line, but when the elements are objects, which contain newlines, the brackets and separators end up glued to them and the indentation levels of opening and closing brackets are off:
# ip -j -p addr show dev lo [ { "ifindex": 1, "ifname": "lo", "flags": [ "LOOPBACK","UP","LOWER_UP" ], ... "addr_info": [ { "family": "inet", "local": "127.0.0.1", ... },{ "family": "inet6", "local": "::1", ... } ] } ]2. Putting each element of scalar arrays on its own line. This is a style change rather than a fix, though it is consistent with common JSON pretty-printers such as jq or python's json.dumps.
I'm happy to send a v2 that only keeps the first part, leaving scalar arrays in their current single-line form if you prefer.
Quoted 6 lines
Quoted 3 lines
Thanks,
HadrienLooks ok, as is. more of an observation by me.
I don't in general look at json pretty print
Meaning you are good with both changes? If so, Hadrien the 2 separate changes should be 2 separate patches if possible.