json escape etc

This commit is contained in:
Olof Hagsand 2016-10-06 20:58:41 +02:00
parent 7062a75fd7
commit 303f4a35a8
3 changed files with 38 additions and 13 deletions

View file

@ -218,7 +218,7 @@ api_data_get(clicon_handle h,
FCGX_FPrintF(r->out, "\r\n");
if (xml2json_cbuf_vec(cbx, vec, veclen, 0) < 0)
goto done;
FCGX_FPrintF(r->out, "[%s]", cbuf_get(cbx));
FCGX_FPrintF(r->out, "%s", cbuf_get(cbx));
FCGX_FPrintF(r->out, "\r\n\r\n");
retval = 0;
done:
@ -341,6 +341,7 @@ api_data_put(clicon_handle h,
FCGX_SetExitStatus(201, r->out); /* Created */
FCGX_FPrintF(r->out, "Content-Type: text/plain\r\n");
FCGX_FPrintF(r->out, "\r\n");
retval = 0;
done:
clicon_debug(1, "%s retval:%d", __FUNCTION__, retval);
if (xdata)

View file

@ -160,6 +160,32 @@ array_eval(cxobj *xprev,
return array;
}
char *
json_escape(char *str)
{
int i, j;
char *snew;
j = 0;
for (i=0;i<strlen(str);i++)
if (str[i]=='\n')
j++;
if ((snew = malloc(strlen(str)+1+j))==NULL){
clicon_err(OE_XML, errno, "malloc");
return NULL;
}
j = 0;
for (i=0;i<strlen(str);i++)
if (str[i]=='\n'){
snew[j++]='\\';
snew[j++]='n';
}
else
snew[j++]=str[i];
snew[j++]='\0';
return snew;
}
/*! Do the actual work of translating XML to JSON
* @param[out] cb Cligen text buffer containing json on exit
* @param[in] x XML tree structure containing XML to translate
@ -214,10 +240,14 @@ xml2json1_cbuf(cbuf *cb,
arraytype2str(arraytype),
childtype2str(childt));
switch(arraytype){
case BODY_ARRAY:
assert(xml_value(x));
cprintf(cb, "\"%s\"", xml_value(x));
case BODY_ARRAY:{
char *str;
if ((str = json_escape(xml_value(x))) == NULL)
goto done;
cprintf(cb, "\"%s\"", str);
free(str);
break;
}
case NO_ARRAY:
if (!flat)
cprintf(cb, "%*s\"%s\": ",

View file

@ -1269,7 +1269,6 @@ xmldb_put_tree_local(clicon_handle h,
char *key;
char *keys;
clicon_debug(1, "%s db:%s, api_path:%s", __FUNCTION__, db, api_path);
yspec = clicon_dbspec_yang(h);
if (db2file(h, db, &filename) < 0)
goto done;
@ -1325,14 +1324,9 @@ xmldb_put_tree_local(clicon_handle h,
i++;
switch (y->ys_keyword){
case Y_LEAF_LIST:
// val2 = vec[i]; /* No */
/* For leaf-list 'keys' is value, see 3.5.1 in restconf draft */
val2 = keys;
if (i>=nvec){
clicon_err(OE_XML, errno, "Leaf-list %s without argument", name);
goto done;
}
// i++;
cprintf(ckey, "/%s", val2);
cprintf(ckey, "/%s", keys);
break;
case Y_LIST:
if ((ykey = yang_find((yang_node*)y, Y_KEY, NULL)) == NULL){