Skip to contentSkip to conversation

misc: convert to high-level json_print API

PATCH Applied Andrea Claudi 2 participants 5 messages Oct 21, 2025 – Oct 26, 2025

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

cover misc: convert to high-level json_print API

This patch series converts three utilities in the misc/ directory (ifstat, nstat, and lnstat) from using the low-level json_writer API to the high-level json_print API, ensuring they use the same json printing logic of all the other iproute2 tools.

The JSON output before and after these changes remains the same.

Andrea Claudi (3):
ifstat: convert to high-level json_print API
nstat: convert to high-level json_print API
lnstat: convert to high-level json_print API

 misc/ifstat.c | 85 ++++++++++++++++++++++-----------------------------
 misc/lnstat.c | 17 +++++------
 misc/nstat.c  | 48 +++++++++++++----------------
 3 files changed, 65 insertions(+), 85 deletions(-)
Signature · 1 line
2.51.0
#1 lore raw

PATCH 1/3 ifstat: convert to high-level json_print API

Replace the low-level json_writer API calls with the high-level json_print API to maintain consistency with the rest of the iproute2 codebase.

---
 misc/ifstat.c | 85 ++++++++++++++++++++++-----------------------------
 1 file changed, 37 insertions(+), 48 deletions(-)
misc/ifstat.c +37 -48
index a47d0b16..ec59a9eb 100644
@@ -28,7 +28,7 @@
 #include <linux/if_link.h>
 
 #include "libnetlink.h"
-#include "json_writer.h"
+#include "json_print.h"
 #include "version.h"
 #include "utils.h"
 
@@ -337,15 +337,13 @@ static void load_raw_table(FILE *fp)
 
 static void dump_raw_db(FILE *fp, int to_hist)
 {
-	json_writer_t *jw = json_output ? jsonw_new(fp) : NULL;
 	struct ifstat_ent *n, *h;
 
 	h = hist_db;
-	if (jw) {
-		jsonw_start_object(jw);
-		jsonw_pretty(jw, pretty);
-		jsonw_name(jw, info_source);
-		jsonw_start_object(jw);
+	new_json_obj_plain(json_output);
+	if (is_json_context()) {
+		open_json_object(NULL);
+		open_json_object(info_source);
 	} else
 		fprintf(fp, "#%s\n", info_source);
 
@@ -369,13 +367,12 @@ static void dump_raw_db(FILE *fp, int to_hist)
 			}
 		}
 
-		if (jw) {
-			jsonw_name(jw, n->name);
-			jsonw_start_object(jw);
+		if (is_json_context()) {
+			open_json_object(n->name);
 
 			for (i = 0; i < MAXS && stats[i]; i++)
-				jsonw_uint_field(jw, stats[i], vals[i]);
-			jsonw_end_object(jw);
+				print_lluint(PRINT_JSON, stats[i], NULL, vals[i]);
+			close_json_object();
 		} else {
 			fprintf(fp, "%d %s ", n->ifindex, n->name);
 			for (i = 0; i < MAXS; i++)
@@ -384,12 +381,11 @@ static void dump_raw_db(FILE *fp, int to_hist)
 			fprintf(fp, "\n");
 		}
 	}
-	if (jw) {
-		jsonw_end_object(jw);
-
-		jsonw_end_object(jw);
-		jsonw_destroy(&jw);
+	if (is_json_context()) {
+		close_json_object();
+		close_json_object();
 	}
+	delete_json_obj_plain();
 }
 
 /* use communication definitions of meg/kilo etc */
@@ -483,18 +479,17 @@ static void print_head(FILE *fp)
 	}
 }
 
-static void print_one_json(json_writer_t *jw, const struct ifstat_ent *n,
+static void print_one_json(const struct ifstat_ent *n,
 			   const unsigned long long *vals)
 {
 	int i, m = show_errors ? 20 : 10;
 
-	jsonw_name(jw, n->name);
-	jsonw_start_object(jw);
+	open_json_object(n->name);
 
 	for (i = 0; i < m && stats[i]; i++)
-		jsonw_uint_field(jw, stats[i], vals[i]);
+		print_lluint(PRINT_JSON, stats[i], NULL, vals[i]);
 
-	jsonw_end_object(jw);
+	close_json_object();
 }
 
 static void print_one_if(FILE *fp, const struct ifstat_ent *n,
@@ -547,14 +542,12 @@ static void print_one_if(FILE *fp, const struct ifstat_ent *n,
 
 static void dump_kern_db(FILE *fp)
 {
-	json_writer_t *jw = json_output ? jsonw_new(fp) : NULL;
 	struct ifstat_ent *n;
 
-	if (jw) {
-		jsonw_start_object(jw);
-		jsonw_pretty(jw, pretty);
-		jsonw_name(jw, info_source);
-		jsonw_start_object(jw);
+	new_json_obj_plain(json_output);
+	if (is_json_context()) {
+		open_json_object(NULL);
+		open_json_object(info_source);
 	} else
 		print_head(fp);
 
@@ -562,30 +555,27 @@ static void dump_kern_db(FILE *fp)
 		if (!match(n->name))
 			continue;
 
-		if (jw)
-			print_one_json(jw, n, n->val);
+		if (is_json_context())
+			print_one_json(n, n->val);
 		else
 			print_one_if(fp, n, n->val);
 	}
-	if (jw) {
-		jsonw_end_object(jw);
-
-		jsonw_end_object(jw);
-		jsonw_destroy(&jw);
+	if (is_json_context()) {
+		close_json_object();
+		close_json_object();
 	}
+	delete_json_obj_plain();
 }
 
 static void dump_incr_db(FILE *fp)
 {
 	struct ifstat_ent *n, *h;
-	json_writer_t *jw = json_output ? jsonw_new(fp) : NULL;
 
 	h = hist_db;
-	if (jw) {
-		jsonw_start_object(jw);
-		jsonw_pretty(jw, pretty);
-		jsonw_name(jw, info_source);
-		jsonw_start_object(jw);
+	new_json_obj_plain(json_output);
+	if (is_json_context()) {
+		open_json_object(NULL);
+		open_json_object(info_source);
 	} else
 		print_head(fp);
 
@@ -607,18 +597,17 @@ static void dump_incr_db(FILE *fp)
 		if (!match(n->name))
 			continue;
 
-		if (jw)
-			print_one_json(jw, n, n->val);
+		if (is_json_context())
+			print_one_json(n, n->val);
 		else
 			print_one_if(fp, n, vals);
 	}
 
-	if (jw) {
-		jsonw_end_object(jw);
-
-		jsonw_end_object(jw);
-		jsonw_destroy(&jw);
+	if (is_json_context()) {
+		close_json_object();
+		close_json_object();
 	}
+	delete_json_obj_plain();
 }
 
 static int children;
Signature · 1 line
2.51.0
#2 lore raw

PATCH 2/3 nstat: convert to high-level json_print API

In reply to Andrea Claudi

Replace the low-level json_writer API calls with the high-level json_print API to maintain consistency with the rest of the iproute2 codebase.

---
 misc/nstat.c | 48 +++++++++++++++++++++---------------------------
 1 file changed, 21 insertions(+), 27 deletions(-)
misc/nstat.c +21 -27
index b2e19bde..4a9f3326 100644
@@ -24,7 +24,7 @@
 #include <math.h>
 #include <getopt.h>
 
-#include <json_writer.h>
+#include "json_print.h"
 #include "version.h"
 #include "utils.h"
 
@@ -309,15 +309,13 @@ static void load_netstat(void)
 
 static void dump_kern_db(FILE *fp, int to_hist)
 {
-	json_writer_t *jw = json_output ? jsonw_new(fp) : NULL;
 	struct nstat_ent *n, *h;
 
 	h = hist_db;
-	if (jw) {
-		jsonw_start_object(jw);
-		jsonw_pretty(jw, pretty);
-		jsonw_name(jw, info_source);
-		jsonw_start_object(jw);
+	new_json_obj_plain(json_output);
+	if (is_json_context()) {
+		open_json_object(NULL);
+		open_json_object(info_source);
 	} else
 		fprintf(fp, "#%s\n", info_source);
 
@@ -340,31 +338,28 @@ static void dump_kern_db(FILE *fp, int to_hist)
 			}
 		}
 
-		if (jw)
-			jsonw_uint_field(jw, n->id, val);
+		if (is_json_context())
+			print_lluint(PRINT_JSON, n->id, NULL, val);
 		else
 			fprintf(fp, "%-32s%-16llu%6.1f\n", n->id, val, n->rate);
 	}
 
-	if (jw) {
-		jsonw_end_object(jw);
-
-		jsonw_end_object(jw);
-		jsonw_destroy(&jw);
+	if (is_json_context()) {
+		close_json_object();
+		close_json_object();
 	}
+	delete_json_obj_plain();
 }
 
 static void dump_incr_db(FILE *fp)
 {
-	json_writer_t *jw = json_output ? jsonw_new(fp) : NULL;
 	struct nstat_ent *n, *h;
 
 	h = hist_db;
-	if (jw) {
-		jsonw_start_object(jw);
-		jsonw_pretty(jw, pretty);
-		jsonw_name(jw, info_source);
-		jsonw_start_object(jw);
+	new_json_obj_plain(json_output);
+	if (is_json_context()) {
+		open_json_object(NULL);
+		open_json_object(info_source);
 	} else
 		fprintf(fp, "#%s\n", info_source);
 
@@ -389,19 +384,18 @@ static void dump_incr_db(FILE *fp)
 		if (!match(n->id))
 			continue;
 
-		if (jw)
-			jsonw_uint_field(jw, n->id, val);
+		if (is_json_context())
+			print_lluint(PRINT_JSON, n->id, NULL, val);
 		else
 			fprintf(fp, "%-32s%-16llu%6.1f%s\n", n->id, val,
 				n->rate, ovfl?" (overflow)":"");
 	}
 
-	if (jw) {
-		jsonw_end_object(jw);
-
-		jsonw_end_object(jw);
-		jsonw_destroy(&jw);
+	if (is_json_context()) {
+		close_json_object();
+		close_json_object();
 	}
+	delete_json_obj_plain();
 }
 
 static int children;
Signature · 1 line
2.51.0
#3 lore raw

PATCH 3/3 lnstat: convert to high-level json_print API

In reply to Andrea Claudi

Replace the low-level json_writer API calls with the high-level json_print API to maintain consistency with the rest of the iproute2 codebase.

---
 misc/lnstat.c | 17 +++++++----------
 1 file changed, 7 insertions(+), 10 deletions(-)
misc/lnstat.c +7 -10
index f802a0f3..8f359578 100644
@@ -30,8 +30,9 @@
 #include <stdlib.h>
 #include <string.h>
 #include <getopt.h>
+#include <linux/types.h>
 
-#include <json_writer.h>
+#include "json_print.h"
 #include "lnstat.h"
 #include "version.h"
 
@@ -109,21 +110,17 @@ static void print_line(FILE *of, const struct lnstat_file *lnstat_files,
 static void print_json(FILE *of, const struct lnstat_file *lnstat_files,
 		       const struct field_params *fp)
 {
-	json_writer_t *jw = jsonw_new(of);
 	int i;
 
-	if (jw == NULL) {
-		fprintf(stderr, "Failed to create JSON writer\n");
-		exit(1);
-	}
-	jsonw_start_object(jw);
+	new_json_obj_plain(1);
+	open_json_object(NULL);
 	for (i = 0; i < fp->num; i++) {
 		const struct lnstat_field *lf = fp->params[i].lf;
 
-		jsonw_uint_field(jw, lf->name, lf->result);
+		print_luint(PRINT_JSON, lf->name, NULL, lf->result);
 	}
-	jsonw_end_object(jw);
-	jsonw_destroy(&jw);
+	close_json_object();
+	delete_json_obj_plain();
 }
 
 /* find lnstat_field according to user specification */
Signature · 1 line
2.51.0
Applied
#4 lore raw
In reply to Andrea Claudi
Quoted 12 lines · Andrea Claudi

This patch series converts three utilities in the misc/ directory (ifstat, nstat, and lnstat) from using the low-level json_writer API to the high-level json_print API, ensuring they use the same json printing logic of all the other iproute2 tools.

The JSON output before and after these changes remains the same.

Andrea Claudi (3):
ifstat: convert to high-level json_print API
nstat: convert to high-level json_print API
lnstat: convert to high-level json_print API

Quoted 5 lines
 misc/ifstat.c | 85 ++++++++++++++++++++++-----------------------------
 misc/lnstat.c | 17 +++++------
 misc/nstat.c  | 48 +++++++++++++----------------
 3 files changed, 65 insertions(+), 85 deletions(-)

applied to iproute2-next; thanks for working on cleanups.