Skip to content

Commit

Permalink
Fixes serialization of small floats
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
jrandall committed Feb 28, 2015
1 parent ef3ad89 commit 7bfcbe2
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions src/json.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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<char> buf(sz + 1);
std::snprintf(&buf[0], buf.size(), "%.15g", m_value.number_float);
string_t formatted = buf.data();
return formatted;
}

default:
Expand Down

0 comments on commit 7bfcbe2

Please sign in to comment.