From 7bfcbe2825f6755a6adcdb2cfb90f6e99ba3376e Mon Sep 17 00:00:00 2001 From: "Joshua C. Randall" Date: Sat, 28 Feb 2015 17:11:46 +0000 Subject: [PATCH] Fixes serialization of small floats Now uses std::snprintf() to generate a "%.15g" formatted string for JSON values of type number_float. 15 decimals digits are enough to round-trip an IEEE 754 double from string->double->string and get an identical result. std::snprintf is called twice. Once to determine the required buffer size and then again after allocating a buffer of that size. Note that the buffer size *could* be hardcoded for better performance. "%.15g" should result in strings of maximum length 23, plus one character for the terminating null for a buffer size of 24. --- src/json.hpp | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/json.hpp b/src/json.hpp index 66756d90..b3613ea3 100644 --- a/src/json.hpp +++ b/src/json.hpp @@ -1853,7 +1853,8 @@ 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 @@ -1961,7 +1962,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 + unsigned int sz = (unsigned int)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; } default: