bridge: json support for fdb and vlan show
Subsystems (3)
v3 cover bridge: json support for fdb and vlan show
From: Roopa Prabhu <[email protected]>
This patch series adds json support for a few bridge show commands. We plan to follow up with json support for additional commands soon.
Anuradha Karuppiah (3):
json_writer: allow base json data type to be array or object
bridge: add json support for bridge fdb show
bridge: add json schema for bridge fdb show
Roopa Prabhu (2):
bridge: add json support for bridge vlan show
bridge: update man page
v2 - change vlan flags to an array as suggested by toshiaki
v3 - no change. resubmitting as requested by stephen
bridge/br_common.h | 1 + bridge/bridge.c | 3 + bridge/fdb.c | 201 +++++++++++++++++++++++++++++++++--------- bridge/vlan.c | 93 ++++++++++++++++++- include/json_writer.h | 5 +- lib/json_writer.c | 39 +++++++- man/man8/bridge.8 | 9 +- misc/ifstat.c | 6 +- misc/lnstat.c | 2 +- misc/nstat.c | 4 +- schema/bridge_fdb_schema.json | 62 +++++++++++++ 11 files changed, 365 insertions(+), 60 deletions(-) create mode 100644 schema/bridge_fdb_schema.json
Signature · 1 line
1.9.1
PATCH v3 2/5 bridge: add json support for bridge vlan show
From: Roopa Prabhu <[email protected]>
$bridge -c vlan show
port vlan ids
swp1 1 PVID Egress Untagged
10-13
swp2 1 PVID Egress Untagged
10-13
br0 1 PVID Egress Untagged
$bridge -json vlan show
{
"swp1": [{
"vlan": 1,
"flags": ["PVID","Egress Untagged"
]
},{
"vlan": 10
},{
"vlan": 11
},{
"vlan": 12
},{
"vlan": 13
}
],
"swp2": [{
"vlan": 1,
"flags": ["PVID","Egress Untagged"
]
},{
"vlan": 10
},{
"vlan": 11
},{
"vlan": 12
},{
"vlan": 13
}
],
"br0": [{
"vlan": 1,
"flags": ["PVID","Egress Untagged"
]
}
]
}$bridge -c -json vlan show
{
"swp1": [{
"vlan": 1,
"flags": ["PVID","Egress Untagged"
]
},{
"vlan": 10,
"vlanEnd": 13
}
],
"swp2": [{
"vlan": 1,
"flags": ["PVID","Egress Untagged"
]
},{
"vlan": 10,
"vlanEnd": 13
}
],
"br0": [{
"vlan": 1,
"flags": ["PVID","Egress Untagged"
]
}
]
}--- bridge/br_common.h | 1 + bridge/bridge.c | 5 ++- bridge/vlan.c | 106 ++++++++++++++++++++++++++++++++++++++++++++++------- 3 files changed, 97 insertions(+), 15 deletions(-)
bridge/br_common.h +1 -0
@@ -23,4 +23,5 @@ extern int show_stats;extern int show_details;extern int timestamp;extern int compress_vlans; +extern int json_output;extern struct rtnl_handle rth;
bridge/bridge.c +4 -1
@@ -23,6 +23,7 @@ int oneline;int show_stats;int show_details;int compress_vlans; +int json_output;int timestamp;char *batch_file;int force;
@@ -38,7 +39,7 @@ static void usage(void)"where OBJECT := { link | fdb | mdb | vlan | monitor }\n" " OPTIONS := { -V[ersion] | -s[tatistics] | -d[etails] |\n" " -o[neline] | -t[imestamp] | -n[etns] name |\n" -" -c[ompressvlans] }\n"); +" -c[ompressvlans] -j{son} }\n");exit (-1 ); }
@@ -173,6 +174,8 @@ main(int argc, char **argv) ++compress_vlans; }else if (matches (opt,"-force" ) ==0 ) { ++force; + }else if (matches (opt,"-json" ) ==0 ) { + ++json_output; }else if (matches (opt,"-batch" ) ==0 ) { argc--; argv++;
bridge/vlan.c +92 -14
155 lines not on this page · raw message
Signature · 1 line
1.9.1
PATCH v3 3/5 bridge: add json support for bridge fdb show
In reply to Roopa PrabhuFrom: Anuradha Karuppiah <[email protected]>
Sample output:
$bridge -j fdb show
[{
"mac": "44:38:39:00:69:88",
"dev": "swp2s0",
"vlan": 2,
"master": "br0",
"state": "permanent"
},{
"mac": "00:02:00:00:00:01",
"dev": "swp2s0",
"vlan": 2,
"master": "br0"
},{
"mac": "00:02:00:00:00:02",
"dev": "swp2s1",
"vlan": 2,
"master": "br0"
},{
"mac": "44:38:39:00:69:89",
"dev": "swp2s1",
"master": "br0",
"state": "permanent"
},{
"mac": "44:38:39:00:69:89",
"dev": "swp2s1",
"vlan": 2,
"master": "br0",
"state": "permanent"
},{
"mac": "44:38:39:00:69:88",
"dev": "br0",
"master": "br0",
"state": "permanent"
}
]--- bridge/fdb.c | 207 ++++++++++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 164 insertions(+), 43 deletions(-)
bridge/fdb.c +164 -43
@@ -21,6 +21,8 @@#include <linux/neighbour.h> #include <string.h> #include <limits.h> +#include <json_writer.h> +#include <stdbool.h> #include "libnetlink.h" #include "br_common.h"
@@ -29,6 +31,8 @@static unsigned int filter_index, filter_vlan; +json_writer_t *jw_global; +static void usage (void ) { fprintf(stderr, "Usage: bridge fdb { add | append | del | replace } ADDR dev DEV\n"
@@ -59,6 +63,15 @@ static const char *state_n2a(unsigned int s)return buf; } +static void start_json_fdb_flags_array (bool *fdb_flags) +{ +if (*fdb_flags) +return ; +jsonw_name (jw_global,"flags" ); +jsonw_start_array (jw_global); + *fdb_flags = true; +} +int print_fdb (const struct sockaddr_nl *who,struct nlmsghdr *n,void *arg) {FILE *fp = arg;
@@ -66,11 +79,12 @@ int print_fdb(const struct sockaddr_nl *who, struct nlmsghdr *n, void *arg)int len = n->nlmsg_len;struct rtattr *tb[NDA_MAX +1 ];__u16 vid =0 ; +bool fdb_flags = false; +const char *state_s;if (n->nlmsg_type !=RTM_NEWNEIGH && n->nlmsg_type !=RTM_DELNEIGH ) {fprintf (stderr,"Not RTM_NEWNEIGH: %08x %08x %08x\n" , n->nlmsg_len, n->nlmsg_type, n->nlmsg_flags); -return 0 ; }
@@ -86,6 +100,11 @@ int print_fdb(const struct sockaddr_nl *who, struct nlmsghdr *n, void *arg)if (filter_index && filter_index != r->ndm_ifindex)return 0 ; +if (jw_global) { +jsonw_pretty (jw_global,1 ); +jsonw_start_object (jw_global); + } +parse_rtattr (tb,NDA_MAX ,NDA_RTA (r), n->nlmsg_len -NLMSG_LENGTH (sizeof (*r)));
@@ -95,40 +114,75 @@ int print_fdb(const struct sockaddr_nl *who, struct nlmsghdr *n, void *arg)if (filter_vlan && filter_vlan != vid)return 0 ; -if (n->nlmsg_type ==RTM_DELNEIGH ) -fprintf (fp,"Deleted " ); +if (n->nlmsg_type ==RTM_DELNEIGH ) { +if (jw_global) +jsonw_string_field (jw_global,"opCode" ,"deleted" ); +else +fprintf (fp,"Deleted " ); + }if (tb[NDA_LLADDR ]) {SPRINT_BUF (b1); -fprintf (fp,"%s " , -ll_addr_n2a (RTA_DATA (tb[NDA_LLADDR ]), -RTA_PAYLOAD (tb[NDA_LLADDR ]), -ll_index_to_type (r->ndm_ifindex), - b1,sizeof (b1))); +ll_addr_n2a (RTA_DATA (tb[NDA_LLADDR ]), +RTA_PAYLOAD (tb[NDA_LLADDR ]), +ll_index_to_type (r->ndm_ifindex), + b1,sizeof (b1)); +if (jw_global) +jsonw_string_field (jw_global,"mac" , b1); +else +fprintf (fp,"%s " , b1); } -if (!filter_index && r->ndm_ifindex) -fprintf (fp,"dev %s " ,ll_index_to_name (r->ndm_ifindex)); +if (!filter_index && r->ndm_ifindex) { +if (jw_global) +jsonw_string_field (jw_global,"dev" , +ll_index_to_name (r->ndm_ifindex)); +else +fprintf (fp,"dev %s " , +ll_index_to_name (r->ndm_ifindex)); + }if (tb[NDA_DST ]) {int family =AF_INET ; +const char *abuf_s;if (RTA_PAYLOAD (tb[NDA_DST ]) ==sizeof (struct in6_addr )) family =AF_INET6 ; -fprintf (fp,"dst %s " , -format_host (family, -RTA_PAYLOAD (tb[NDA_DST ]), -RTA_DATA (tb[NDA_DST ]))); + abuf_s =format_host (family, +RTA_PAYLOAD (tb[NDA_DST ]), +RTA_DATA (tb[NDA_DST ])); +if (jw_global) +jsonw_string_field (jw_global,"dst" , abuf_s); +else +fprintf (fp,"dst %s " , abuf_s); + } + +if (vid) { +if (jw_global) +jsonw_uint_field (jw_global,"vlan" , vid); +else +fprintf (fp,"vlan %hu " , vid); + } + +if (tb[NDA_PORT ]) { +if (jw_global) +jsonw_uint_field (jw_global,"port" , +ntohs (rta_getattr_u16 (tb[NDA_PORT ]))); +else +fprintf (fp,"port %d " , +ntohs (rta_getattr_u16 (tb[NDA_PORT ]))); } -if (vid) -fprintf (fp,"vlan %hu " , vid); +if (tb[NDA_VNI ]) { +if (jw_global) +jsonw_uint_field (jw_global,"vni" , +rta_getattr_u32 (tb[NDA_VNI ])); +else +fprintf (fp,"vni %d " , +rta_getattr_u32 (tb[NDA_VNI ])); + } -if (tb[NDA_PORT ]) -fprintf (fp,"port %d " ,ntohs (rta_getattr_u16 (tb[NDA_PORT ]))); -if (tb[NDA_VNI ]) -fprintf (fp,"vni %d " ,rta_getattr_u32 (tb[NDA_VNI ]));if (tb[NDA_IFINDEX ]) {unsigned int ifindex =rta_getattr_u32 (tb[NDA_IFINDEX ]);
@@ -136,37 +190,95 @@ int print_fdb(const struct sockaddr_nl *who, struct nlmsghdr *n, void *arg)char ifname[IF_NAMESIZE ];if (!tb[NDA_LINK_NETNSID ] && -if_indextoname (ifindex, ifname)) -fprintf (fp,"via %s " , ifname); -else -fprintf (fp,"via ifindex %u " , ifindex); +if_indextoname (ifindex, ifname)) { +if (jw_global) +jsonw_string_field (jw_global,"viaIf" , + ifname); +else +fprintf (fp,"via %s " , ifname); + }else { +if (jw_global) +jsonw_uint_field (jw_global,"viaIfIndex" , + ifindex); +else +fprintf (fp,"via ifindex %u " , ifindex); + } } } -if (tb[NDA_LINK_NETNSID ]) -fprintf (fp,"link-netnsid %d " , -rta_getattr_u32 (tb[NDA_LINK_NETNSID ])); + +if (tb[NDA_LINK_NETNSID ]) { +if (jw_global) +jsonw_uint_field (jw_global,"linkNetNsId" , +rta_getattr_u32 (tb[NDA_LINK_NETNSID ])); +else +fprintf (fp,"link-netnsid %d " , +rta_getattr_u32 (tb[NDA_LINK_NETNSID ])); + }if (show_stats && tb[NDA_CACHEINFO ]) {struct nda_cacheinfo *ci =RTA_DATA (tb[NDA_CACHEINFO ]);int hz =get_user_hz (); -fprintf (fp,"used %d/%d " , ci->ndm_used/hz, - ci->ndm_updated/hz); +if (jw_global) { +jsonw_uint_field (jw_global,"used" , + ci->ndm_used/hz); +jsonw_uint_field (jw_global,"updated" , + ci->ndm_updated/hz); + }else { +fprintf (fp,"used %d/%d " , ci->ndm_used/hz, + ci->ndm_updated/hz); + } + } + +if (jw_global) { +if (r->ndm_flags &NTF_SELF ) { +start_json_fdb_flags_array (&fdb_flags); +jsonw_string (jw_global,"self" ); + } +if (r->ndm_flags &NTF_ROUTER ) { +start_json_fdb_flags_array (&fdb_flags); +jsonw_string (jw_global,"router" ); + } +if (r->ndm_flags &NTF_EXT_LEARNED ) { +start_json_fdb_flags_array (&fdb_flags); +jsonw_string (jw_global,"offload" ); + } +if (r->ndm_flags &NTF_MASTER ) +jsonw_string (jw_global,"master" ); +if (fdb_flags) +jsonw_end_array (jw_global); + +if (tb[NDA_MASTER ]) +jsonw_string_field (jw_global, +"master" , +ll_index_to_name (rta_getattr_u32 (tb[NDA_MASTER ]))); + + }else { +if (r->ndm_flags &NTF_SELF ) +fprintf (fp,"self " ); +if (r->ndm_flags &NTF_ROUTER ) +fprintf (fp,"router " ); +if (r->ndm_flags &NTF_EXT_LEARNED ) +fprintf (fp,"offload " ); +if (tb[NDA_MASTER ]) { +fprintf (fp,"master %s " , +ll_index_to_name (rta_getattr_u32 (tb[NDA_MASTER ]))); + }else if (r->ndm_flags &NTF_MASTER ) { +fprintf (fp,"master " ); + } + } + + state_s =state_n2a (r->ndm_state); +if (jw_global) { +if (state_s[0 ]) +jsonw_string_field (jw_global,"state" , state_s); + +jsonw_end_object (jw_global); + }else { +fprintf (fp,"%s\n" , state_s); + +fflush (fp); } -if (r->ndm_flags &NTF_SELF ) -fprintf (fp,"self " ); -if (tb[NDA_MASTER ]) -fprintf (fp,"master %s " , -ll_index_to_name (rta_getattr_u32 (tb[NDA_MASTER ]))); -else if (r->ndm_flags &NTF_MASTER ) -fprintf (fp,"master " ); -if (r->ndm_flags &NTF_ROUTER ) -fprintf (fp,"router " ); -if (r->ndm_flags &NTF_EXT_LEARNED ) -fprintf (fp,"offload " ); - -fprintf (fp,"%s\n" ,state_n2a (r->ndm_state)); -fflush (fp);return 0 ; }
@@ -233,10 +345,19 @@ static int fdb_show(int argc, char **argv)exit (1 ); } +if (json_output) { + jw_global =jsonw_new_array (stdout); +if (!jw_global) { +fprintf (stderr,"Error allocation json object\n" ); +exit (1 ); + } + }if (rtnl_dump_filter (&rth, print_fdb, stdout) <0 ) {fprintf (stderr,"Dump terminated\n" );exit (1 ); } +if (jw_global) +jsonw_destroy (&jw_global);return 0 ; }
Signature · 1 line
1.9.1
PATCH v3 4/5 bridge: add json schema for bridge fdb show
In reply to Roopa PrabhuFrom: Anuradha Karuppiah <[email protected]>
we think storing the schema file for the json
format will be useful.
--- schema/bridge_fdb_schema.json | 62 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 schema/bridge_fdb_schema.json
schema/bridge_fdb_schema.json new file +62 -0
@@ -0,0 +1,62 @@ +{ + "$schema": "http://json-schema.org/draft-04/schema#", + "description": "bridge fdb show", + "type": "array", + "items": { + "type": "object", + "properties": { + "dev": { + "type": "string" + }, + "dst": { + "description" : "host name or ip address", + "type": "string" + }, + "flags": { + "type": "array", + "items": { + "enum": ["self", "master", "router", "offload"] + }, + "uniqueItems": true + }, + "linkNetNsId": { + "type": "integer" + }, + "mac": { + "type": "string" + }, + "master": { + "type": "string" + }, + "opCode": { + "description" : "used to indicate fdb entry del", + "enum": ["deleted"] + }, + "port": { + "type": "integer" + }, + "state": { + "description" : "permanent, static, stale, state=#x", + "type": "string" + }, + "updated": { + "type": "integer" + }, + "used": { + "type": "integer" + }, + "viaIf": { + "type": "string" + }, + "viaIfIndex": { + "type": "integer" + }, + "vlan": { + "type": "integer" + }, + "vni": { + "type": "integer" + } + } + } +}
Signature · 1 line
1.9.1
PATCH v3 5/5 bridge: update man page
In reply to Roopa PrabhuFrom: Roopa Prabhu <[email protected]>
--- man/man8/bridge.8 | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-)
man/man8/bridge.8 +7 -2
@@ -20,8 +20,9 @@ bridge \- show / manipulate bridge addresses and devices .IR OPTIONS " := { " \fB\-V\fR[\fIersion\fR] | \fB\-s\fR[\fItatistics\fR] | -\fB\-n\fR[\fIetns\fR] name } -\fB\-b\fR[\fIatch\fR] filename } +\fB\-n\fR[\fIetns\fR] name | +\fB\-b\fR[\fIatch\fR] filename | +\fB\-j\fR[\fIson\fR] } .ti -8 .BR "bridge link set"
@@ -153,6 +154,10 @@ Don't terminate bridge command on errors in batch mode. If there were any errors during execution of the commands, the application return code will be non zero. +.TP +.BR "\-json" +Display results in JSON format. Currently available for vlan and fdb. + .SH BRIDGE - COMMAND SYNTAX .SS
Signature · 1 line
1.9.1
PATCH v3 1/5 json_writer: allow base json data type to be array or object
In reply to Roopa PrabhuFrom: Anuradha Karuppiah <[email protected]>
This patch adds a type qualifier to json_writer. Type can be a json object or array. This can be extended to other types like json-string, json-number etc in the future.
--- include/json_writer.h | 5 +++-- lib/json_writer.c | 39 +++++++++++++++++++++++++++++++++++---- misc/ifstat.c | 6 +++--- misc/lnstat.c | 2 +- misc/nstat.c | 4 ++-- 5 files changed, 44 insertions(+), 12 deletions(-)
include/json_writer.h +3 -2
@@ -21,8 +21,9 @@/* Opaque class structure */ typedef struct json_writer json_writer_t ; -/* Create a new JSON stream */ -json_writer_t *jsonw_new (FILE *f); +/* Create a new JSON stream with data type */ +json_writer_t *jsonw_new_object (FILE *f); +json_writer_t *jsonw_new_array (FILE *f);/* End output to JSON stream */ void jsonw_destroy (json_writer_t **self_p);
lib/json_writer.c +35 -4
@@ -22,11 +22,17 @@#include "json_writer.h" +enum jsonw_data_type { +JSONW_TYPE_OBJECT , +JSONW_TYPE_ARRAY +}; +struct json_writer {FILE *out;/* output file */ unsigned depth;/* nesting */ bool pretty;/* optional whitepace */ char sep;/* either nul or comma */ +int type;/* currently either object or array */ };/* indentation for pretty print */
@@ -94,7 +100,7 @@ static void jsonw_puts(json_writer_t *self, const char *str) }/* Create a new JSON stream */ -json_writer_t *jsonw_new (FILE *f) +static json_writer_t *jsonw_new (FILE *f,int type) {json_writer_t *self =malloc (sizeof (*self));if (self) {
@@ -102,11 +108,29 @@ json_writer_t *jsonw_new(FILE *f) self->depth =0 ; self->pretty = false; self->sep ='\0' ; -putc ('{' , self->out); + self->type = type; +switch (self->type) { +case JSONW_TYPE_OBJECT : +putc ('{' , self->out); +break ; +case JSONW_TYPE_ARRAY : +putc ('[' , self->out); +break ; + } }return self; } +json_writer_t *jsonw_new_object (FILE *f) +{ +return jsonw_new (f,JSONW_TYPE_OBJECT ); +} + +json_writer_t *jsonw_new_array (FILE *f) +{ +return jsonw_new (f,JSONW_TYPE_ARRAY ); +} +/* End output to JSON stream */ void jsonw_destroy (json_writer_t **self_p) {
@@ -114,7 +138,14 @@ void jsonw_destroy(json_writer_t **self_p)assert (self->depth ==0 );jsonw_eol (self); -fputs ("}\n" , self->out); +switch (self->type) { +case JSONW_TYPE_OBJECT : +fputs ("}\n" , self->out); +break ; +case JSONW_TYPE_ARRAY : +fputs ("]\n" , self->out); +break ; + }fflush (self->out);free (self); *self_p =NULL ;
@@ -267,7 +298,7 @@ void jsonw_null_field(json_writer_t *self, const char *prop)#ifdef TEST int main (int argc,char **argv) { -json_writer_t *wr =jsonw_new (stdout); +json_writer_t *wr =jsonw_new_object (stdout);jsonw_pretty (wr, true);jsonw_name (wr,"Vyatta" );
misc/ifstat.c +3 -3
@@ -240,7 +240,7 @@ 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 ; +json_writer_t *jw = json_output ?jsonw_new_object (fp) :NULL ;struct ifstat_ent *n, *h; h = hist_db;
@@ -447,7 +447,7 @@ 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 ; +json_writer_t *jw = json_output ?jsonw_new_object (fp) :NULL ;struct ifstat_ent *n;if (jw) {
@@ -473,7 +473,7 @@ static void dump_kern_db(FILE *fp)static void dump_incr_db (FILE *fp) {struct ifstat_ent *n, *h; -json_writer_t *jw = json_output ?jsonw_new (fp) :NULL ; +json_writer_t *jw = json_output ?jsonw_new_object (fp) :NULL ; h = hist_db;if (jw) {
misc/lnstat.c +1 -1
@@ -110,7 +110,7 @@ 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); +json_writer_t *jw =jsonw_new_object (of);int i;jsonw_start_object (jw);
misc/nstat.c +2 -2
@@ -279,7 +279,7 @@ 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 ; +json_writer_t *jw = json_output ?jsonw_new_object (fp) :NULL ;struct nstat_ent *n, *h; h = hist_db;
@@ -323,7 +323,7 @@ static void dump_kern_db(FILE *fp, int to_hist)static void dump_incr_db (FILE *fp) { -json_writer_t *jw = json_output ?jsonw_new (fp) :NULL ; +json_writer_t *jw = json_output ?jsonw_new_object (fp) :NULL ;struct nstat_ent *n, *h; h = hist_db;
Signature · 1 line
1.9.1
Re: [PATCH iproute2 net-next v3 1/5] json_writer: allow base json data type to be array or object
Quoted 7 lines · On Mon, 20 Jun 2016 23:39:43 -0700 Roopa Prabhu <[email protected]>…
From: Anuradha Karuppiah <[email protected]>
This patch adds a type qualifier to
json_writer. Type can be a json object or array. This can be extended to other types like json-string, json-number etc in the future.Signed-off-by Anuradha Karuppiah <[email protected]>
Since json writer is not used in many places yet, why not just get rid of the automatic object in the constructor.
lib/json_writer.c +0 -2
@@ -102,7 +102,6 @@ json_writer_t *jsonw_new(FILE *f) self->depth =0 ; self->pretty = false; self->sep ='\0' ; -putc ('{' , self->out); }return self; }
@@ -114,7 +113,6 @@ void jsonw_destroy(json_writer_t **self_p)assert (self->depth ==0 );jsonw_eol (self); -fputs ("}\n" , self->out);fflush (self->out);free (self); *self_p =NULL ;
misc/ifstat.c +4 -5
@@ -246,7 +246,6 @@ static void dump_raw_db(FILE *fp, int to_hist) h = hist_db;if (jw) {jsonw_pretty (jw, pretty); -jsonw_name (jw, info_source);jsonw_start_object (jw); }else fprintf (fp,"#%s\n" , info_source);
@@ -452,7 +451,6 @@ static void dump_kern_db(FILE *fp)if (jw) {jsonw_pretty (jw, pretty); -jsonw_name (jw, info_source);jsonw_start_object (jw); }else print_head (fp);
@@ -466,8 +464,10 @@ static void dump_kern_db(FILE *fp)else print_one_if (fp, n, n->val); } -if (json_output) -fprintf (fp,"\n} }\n" ); +if (jw) { +jsonw_end_object (jw); +jsonw_destroy (&jw); + } }static void dump_incr_db (FILE *fp)
@@ -478,7 +478,6 @@ static void dump_incr_db(FILE *fp) h = hist_db;if (jw) {jsonw_pretty (jw, pretty); -jsonw_name (jw, info_source);jsonw_start_object (jw); }else print_head (fp);
misc/nstat.c +0 -1
@@ -285,7 +285,6 @@ static void dump_kern_db(FILE *fp, int to_hist) h = hist_db;if (jw) {jsonw_pretty (jw, pretty); -jsonw_name (jw, info_source);jsonw_start_object (jw); }else fprintf (fp,"#%s\n" , info_source);
Re: [PATCH iproute2 net-next v3 1/5] json_writer: allow base json data type to be array or object
Quoted 5 lines · Stephen Hemminger
Quoted 2 lines · On Mon, 20 Jun 2016 23:39:43 -0700 Roopa Prabhu <[email protected]>…
From: Anuradha Karuppiah <[email protected]>
Quoted 8 lines
Quoted 5 lines
This patch adds a type qualifier to
json_writer. Type can be a json object or array. This can be extended to other types like json-string, json-number etc in the future.Signed-off-by Anuradha Karuppiah <[email protected]>Since json writer is not used in many places yet, why not just get rid of the automatic object in the constructor.
I wanted to force the external api to start with an json-object or json-array. It reduces the chance of mistakes vs. a typeless constructor. With a typeless constructor you can accidentally end up with a json output that doesn't pass json lint; especially if optional params are being suppressed at different places.
Quoted 76 lines
lib/json_writer.c +0
-2@@ -102,7 +102,6 @@ json_writer_t *jsonw_new(FILE *f) self->depth =0 ; self->pretty = false; self->sep ='\0' ; -putc ('{' , self->out); }return self; }@@ -114,7 +113,6 @@ void jsonw_destroy(json_writer_t **self_p)assert (self->depth ==0 );jsonw_eol (self); -fputs ("}\n" , self->out);fflush (self->out);free (self); *self_p =NULL ;misc/ifstat.c +4
-5@@ -246,7 +246,6 @@ static void dump_raw_db(FILE *fp, int to_hist) h = hist_db;if (jw) {jsonw_pretty (jw, pretty); -jsonw_name (jw, info_source);jsonw_start_object (jw); }else fprintf (fp,"#%s\n" , info_source);@@ -452,7 +451,6 @@ static void dump_kern_db(FILE *fp)if (jw) {jsonw_pretty (jw, pretty); -jsonw_name (jw, info_source);jsonw_start_object (jw); }else print_head (fp);@@ -466,8 +464,10 @@ static void dump_kern_db(FILE *fp)else print_one_if (fp, n, n->val); } -if (json_output) -fprintf (fp,"\n} }\n" ); +if (jw) { +jsonw_end_object (jw); +jsonw_destroy (&jw); + } }static void dump_incr_db (FILE *fp)@@ -478,7 +478,6 @@ static void dump_incr_db(FILE *fp) h = hist_db;if (jw) {jsonw_pretty (jw, pretty); -jsonw_name (jw, info_source);jsonw_start_object (jw); }else print_head (fp);misc/nstat.c +0
-1@@ -285,7 +285,6 @@ static void dump_kern_db(FILE *fp, int to_hist) h = hist_db;if (jw) {jsonw_pretty (jw, pretty); -jsonw_name (jw, info_source);jsonw_start_object (jw); }else fprintf (fp,"#%s\n" , info_source);
Re: [PATCH iproute2 net-next v3 1/5] json_writer: allow base json data type to be array or object
Quoted 13 lines · On Tue, 21 Jun 2016 09:24:50 -0700 Anuradha Karuppiah…
Quoted 11 lines · Stephen Hemminger
Quoted 7 lines · On Mon, 20 Jun 2016 23:39:43 -0700 Roopa Prabhu <[email protected]>…
From: Anuradha Karuppiah <[email protected]>
This patch adds a type qualifier to
json_writer. Type can be a json object or array. This can be extended to other types like json-string, json-number etc in the future.Signed-off-by Anuradha Karuppiah <[email protected]>
Quoted 8 lines
Quoted 2 lines
Since json writer is not used in many places yet, why not just get rid of the automatic object in the constructor.
I wanted to force the external api to start with an json-object or json-array. It reduces the chance of mistakes vs. a typeless constructor. With a typeless constructor you can accidentally end up with a json output that doesn't pass json lint; especially if optional params are being suppressed at different places.
Still, this is not how jsonwriter works in .NET, Android, or Java. It is easily confusing to developers if similar API's behave differently. Kind of like if printf() always appended a new line on some platforms.