From 0d7d7ec733f740fa35a41aa857a89abc1b8b8881 Mon Sep 17 00:00:00 2001 From: Sebastian Harl Date: Mon, 17 Nov 2014 19:41:40 +0100 Subject: [PATCH] Make sure to print non-a-number as 'nan'. 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 | 5 ++++- src/core/store.c | 10 ++++++++-- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/src/core/data.c b/src/core/data.c index 3186aa2..9f2b1f6 100644 --- a/src/core/data.c +++ b/src/core/data.c @@ -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) diff --git a/src/core/store.c b/src/core/store.c index 455e5a3..ad59a2e 100644 --- a/src/core/store.c +++ b/src/core/store.c @@ -43,6 +43,7 @@ #include #include +#include #include /* @@ -480,8 +481,13 @@ ts_tojson(sdb_timeseries_t *ts, sdb_strbuf_t *buf) snprintf(time_str, sizeof(time_str), ""); 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, ","); -- 2.30.2