* [PATCH iproute2-next] json_writer: fix pretty printing of arrays
@ 2026-10-22 21:10 Hadrien Patte
2026-10-24 1:13 ` Stephen Hemminger
0 siblings, 1 reply; 5+ messages in thread
From: Hadrien Patte @ 2026-10-22 21:10 UTC (permalink / raw)
To: netdev; +Cc: David Ahern, Stephen Hemminger, Hadrien Patte
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.
Signed-off-by: Hadrien Patte <hadrien.patte@protonmail.com>
---
lib/json_writer.c | 28 +++++++++++++++++-----------
1 file changed, 17 insertions(+), 11 deletions(-)
diff --git a/lib/json_writer.c b/lib/json_writer.c
index d4a1c72b..d5ff8d8b 100644
--- a/lib/json_writer.c
+++ b/lib/json_writer.c
@@ -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
^ permalink raw reply related [flat|nested] 5+ messages in thread* Re: [PATCH iproute2-next] json_writer: fix pretty printing of arrays
2026-10-22 21:10 [PATCH iproute2-next] json_writer: fix pretty printing of arrays Hadrien Patte
@ 2026-10-24 1:13 ` Stephen Hemminger
2026-10-24 18:50 ` Hadrien Patte
0 siblings, 1 reply; 5+ messages in thread
From: Stephen Hemminger @ 2026-10-24 1:13 UTC (permalink / raw)
To: Hadrien Patte; +Cc: netdev, David Ahern
On Thu, 22 Oct 2026 21:10:07 +0000
Hadrien Patte <hadrien.patte@protonmail.com> wrote:
> 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?
^ permalink raw reply [flat|nested] 5+ messages in thread* Re: [PATCH iproute2-next] json_writer: fix pretty printing of arrays
2026-10-24 1:13 ` Stephen Hemminger
@ 2026-10-24 18:50 ` Hadrien Patte
2026-10-25 18:34 ` Stephen Hemminger
0 siblings, 1 reply; 5+ messages in thread
From: Hadrien Patte @ 2026-10-24 18:50 UTC (permalink / raw)
To: stephen; +Cc: dsahern, hadrien.patte, netdev
On Fri, 23 Oct 2026 18:13:02 -0700
Stephen Hemminger <stephen@networkplumber.org> wrote:
> 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
^ permalink raw reply [flat|nested] 5+ messages in thread* Re: [PATCH iproute2-next] json_writer: fix pretty printing of arrays
2026-10-24 18:50 ` Hadrien Patte
@ 2026-10-25 18:34 ` Stephen Hemminger
2026-10-25 19:42 ` David Ahern
0 siblings, 1 reply; 5+ messages in thread
From: Stephen Hemminger @ 2026-10-25 18:34 UTC (permalink / raw)
To: Hadrien Patte; +Cc: dsahern, netdev
On Sat, 24 Oct 2026 18:50:46 +0000
Hadrien Patte <hadrien.patte@protonmail.com> wrote:
> On Fri, 23 Oct 2026 18:13:02 -0700
> Stephen Hemminger <stephen@networkplumber.org> wrote:
>
> > 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
>
Looks ok, as is. more of an observation by me.
I don't in general look at json pretty print
^ permalink raw reply [flat|nested] 5+ messages in thread* Re: [PATCH iproute2-next] json_writer: fix pretty printing of arrays
2026-10-25 18:34 ` Stephen Hemminger
@ 2026-10-25 19:42 ` David Ahern
0 siblings, 0 replies; 5+ messages in thread
From: David Ahern @ 2026-10-25 19:42 UTC (permalink / raw)
To: Stephen Hemminger, Hadrien Patte; +Cc: netdev
On 10/25/26 12:34 PM, Stephen Hemminger wrote:
> On Sat, 24 Oct 2026 18:50:46 +0000
> Hadrien Patte <hadrien.patte@protonmail.com> wrote:
>
>> On Fri, 23 Oct 2026 18:13:02 -0700
>> Stephen Hemminger <stephen@networkplumber.org> wrote:
>>
>>> 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
>>
>
> Looks 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.
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2026-10-25 19:42 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-10-22 21:10 [PATCH iproute2-next] json_writer: fix pretty printing of arrays Hadrien Patte
2026-10-24 1:13 ` Stephen Hemminger
2026-10-24 18:50 ` Hadrien Patte
2026-10-25 18:34 ` Stephen Hemminger
2026-10-25 19:42 ` David Ahern
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox