From c391265484ecb3baa2a00c80658c99ad65963ec6 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Jos=C3=A9=20Fonseca?= Date: Fri, 26 Oct 2012 23:45:35 +0100 Subject: [PATCH] Allow NaN, Infinity in JSON. Non-standard but widely support (json.py, qjson) --- retrace/json.hpp | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/retrace/json.hpp b/retrace/json.hpp index 2f50a8c..4ad2ab3 100644 --- a/retrace/json.hpp +++ b/retrace/json.hpp @@ -37,8 +37,9 @@ #ifdef _MSC_VER # include # define isfinite _finite +# define isnan _isnan #else -# include // isfinite +# include // isfinite, isnan #endif #include @@ -335,15 +336,21 @@ public: template inline void writeNumber(T n) { - if (!isfinite(n)) { - // NaN/Inf - writeNull(); + separator(); + if (isnan(n)) { + // NaN is non-standard but widely supported + os << "NaN"; + } else if (!isfinite(n)) { + // Infinite is non-standard but widely supported + if (n < 0) { + os << '-'; + } + os << "Infinity"; } else { - separator(); os << std::dec << std::setprecision(std::numeric_limits::digits10 + 1) << n; - value = true; - space = ' '; } + value = true; + space = ' '; } inline void writeStringMember(const char *name, const char *s) { -- 2.43.0