better json printing

This commit is contained in:
Olof hagsand 2016-09-22 16:57:26 +02:00
parent fe91ba707d
commit b82a8bae26
5 changed files with 33 additions and 28 deletions

View file

@ -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;
}
}

View file

@ -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)

View file

@ -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:

View file

@ -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 */

View file

@ -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 level = 0;
int i;
cxobj *xc;
cprintf(cb, "%*s{%s",
pretty?(level*JSON_INDENT):0,"",
pretty?"\n":"");
if (top){
if (xml2json1_cbuf(cb, x, level+1, pretty) < 0)
goto done;
}
else{
for (i=0; i<xml_child_nr(x); i++){
xc = xml_child_i(x, i);
if (xml2json1_cbuf(cb, xc, level+1, pretty) < 0)
goto done;
}
}
cprintf(cb, "%*s}%s",
pretty?(level*JSON_INDENT):0,"",
pretty?"\n":"");
@ -332,7 +345,8 @@ xml2json_cbuf(cbuf *cb,
/*! Translate from xml tree to JSON and print to file
* @param[in] f File to print to
* @param[in] x XML tree to translate from
* @param[in] level Indentation level
* @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
*
@ -344,7 +358,8 @@ xml2json_cbuf(cbuf *cb,
int
xml2json(FILE *f,
cxobj *x,
int pretty)
int pretty,
int top)
{
int retval = 1;
cbuf *cb = NULL;
@ -353,7 +368,7 @@ xml2json(FILE *f,
clicon_err(OE_XML, errno, "cbuf_new");
goto done;
}
if (xml2json_cbuf(cb, x, pretty) < 0)
if (xml2json_cbuf(cb, x, pretty, top) < 0)
goto done;
fprintf(f, "%s", cbuf_get(cb));
retval = 0;