Skip to content

Commit

Permalink
minor adjustments to pull request #38
Browse files Browse the repository at this point in the history
  • Loading branch information
nlohmann committed Mar 23, 2015
1 parent a382a93 commit 53e3da8
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 7 deletions.
12 changes: 7 additions & 5 deletions src/json.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ iterators allow a ReversibleContainer to be iterated over in reverse.

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <functional>
#include <initializer_list>
#include <iomanip>
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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<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;
return string_t(buf.data());
}

default:
Expand Down
12 changes: 10 additions & 2 deletions src/json.hpp.re2c
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ iterators allow a ReversibleContainer to be iterated over in reverse.

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <functional>
#include <initializer_list>
#include <iomanip>
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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<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);
return string_t(buf.data());
}

default:
Expand Down

0 comments on commit 53e3da8

Please sign in to comment.