iproute2: provide common json output formatter
Subsystems (3)
PATCH 1/1 iproute2: provide common json output formatter
Formatting JSON is moderately painful. Provide a simple API to do the syntax formatting. Use it to replace existing json in *stat commands.
--- include/json_writer.h | 61 ++++++++++ lib/Makefile | 3 +- lib/json_writer.c | 312 ++++++++++++++++++++++++++++++++++++++++++++++++++ misc/Makefile | 2 +- misc/ifstat.c | 103 +++++++++-------- misc/lnstat.c | 22 ++-- misc/nstat.c | 59 ++++++---- 7 files changed, 477 insertions(+), 85 deletions(-) create mode 100644 include/json_writer.h create mode 100644 lib/json_writer.c
include/json_writer.h new file +61 -0
@@ -0,0 +1,61 @@ +/* +* Simple streaming JSON writer +* +* This takes care of the annoying bits of JSON syntax like the commas +* after elements +* +* This program is free software; you can redistribute it and/or +* modify it under the terms of the GNU General Public License +* as published by the Free Software Foundation; either version +* 2 of the License, or (at your option) any later version. +* +* Authors: Stephen Hemminger <[email protected]> +*/ + +#ifndef _JSON_WRITER_H_ +#define _JSON_WRITER_H_ + +#include <stdbool.h> +#include <stdint.h> + +/* Opaque class structure */ +typedef struct json_writer json_writer_t ; + +/* Create a new JSON stream */ +json_writer_t *jsonw_new (FILE *f); +/* End output to JSON stream */ +void jsonw_destroy (json_writer_t **self_p); + +/* Cause output to have pretty whitespace */ +void jsonw_pretty (json_writer_t *self,bool on); + +/* Add property name */ +void jsonw_name (json_writer_t *self,const char *name); + +/* Add value */ +void jsonw_string (json_writer_t *self,const char *value); +void jsonw_bool (json_writer_t *self,bool value); +void jsonw_float (json_writer_t *self,double number); +void jsonw_uint (json_writer_t *self,uint64_t number); +void jsonw_int (json_writer_t *self,int64_t number); +void jsonw_null (json_writer_t *self); + +/* Useful Combinations of name and value */ +void jsonw_string_field (json_writer_t *self,const char *prop,const char *val); +void jsonw_bool_field (json_writer_t *self,const char *prop,bool value); +void jsonw_float_field (json_writer_t *self,const char *prop,double num); +void jsonw_uint_field (json_writer_t *self,const char *prop,uint64_t num); +void jsonw_int_field (json_writer_t *self,const char *prop,int64_t num); +void jsonw_null_field (json_writer_t *self,const char *prop); + +/* Collections */ +void jsonw_start_object (json_writer_t *self); +void jsonw_end_object (json_writer_t *self); + +void jsonw_start_array (json_writer_t *self); +void jsonw_end_array (json_writer_t *self); + +/* Override default exception handling */ +typedef void (jsonw_err_handler_fn )(const char *); + +#endif /* _JSON_WRITER_H_ */
lib/Makefile +2 -1
@@ -6,7 +6,8 @@ endif CFLAGS += -fPIC -UTILOBJ=utils.o rt_names.o ll_types.o ll_proto.o ll_addr.o inet_proto.o namespace.o \ +UTILOBJ = utils.o rt_names.o ll_types.o ll_proto.o ll_addr.o \ + inet_proto.o namespace.o json_writer.o \ names.o color.o NLOBJ=libgenl.o ll_map.o libnetlink.o
lib/json_writer.c new file +312 -0
312 lines not on this page · raw message
misc/Makefile +1 -1
@@ -19,7 +19,7 @@ all: $(TARGETS) ss: $(SSOBJ) nstat: nstat.c - $(CC) $(CFLAGS) $(LDFLAGS) -o nstat nstat.c -lm + $(CC) $(CFLAGS) $(LDFLAGS) -o nstat nstat.c $(LIBNETLINK) -lm ifstat: ifstat.c $(CC) $(CFLAGS) $(LDFLAGS) -o ifstat ifstat.c $(LIBNETLINK) -lm
misc/ifstat.c +57 -46
203 lines not on this page · raw message
misc/lnstat.c +8 -14
@@ -36,6 +36,7 @@#include <string.h> #include <getopt.h> +#include <json_writer.h> #include "lnstat.h" static struct option opts[] = {
@@ -49,6 +50,7 @@ static struct option opts[] = { { "keys", 1, NULL, 'k' }, { "subject", 1, NULL, 's' }, { "width", 1, NULL, 'w' }, + { "oneline", 0, NULL, 0 }, };static int usage (char *name,int exit_code)
@@ -107,25 +109,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; -const char *sep; -const char *base =NULL ; -fputs ("{\n" , of); +jsonw_start_object (jw);for (i =0 ; i < fp->num; i++) {const struct lnstat_field *lf = fp->params[i].lf; -if (!base || lf->file->basename != base) { -if (base)fputs ("},\n" , of); - base = lf->file->basename; - sep ="\n\t" ; -fprintf (of," \"%s\":{" , base); - } -fprintf (of,"%s\"%s\":%lu" , sep, - lf->name, lf->result); - sep =",\n\t" ; +jsonw_uint_field (jw, lf->name, lf->result); } -fputs ("}\n}\n" , of); +jsonw_end_object (jw); +jsonw_destroy (&jw); }/* find lnstat_field according to user specification */
@@ -272,7 +266,7 @@ int main(int argc, char **argv) num_req_files =1 ; } -while ((c =getopt_long (argc, argv,"Vc:djf:h?i:k:s:w:" , +while ((c =getopt_long (argc, argv,"Vc:djpf:h?i:k:s:w:" , opts,NULL )) !=-1 ) {int len =0 ;char *tmp, *tok;
misc/nstat.c +36 -23
128 lines not on this page · raw message
Signature · 1 line
2.1.4