Commit 0b60d970 authored by Niels's avatar Niels

make code independent of concrete locale

parent 31bccc83
...@@ -88,6 +88,19 @@ struct has_mapped_type ...@@ -88,6 +88,19 @@ struct has_mapped_type
static constexpr bool value = sizeof(test<T>(0)) == 1; static constexpr bool value = sizeof(test<T>(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<char>
{
protected:
char do_decimal_point() const
{
return '.';
}
};
} }
/*! /*!
...@@ -6161,7 +6174,7 @@ class basic_json ...@@ -6161,7 +6174,7 @@ class basic_json
{ {
// no exponent - output as a decimal // no exponent - output as a decimal
std::stringstream ss; 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) ss << std::setprecision(m_type.bits.precision)
<< std::fixed << m_value.number_float; << std::fixed << m_value.number_float;
o << ss.str(); o << ss.str();
...@@ -6182,7 +6195,7 @@ class basic_json ...@@ -6182,7 +6195,7 @@ class basic_json
// to be safe, we read this value from // to be safe, we read this value from
// std::numeric_limits<number_float_t>::digits10 // std::numeric_limits<number_float_t>::digits10
std::stringstream ss; 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<double>::digits10) ss << std::setprecision(std::numeric_limits<double>::digits10)
<< m_value.number_float; << m_value.number_float;
o << ss.str(); o << ss.str();
......
...@@ -88,6 +88,19 @@ struct has_mapped_type ...@@ -88,6 +88,19 @@ struct has_mapped_type
static constexpr bool value = sizeof(test<T>(0)) == 1; static constexpr bool value = sizeof(test<T>(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<char>
{
protected:
char do_decimal_point() const
{
return '.';
}
};
} }
/*! /*!
...@@ -6161,7 +6174,7 @@ class basic_json ...@@ -6161,7 +6174,7 @@ class basic_json
{ {
// no exponent - output as a decimal // no exponent - output as a decimal
std::stringstream ss; 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) ss << std::setprecision(m_type.bits.precision)
<< std::fixed << m_value.number_float; << std::fixed << m_value.number_float;
o << ss.str(); o << ss.str();
...@@ -6182,7 +6195,7 @@ class basic_json ...@@ -6182,7 +6195,7 @@ class basic_json
// to be safe, we read this value from // to be safe, we read this value from
// std::numeric_limits<number_float_t>::digits10 // std::numeric_limits<number_float_t>::digits10
std::stringstream ss; 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<double>::digits10) ss << std::setprecision(std::numeric_limits<double>::digits10)
<< m_value.number_float; << m_value.number_float;
o << ss.str(); o << ss.str();
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment