Skip to content

Commit

Permalink
reset locale (for #272)
Browse files Browse the repository at this point in the history
  • Loading branch information
nlohmann committed Jun 28, 2016
1 parent 420659f commit 7214243
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 3 deletions.
5 changes: 4 additions & 1 deletion src/json.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5658,10 +5658,13 @@ class basic_json
// reset width to 0 for subsequent calls to this stream
o.width(0);
// fix locale problems
o.imbue(std::locale(std::locale(), new DecimalSeparator));
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
5 changes: 4 additions & 1 deletion src/json.hpp.re2c
Original file line number Diff line number Diff line change
Expand Up @@ -5658,10 +5658,13 @@ class basic_json
// reset width to 0 for subsequent calls to this stream
o.width(0);
// fix locale problems
o.imbue(std::locale(std::locale(), new DecimalSeparator));
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
8 changes: 7 additions & 1 deletion test/src/unit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14079,9 +14079,15 @@ 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() == "23.42");
CHECK(ss.str() == "47,1123.42");
ss << 47.11;
CHECK(ss.str() == "47,1123.4247,11");

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

0 comments on commit 7214243

Please sign in to comment.