From fae87266906c10fc055eb270eddc622404696e63 Mon Sep 17 00:00:00 2001 From: Michael Tesch Date: Tue, 19 Mar 2019 11:12:54 +0100 Subject: [PATCH] Replace JSON inf and nan with JS compliant Infinity and NaN --- src/json_reporter.cc | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/src/json_reporter.cc b/src/json_reporter.cc index 26f94c6b..25beba01 100644 --- a/src/json_reporter.cc +++ b/src/json_reporter.cc @@ -23,6 +23,7 @@ #include #include #include +#include #include "string_util.h" #include "timers.h" @@ -53,10 +54,15 @@ std::string FormatKV(std::string const& key, double value) { std::stringstream ss; ss << '"' << key << "\": "; - const auto max_digits10 = std::numeric_limits::max_digits10; - const auto max_fractional_digits10 = max_digits10 - 1; - - ss << std::scientific << std::setprecision(max_fractional_digits10) << value; + if (std::isnan(value)) + ss << "NaN"; + else if (std::isinf(value)) + ss << (value < 0 ? "-" : "") << "Infinity"; + else { + const auto max_digits10 = std::numeric_limits::max_digits10; + const auto max_fractional_digits10 = max_digits10 - 1; + ss << std::scientific << std::setprecision(max_fractional_digits10) << value; + } return ss.str(); }