From 0b60d970e916540ccc9abc4706c2621e174a0481 Mon Sep 17 00:00:00 2001 From: Niels Date: Tue, 5 Apr 2016 22:12:12 +0200 Subject: [PATCH] make code independent of concrete locale --- src/json.hpp | 17 +++++++++++++++-- src/json.hpp.re2c | 17 +++++++++++++++-- 2 files changed, 30 insertions(+), 4 deletions(-) diff --git a/src/json.hpp b/src/json.hpp index 13a6c49a..8f671fb0 100644 --- a/src/json.hpp +++ b/src/json.hpp @@ -88,6 +88,19 @@ struct has_mapped_type static constexpr bool value = sizeof(test(0)) == 1; }; +/*! +@brief helper class to create locales with decimal point +@sa https://github.com/nlohmann/json/issues/51#issuecomment-86869315 +*/ +class DecimalSeparator : public std::numpunct +{ + protected: + char do_decimal_point() const + { + return '.'; + } +}; + } /*! @@ -6161,7 +6174,7 @@ class basic_json { // no exponent - output as a decimal std::stringstream ss; - ss.imbue(std::locale("C")); + ss.imbue(std::locale(std::locale(), new DecimalSeparator)); // fix locale problems ss << std::setprecision(m_type.bits.precision) << std::fixed << m_value.number_float; o << ss.str(); @@ -6182,7 +6195,7 @@ class basic_json // to be safe, we read this value from // std::numeric_limits::digits10 std::stringstream ss; - ss.imbue(std::locale("C")); + 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(); diff --git a/src/json.hpp.re2c b/src/json.hpp.re2c index 70079144..ebf83d83 100644 --- a/src/json.hpp.re2c +++ b/src/json.hpp.re2c @@ -88,6 +88,19 @@ struct has_mapped_type static constexpr bool value = sizeof(test(0)) == 1; }; +/*! +@brief helper class to create locales with decimal point +@sa https://github.com/nlohmann/json/issues/51#issuecomment-86869315 +*/ +class DecimalSeparator : public std::numpunct +{ + protected: + char do_decimal_point() const + { + return '.'; + } +}; + } /*! @@ -6161,7 +6174,7 @@ class basic_json { // no exponent - output as a decimal std::stringstream ss; - ss.imbue(std::locale("C")); // fix locale problems + ss.imbue(std::locale(std::locale(), new DecimalSeparator)); // fix locale problems ss << std::setprecision(m_type.bits.precision) << std::fixed << m_value.number_float; o << ss.str(); @@ -6182,7 +6195,7 @@ class basic_json // to be safe, we read this value from // std::numeric_limits::digits10 std::stringstream ss; - ss.imbue(std::locale("C")); // fix locale problems + 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();