]> git.ipfire.org Git - thirdparty/iproute2.git/commitdiff
nstat: convert to high-level json_print API
authorAndrea Claudi <aclaudi@redhat.com>
Tue, 21 Oct 2025 20:39:17 +0000 (22:39 +0200)
committerDavid Ahern <dsahern@kernel.org>
Sun, 26 Oct 2025 23:14:14 +0000 (17:14 -0600)
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>
Signed-off-by: David Ahern <dsahern@kernel.org>
misc/nstat.c

index b2e19bde2df5447896e1576470d049034a1ecc1a..4a9f33269f3b3398e3c9067bc088c6d2b31abbe9 100644 (file)
@@ -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;