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

View file

@ -160,6 +160,32 @@ array_eval(cxobj *xprev,
return array; 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 /*! Do the actual work of translating XML to JSON
* @param[out] cb Cligen text buffer containing json on exit * @param[out] cb Cligen text buffer containing json on exit
* @param[in] x XML tree structure containing XML to translate * @param[in] x XML tree structure containing XML to translate
@ -214,10 +240,14 @@ xml2json1_cbuf(cbuf *cb,
arraytype2str(arraytype), arraytype2str(arraytype),
childtype2str(childt)); childtype2str(childt));
switch(arraytype){ switch(arraytype){
case BODY_ARRAY: case BODY_ARRAY:{
assert(xml_value(x)); char *str;
cprintf(cb, "\"%s\"", xml_value(x)); if ((str = json_escape(xml_value(x))) == NULL)
goto done;
cprintf(cb, "\"%s\"", str);
free(str);
break; break;
}
case NO_ARRAY: case NO_ARRAY:
if (!flat) if (!flat)
cprintf(cb, "%*s\"%s\": ", cprintf(cb, "%*s\"%s\": ",

View file

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