Skip to content

Commit

Permalink
Merge branch 'feature/issue272' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
nlohmann committed Jun 28, 2016
2 parents 360f0f3 + 7214243 commit 6a108bb
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 10 deletions.
14 changes: 9 additions & 5 deletions src/json.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2097,6 +2097,8 @@ class basic_json
string_t dump(const int indent = -1) const
{
std::stringstream ss;
// fix locale problems
ss.imbue(std::locale(std::locale(), new DecimalSeparator));

if (indent >= 0)
{
Expand Down Expand Up @@ -5655,9 +5657,14 @@ class basic_json

// reset width to 0 for subsequent calls to this stream
o.width(0);
// fix locale problems
auto old_locale = o.imbue(std::locale(std::locale(), new DecimalSeparator));

// do the actual serialization
j.dump(o, pretty_print, static_cast<unsigned int>(indentation));

// reset locale
o.imbue(old_locale);
return o;
}

Expand Down Expand Up @@ -6128,11 +6135,8 @@ class basic_json
// string->double->string or string->long
// double->string; to be safe, we read this value from
// std::numeric_limits<number_float_t>::digits10
std::stringstream ss;
ss.imbue(std::locale(std::locale(), new DecimalSeparator)); // fix locale problems
ss << std::setprecision(std::numeric_limits<double>::digits10)
<< m_value.number_float;
o << ss.str();
o << std::setprecision(std::numeric_limits<double>::digits10)
<< m_value.number_float;
}
return;
}
Expand Down
14 changes: 9 additions & 5 deletions src/json.hpp.re2c
Original file line number Diff line number Diff line change
Expand Up @@ -2097,6 +2097,8 @@ class basic_json
string_t dump(const int indent = -1) const
{
std::stringstream ss;
// fix locale problems
ss.imbue(std::locale(std::locale(), new DecimalSeparator));

if (indent >= 0)
{
Expand Down Expand Up @@ -5655,9 +5657,14 @@ class basic_json

// reset width to 0 for subsequent calls to this stream
o.width(0);
// fix locale problems
auto old_locale = o.imbue(std::locale(std::locale(), new DecimalSeparator));

// do the actual serialization
j.dump(o, pretty_print, static_cast<unsigned int>(indentation));

// reset locale
o.imbue(old_locale);
return o;
}

Expand Down Expand Up @@ -6128,11 +6135,8 @@ class basic_json
// string->double->string or string->long
// double->string; to be safe, we read this value from
// std::numeric_limits<number_float_t>::digits10
std::stringstream ss;
ss.imbue(std::locale(std::locale(), new DecimalSeparator)); // fix locale problems
ss << std::setprecision(std::numeric_limits<double>::digits10)
<< m_value.number_float;
o << ss.str();
o << std::setprecision(std::numeric_limits<double>::digits10)
<< m_value.number_float;
}
return;
}
Expand Down
10 changes: 10 additions & 0 deletions test/src/unit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14079,6 +14079,16 @@ TEST_CASE("regression tests")
CHECK(j1a.dump() == "23.42");
CHECK(j1b.dump() == "23.42");

// check if locale is properly reset
std::stringstream ss;
ss.imbue(std::locale(std::locale(), new CommaDecimalSeparator));
ss << 47.11;
CHECK(ss.str() == "47,11");
ss << j1a;
CHECK(ss.str() == "47,1123.42");
ss << 47.11;
CHECK(ss.str() == "47,1123.4247,11");

CHECK(j2a.dump() == "23.42");
//issue #230
//CHECK(j2b.dump() == "23.42");
Expand Down

0 comments on commit 6a108bb

Please sign in to comment.