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 --- 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 #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 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 --- 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 #include -#include +#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 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 --- 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 #include #include +#include -#include +#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