Added Floating point support to JSON

This commit is contained in:
Olof hagsand 2017-10-29 16:15:18 +01:00
parent 44612c57d8
commit 7b719cc6db
3 changed files with 86 additions and 2 deletions

View file

@ -2,6 +2,7 @@
## 3.3.3 Upcoming ## 3.3.3 Upcoming
* Added Floating point support to JSON
* Restconf: http cookie sent as attribute in rpc restconf_post operations to backend. * Restconf: http cookie sent as attribute in rpc restconf_post operations to backend.
* Added option CLICON_CLISPEC_FILE as complement to CLICON_CLISPEC_DIR to * Added option CLICON_CLISPEC_FILE as complement to CLICON_CLISPEC_DIR to
specify single CLI specification file, not only directory containing files. specify single CLI specification file, not only directory containing files.

View file

@ -624,3 +624,82 @@ json_parse_str(char *str,
return json_parse(str, "", *xt); return json_parse(str, "", *xt);
} }
/*
* Turn this on to get a json parse and pretty print test program
* Usage: xpath
* read xml from input
* Example compile:
gcc -g -o json -I. -I../clixon ./clixon_json.c -lclixon -lcligen
* Example run:
echo "" | xml
*/
#if 0 /* Test program */
static int
usage(char *argv0)
{
fprintf(stderr, "usage:%s.\n\tInput on stdin\n", argv0);
exit(0);
}
#define BUFLEN 1024 /* Size of xml read buffer */
int
main(int argc, char **argv)
{
cxobj *xt;
cxobj *xc;
cbuf *cb = cbuf_new();
int maxbuf = BUFLEN;
char *xmlbuf = NULL;
char *buf = NULL;
int i;
int c;
int len;
FILE *f = stdin;
if (argc != 1){
usage(argv[0]);
return 0;
}
clicon_log_init(__FILE__, LOG_INFO, CLICON_LOG_STDERR);
len = 1024; /* any number is fine */
if ((buf = malloc(len)) == NULL){
perror("malloc");
return -1;
}
memset(buf, 0, len);
i = 0; /* position in buf */
while (1){ /* read the whole file */
if ((c = fgetc(f)) == EOF)
break;
if (len==i){
if ((buf = realloc(buf, 2*len)) == NULL){
fprintf(stderr, "%s: realloc: %s\n", __FUNCTION__, strerror(errno));
goto done;
}
memset(buf+len, 0, len);
len *= 2;
}
buf[i++] = (char)(c&0xff);
} /* read a line */
if (json_parse_str(buf, &xt) < 0)
return -1;
xc = NULL;
while ((xc = xml_child_each(xt, xc, -1)) != NULL) {
xmltree2cbuf(cb, xc, 0); /* dump data structures */
//clicon_xml2cbuf(cb, xc, 0, 1); /* print xml */
}
fprintf(stdout, "%s", cbuf_get(cb));
if (xt)
xml_free(xt);
if (cb)
cbuf_free(cb);
done:
return 0;
}
#endif /* Test program */

View file

@ -77,6 +77,11 @@ clixon_json_parsewrap(void)
%} %}
digit [0-9]
integer {digit}+
real ({digit}+[.]{digit}*)|({digit}*[.]{digit}+)
exp ({integer}|{real})[eE][+-]{integer}
%x START %x START
%s STRING %s STRING
%s ESCAPE %s ESCAPE
@ -95,8 +100,7 @@ clixon_json_parsewrap(void)
<START>null { return J_NULL; } <START>null { return J_NULL; }
<START>false { return J_FALSE; } <START>false { return J_FALSE; }
<START>true { return J_TRUE; } <START>true { return J_TRUE; }
<START>[-+]?[0-9]+ { clixon_json_parselval.string = strdup(yytext); <START>({integer}|{real}|{exp}) { clixon_json_parselval.string = strdup(yytext); return J_NUMBER;}
return J_NUMBER;}
<START>. { return -1; } <START>. { return -1; }
<STRING>\" { BEGIN(START); return J_DQ; } <STRING>\" { BEGIN(START); return J_DQ; }
<STRING>\\ { BEGIN(ESCAPE); } <STRING>\\ { BEGIN(ESCAPE); }