From b82a8bae2616162d193b301a8829fa66c1c6d609 Mon Sep 17 00:00:00 2001 From: Olof hagsand Date: Thu, 22 Sep 2016 16:57:26 +0200 Subject: [PATCH] better json printing --- apps/cli/cli_common.c | 2 +- apps/cli/cli_show.c | 4 +--- apps/restconf/restconf_main.c | 9 +------- lib/clixon/clixon_json.h | 5 ++--- lib/src/clixon_json.c | 41 ++++++++++++++++++++++++----------- 5 files changed, 33 insertions(+), 28 deletions(-) diff --git a/apps/cli/cli_common.c b/apps/cli/cli_common.c index 1b7888f8..7ab70b54 100644 --- a/apps/cli/cli_common.c +++ b/apps/cli/cli_common.c @@ -835,7 +835,7 @@ cli_notification_cb(int s, void *arg) if (clicon_xml_parse_string(&eventstr, &xt) < 0) goto done; if ((xn = xml_child_i(xt, 0)) != NULL){ - if (xml2json(stdout, xn, 0) < 0) + if (xml2json(stdout, xn, 0, 1) < 0) goto done; } } diff --git a/apps/cli/cli_show.c b/apps/cli/cli_show.c index 70319730..60e60044 100644 --- a/apps/cli/cli_show.c +++ b/apps/cli/cli_show.c @@ -439,13 +439,11 @@ show_conf_as_json(clicon_handle h, cg_var *arg) { cxobj *xt = NULL; - cxobj *xc; int retval = -1; if (show_conf_as(h, cvv, arg, &xt) < 0) goto done; - xc = NULL; /* Dont print xt itself */ - xml2json(stdout, xt, 1); + xml2json(stdout, xt, 1, 0); retval = 0; done: if (xt) diff --git a/apps/restconf/restconf_main.c b/apps/restconf/restconf_main.c index b7f3a964..ebac125a 100644 --- a/apps/restconf/restconf_main.c +++ b/apps/restconf/restconf_main.c @@ -180,17 +180,10 @@ api_data_get(clicon_handle h, FCGX_FPrintF(r->out, "Content-Type: application/yang.data+xml\r\n"); FCGX_FPrintF(r->out, "\r\n"); -#if 0 - /* Iterate over result */ - if (xpath_vec(xt, cbuf_get(path1), &vec, &veclen) < 0) - goto done; -#endif if ((cbx = cbuf_new()) == NULL) goto done; - cprintf(cbx, "{\n"); - if (xml2json_cbuf(cbx, xt, 1) < 0) + if (xml2json_cbuf(cbx, xt, 1, 0) < 0) goto done; - cprintf(cbx, "}"); FCGX_FPrintF(r->out, "%s\r\n", cbuf_get(cbx)); retval = 0; done: diff --git a/lib/clixon/clixon_json.h b/lib/clixon/clixon_json.h index 0a3b9952..9bf443d4 100644 --- a/lib/clixon/clixon_json.h +++ b/lib/clixon/clixon_json.h @@ -27,8 +27,7 @@ * Prototypes */ int json_parse_str(char *str, cxobj **xt); - -int xml2json_cbuf(cbuf *cb, cxobj *x, int pretty); -int xml2json(FILE *f, cxobj *x, int pretty); +int xml2json_cbuf(cbuf *cb, cxobj *x, int pretty, int top); +int xml2json(FILE *f, cxobj *x, int pretty, int top); #endif /* _CLIXON_JSON_H */ diff --git a/lib/src/clixon_json.c b/lib/src/clixon_json.c index 22443068..905cda87 100644 --- a/lib/src/clixon_json.c +++ b/lib/src/clixon_json.c @@ -295,7 +295,8 @@ xml2json1_cbuf(cbuf *cb, * * @param[in,out] cb Cligen buffer to write to * @param[in] x XML tree to translate from - * @param[in] pretty set if the output should be pretty-printed + * @param[in] pretty Set if output is pretty-printed + * @param[in] top By default only children are printed, set if include top * @retval 0 OK * @retval -1 Error * @@ -311,16 +312,28 @@ xml2json1_cbuf(cbuf *cb, int xml2json_cbuf(cbuf *cb, cxobj *x, - int pretty) + int pretty, + int top) { - int retval = 1; - int level = 1; + int retval = 1; + int level = 0; + int i; + cxobj *xc; cprintf(cb, "%*s{%s", pretty?(level*JSON_INDENT):0,"", pretty?"\n":""); - if (xml2json1_cbuf(cb, x, level+1, pretty) < 0) - goto done; + if (top){ + if (xml2json1_cbuf(cb, x, level+1, pretty) < 0) + goto done; + } + else{ + for (i=0; i