From 420659f1870b0871dfaf31d106bf9fcc8bc0dfd1 Mon Sep 17 00:00:00 2001 From: Niels Date: Tue, 28 Jun 2016 10:42:17 +0200 Subject: [PATCH 1/2] addressing #272 --- src/json.hpp | 11 ++++++----- src/json.hpp.re2c | 11 ++++++----- test/src/unit.cpp | 4 ++++ 3 files changed, 16 insertions(+), 10 deletions(-) diff --git a/src/json.hpp b/src/json.hpp index b02a1344..3c57c42f 100644 --- a/src/json.hpp +++ b/src/json.hpp @@ -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) { @@ -5655,6 +5657,8 @@ 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)); // do the actual serialization j.dump(o, pretty_print, static_cast(indentation)); @@ -6128,11 +6132,8 @@ class basic_json // string->double->string or string->long // double->string; to be safe, we read this value from // std::numeric_limits::digits10 - std::stringstream ss; - ss.imbue(std::locale(std::locale(), new DecimalSeparator)); // fix locale problems - ss << std::setprecision(std::numeric_limits::digits10) - << m_value.number_float; - o << ss.str(); + o << std::setprecision(std::numeric_limits::digits10) + << m_value.number_float; } return; } diff --git a/src/json.hpp.re2c b/src/json.hpp.re2c index aba10b49..63141e20 100644 --- a/src/json.hpp.re2c +++ b/src/json.hpp.re2c @@ -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) { @@ -5655,6 +5657,8 @@ 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)); // do the actual serialization j.dump(o, pretty_print, static_cast(indentation)); @@ -6128,11 +6132,8 @@ class basic_json // string->double->string or string->long // double->string; to be safe, we read this value from // std::numeric_limits::digits10 - std::stringstream ss; - ss.imbue(std::locale(std::locale(), new DecimalSeparator)); // fix locale problems - ss << std::setprecision(std::numeric_limits::digits10) - << m_value.number_float; - o << ss.str(); + o << std::setprecision(std::numeric_limits::digits10) + << m_value.number_float; } return; } diff --git a/test/src/unit.cpp b/test/src/unit.cpp index fe3bc773..af16f00d 100644 --- a/test/src/unit.cpp +++ b/test/src/unit.cpp @@ -14079,6 +14079,10 @@ TEST_CASE("regression tests") CHECK(j1a.dump() == "23.42"); CHECK(j1b.dump() == "23.42"); + std::stringstream ss; + ss << j1a; + CHECK(ss.str() == "23.42"); + CHECK(j2a.dump() == "23.42"); //issue #230 //CHECK(j2b.dump() == "23.42"); From 7214243d89b166d8b52c18a997720a5fe4be2fe6 Mon Sep 17 00:00:00 2001 From: Niels Date: Tue, 28 Jun 2016 19:18:23 +0200 Subject: [PATCH 2/2] reset locale (for #272) --- src/json.hpp | 5 ++++- src/json.hpp.re2c | 5 ++++- test/src/unit.cpp | 8 +++++++- 3 files changed, 15 insertions(+), 3 deletions(-) diff --git a/src/json.hpp b/src/json.hpp index 3c57c42f..7adcbb43 100644 --- a/src/json.hpp +++ b/src/json.hpp @@ -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(indentation)); + + // reset locale + o.imbue(old_locale); return o; } diff --git a/src/json.hpp.re2c b/src/json.hpp.re2c index 63141e20..4b86afd6 100644 --- a/src/json.hpp.re2c +++ b/src/json.hpp.re2c @@ -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(indentation)); + + // reset locale + o.imbue(old_locale); return o; } diff --git a/test/src/unit.cpp b/test/src/unit.cpp index af16f00d..1f5c6d9f 100644 --- a/test/src/unit.cpp +++ b/test/src/unit.cpp @@ -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