PATCH: Fix for JSON output format
Thomas Mainka <thomas.mainka <at> gmx.de>
2012-09-05 20:32:44 GMT
Hi everyone,
Recently I have experimented with dynamic graphing of RRDtool data on
web pages (as JSON data queried by jQuery). While the JSON export and
transport worked fine, after some testing I realized that starting with
version 1.4 jQuery rejects JSON data which it thinks is malformed. When
directly fed the output output of "rrdtool xport --json" jQuery barfs
with an syntax error.
Of course there are workarounds, but the attached fix is pretty trivial:
Only the double quotes were missing in most of the JSON output from
RRDtool to pass the jQuery test. The JSON standard is pretty strict on
strings and requires any string to be wrapped in double quotes:
http://json.org/
Personally I have also tested the resulting output with
http://jsonlint.org/ and http://www.jsoneditoronline.org/ which both
worked for me. YMMV, though.
Regards,
Thomas
--- ../rrdtool-1.4.7.orig/src/rrd_tool.c 2012-01-24 11:08:48.000000000 +0100
+++ src/rrd_tool.c 2012-09-05 17:11:34.471626553 +0200
<at> <at> -737,13 +737,13 <at> <at>
printf(" <%s>\n", META_TAG);
}
else {
- printf("{ about: 'RRDtool xport JSON output',\n meta: {\n");
+ printf("{ \"about\": \"RRDtool xport JSON output\",\n \"meta\": {\n");
}
#define pXJV(indent,fmt,tag,value) \
if (json) { \
- printf(indent "%s: " fmt ",\n",tag,value); \
+ printf(indent "\"%s\": " fmt ",\n",tag,value); \
} else { \
printf(indent "<%s>" fmt "</%s>\n",tag,value,tag); \
}
<at> <at> -757,7 +757,7 <at> <at>
}
if (json){
- printf(" %s: [\n", LEGEND_TAG);
+ printf(" \"%s\": [\n", LEGEND_TAG);
}
else {
printf(" <%s>\n", LEGEND_TAG);
<at> <at> -766,7 +766,7 <at> <at>
char *entry = NULL;
entry = legend_v[j];
if (json){
- printf(" '%s'", entry);
+ printf(" \"%s\"", entry);
if (j < col_cnt -1){
printf(",");
}
<at> <at> -788,7 +788,7 <at> <at>
}
if (json){
- printf(" %s: [\n",DATA_TAG);
+ printf(" \"%s\": [\n",DATA_TAG);
} else {
printf(" <%s>\n", DATA_TAG);
}
_______________________________________________
rrd-developers mailing list
rrd-developers <at> lists.oetiker.ch
https://lists.oetiker.ch/cgi-bin/listinfo/rrd-developers