From 0e8c5ba68f66b0056cc42373b4cd892b9667171d Mon Sep 17 00:00:00 2001 From: Jean-Claude Monnin Date: Thu, 4 Jun 2015 10:35:12 +0200 Subject: [PATCH] When dumping non-finite floating point values, output 'null'. In JSON the float point special values NaN and Infinity should serialised to 'null'. Previously, 'snprintf' in 'dump' was giving a string that isn't compliant to the JSON standard. --- json11.cpp | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/json11.cpp b/json11.cpp index 34f9098..0aa125b 100644 --- a/json11.cpp +++ b/json11.cpp @@ -21,6 +21,7 @@ #include "json11.hpp" #include +#include #include #include #include @@ -45,9 +46,13 @@ static void dump(std::nullptr_t, string &out) { } static void dump(double value, string &out) { - char buf[32]; - snprintf(buf, sizeof buf, "%.17g", value); - out += buf; + if (std::isfinite(value)) { + char buf[32]; + snprintf(buf, sizeof buf, "%.17g", value); + out += buf; + } else { + out += "null"; + } } static void dump(int value, string &out) {