Netdev List
 help / color / mirror / Atom feed
* [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

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