From 53e3da8071db51357c7b534fa4a0b7b74409d4c2 Mon Sep 17 00:00:00 2001 From: Niels Date: Mon, 23 Mar 2015 20:27:30 +0100 Subject: [PATCH] minor adjustments to pull request #38 --- src/json.hpp | 12 +++++++----- src/json.hpp.re2c | 12 ++++++++++-- 2 files changed, 17 insertions(+), 7 deletions(-) diff --git a/src/json.hpp b/src/json.hpp index d8faba16..b845955f 100644 --- a/src/json.hpp +++ b/src/json.hpp @@ -32,6 +32,7 @@ iterators allow a ReversibleContainer to be iterated over in reverse. #include #include +#include #include #include #include @@ -1882,7 +1883,8 @@ class basic_json recursively. Note that - strings and object keys are escaped using escape_string() - - integer numbers are converted to a string before output using std::to_string() + - integer numbers are converted to a string before output using + std::to_string() - floating-point numbers are converted to a string using "%g" format @param prettyPrint whether the output shall be pretty-printed @@ -1991,12 +1993,12 @@ class basic_json case (value_t::number_float): { - // 15 digits of precision allows round-trip IEEE 754 string->double->string - unsigned int sz = (unsigned int)std::snprintf(nullptr, 0, "%.15g", m_value.number_float); + // 15 digits of precision allows round-trip IEEE 754 + // string->double->string + const auto sz = static_cast(std::snprintf(nullptr, 0, "%.15g", m_value.number_float)); std::vector buf(sz + 1); std::snprintf(&buf[0], buf.size(), "%.15g", m_value.number_float); - string_t formatted = buf.data(); - return formatted; + return string_t(buf.data()); } default: diff --git a/src/json.hpp.re2c b/src/json.hpp.re2c index b78afcd5..9d0a62cd 100644 --- a/src/json.hpp.re2c +++ b/src/json.hpp.re2c @@ -32,6 +32,7 @@ iterators allow a ReversibleContainer to be iterated over in reverse. #include #include +#include #include #include #include @@ -1882,7 +1883,9 @@ class basic_json recursively. Note that - strings and object keys are escaped using escape_string() - - numbers are converted to a string before output using std::to_string() + - integer numbers are converted to a string before output using + std::to_string() + - floating-point numbers are converted to a string using "%g" format @param prettyPrint whether the output shall be pretty-printed @param indentStep the indent level @@ -1990,7 +1993,12 @@ class basic_json case (value_t::number_float): { - return std::to_string(m_value.number_float); + // 15 digits of precision allows round-trip IEEE 754 + // string->double->string + const auto sz = static_cast(std::snprintf(nullptr, 0, "%.15g", m_value.number_float)); + std::vector buf(sz + 1); + std::snprintf(&buf[0], buf.size(), "%.15g", m_value.number_float); + return string_t(buf.data()); } default: