Code

Make sure to print non-a-number as 'nan'.
authorSebastian Harl <sh@tokkee.org>
Mon, 17 Nov 2014 18:41:40 +0000 (19:41 +0100)
committerSebastian Harl <sh@tokkee.org>
Mon, 17 Nov 2014 18:41:40 +0000 (19:41 +0100)
Some versions of GNU libc use '-nan' instead which we don't want. For example,
Go's JSON package doesn't support that at all.

src/core/data.c
src/core/store.c

index 3186aa2622ecbfdc3caf2fc877dc7ae890c7754f..9f2b1f6fe4dda241997222ec62ac4fcdabf96eb0 100644 (file)
@@ -851,7 +851,10 @@ sdb_data_format(const sdb_data_t *datum, char *buf, size_t buflen, int quoted)
                ret = snprintf(buf, buflen, "%"PRIi64, datum->data.integer);
        }
        else if (datum->type == SDB_TYPE_DECIMAL) {
-               ret = snprintf(buf, buflen, "%g", datum->data.decimal);
+               if (isnan(datum->data.decimal))
+                       ret = snprintf(buf, buflen, "nan");
+               else
+                       ret = snprintf(buf, buflen, "%g", datum->data.decimal);
        }
        else if (datum->type == SDB_TYPE_STRING) {
                if (! datum->data.string)
index 455e5a328ae14c29d012906bd1057959d8ad15ae..ad59a2e99df3612178445edafe02eeabe3d9fa69 100644 (file)
@@ -43,6 +43,7 @@
 #include <stdlib.h>
 #include <string.h>
 
+#include <math.h>
 #include <pthread.h>
 
 /*
@@ -480,8 +481,13 @@ ts_tojson(sdb_timeseries_t *ts, sdb_strbuf_t *buf)
                                snprintf(time_str, sizeof(time_str), "<error>");
                        time_str[sizeof(time_str) - 1] = '\0';
 
-                       sdb_strbuf_append(buf, "{\"timestamp\": \"%s\", "
-                                       "\"value\": \"%f\"}", time_str, ts->data[i][j].value);
+                       /* Some GNU libc versions may print '-nan' which we dont' want */
+                       if (isnan(ts->data[i][j].value))
+                               sdb_strbuf_append(buf, "{\"timestamp\": \"%s\", "
+                                               "\"value\": \"nan\"}", time_str);
+                       else
+                               sdb_strbuf_append(buf, "{\"timestamp\": \"%s\", "
+                                               "\"value\": \"%f\"}", time_str, ts->data[i][j].value);
 
                        if (j < ts->data_len - 1)
                                sdb_strbuf_append(buf, ",");