Netdev List
 help / color / mirror / Atom feed
From: Hadrien Patte <hadrien.patte@protonmail.com>
To: netdev@vger.kernel.org
Cc: David Ahern <dsahern@kernel.org>,
	Stephen Hemminger <stephen@networkplumber.org>,
	Hadrien Patte <hadrien.patte@protonmail.com>
Subject: [PATCH iproute2-next] json_writer: fix pretty printing of arrays
Date: Thu, 22 Oct 2026 21:10:07 +0000	[thread overview]
Message-ID: <20261022211002.31098-1-hadrien.patte@protonmail.com> (raw)

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




             reply	other threads:[~2026-10-22 21:10 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-10-22 21:10 Hadrien Patte [this message]
2026-10-24  1:13 ` [PATCH iproute2-next] json_writer: fix pretty printing of arrays Stephen Hemminger
2026-10-24 18:50   ` Hadrien Patte
2026-10-25 18:34     ` Stephen Hemminger
2026-10-25 19:42       ` David Ahern

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261022211002.31098-1-hadrien.patte@protonmail.com \
    --to=hadrien.patte@protonmail.com \
    --cc=dsahern@kernel.org \
    --cc=netdev@vger.kernel.org \
    --cc=stephen@networkplumber.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox