* [PATCH iproute2-next 1/3] ifstat: convert to high-level json_print API
2025-10-21 20:39 [PATCH iproute2-next 0/3] misc: convert to high-level json_print API Andrea Claudi
@ 2025-10-21 20:39 ` Andrea Claudi
2025-10-21 20:39 ` [PATCH iproute2-next 2/3] nstat: " Andrea Claudi
` (2 subsequent siblings)
3 siblings, 0 replies; 5+ messages in thread
From: Andrea Claudi @ 2025-10-21 20:39 UTC (permalink / raw)
To: netdev; +Cc: Stephen Hemminger, David Ahern
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.
Signed-off-by: Andrea Claudi <aclaudi@redhat.com>
---
misc/ifstat.c | 85 ++++++++++++++++++++++-----------------------------
1 file changed, 37 insertions(+), 48 deletions(-)
diff --git a/misc/ifstat.c b/misc/ifstat.c
index a47d0b16..ec59a9eb 100644
--- a/misc/ifstat.c
+++ b/misc/ifstat.c
@@ -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;
--
2.51.0
^ permalink raw reply related [flat|nested] 5+ messages in thread* [PATCH iproute2-next 2/3] nstat: convert to high-level json_print API
2025-10-21 20:39 [PATCH iproute2-next 0/3] misc: convert to high-level json_print API Andrea Claudi
2025-10-21 20:39 ` [PATCH iproute2-next 1/3] ifstat: " Andrea Claudi
@ 2025-10-21 20:39 ` Andrea Claudi
2025-10-21 20:39 ` [PATCH iproute2-next 3/3] lnstat: " Andrea Claudi
2025-10-26 23:24 ` [PATCH iproute2-next 0/3] misc: " David Ahern
3 siblings, 0 replies; 5+ messages in thread
From: Andrea Claudi @ 2025-10-21 20:39 UTC (permalink / raw)
To: netdev; +Cc: Stephen Hemminger, David Ahern
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.
Signed-off-by: Andrea Claudi <aclaudi@redhat.com>
---
misc/nstat.c | 48 +++++++++++++++++++++---------------------------
1 file changed, 21 insertions(+), 27 deletions(-)
diff --git a/misc/nstat.c b/misc/nstat.c
index b2e19bde..4a9f3326 100644
--- a/misc/nstat.c
+++ b/misc/nstat.c
@@ -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;
--
2.51.0
^ permalink raw reply related [flat|nested] 5+ messages in thread* [PATCH iproute2-next 3/3] lnstat: convert to high-level json_print API
2025-10-21 20:39 [PATCH iproute2-next 0/3] misc: convert to high-level json_print API Andrea Claudi
2025-10-21 20:39 ` [PATCH iproute2-next 1/3] ifstat: " Andrea Claudi
2025-10-21 20:39 ` [PATCH iproute2-next 2/3] nstat: " Andrea Claudi
@ 2025-10-21 20:39 ` Andrea Claudi
2025-10-26 23:24 ` [PATCH iproute2-next 0/3] misc: " David Ahern
3 siblings, 0 replies; 5+ messages in thread
From: Andrea Claudi @ 2025-10-21 20:39 UTC (permalink / raw)
To: netdev; +Cc: Stephen Hemminger, David Ahern
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.
Signed-off-by: Andrea Claudi <aclaudi@redhat.com>
---
misc/lnstat.c | 17 +++++++----------
1 file changed, 7 insertions(+), 10 deletions(-)
diff --git a/misc/lnstat.c b/misc/lnstat.c
index f802a0f3..8f359578 100644
--- a/misc/lnstat.c
+++ b/misc/lnstat.c
@@ -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 */
--
2.51.0
^ permalink raw reply related [flat|nested] 5+ messages in thread* Re: [PATCH iproute2-next 0/3] misc: convert to high-level json_print API
2025-10-21 20:39 [PATCH iproute2-next 0/3] misc: convert to high-level json_print API Andrea Claudi
` (2 preceding siblings ...)
2025-10-21 20:39 ` [PATCH iproute2-next 3/3] lnstat: " Andrea Claudi
@ 2025-10-26 23:24 ` David Ahern
3 siblings, 0 replies; 5+ messages in thread
From: David Ahern @ 2025-10-26 23:24 UTC (permalink / raw)
To: Andrea Claudi, netdev; +Cc: Stephen Hemminger
On 10/21/25 2:39 PM, Andrea Claudi wrote:
> 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(-)
>
applied to iproute2-next; thanks for working on cleanups.
^ permalink raw reply [flat|nested] 5+ messages in thread