Merge pull request #7 from cdunn2001/fix-locale

Use std::stringstream instead of snprintf() for double->string conversion
This commit is contained in:
Christopher Dunn 2014-07-09 11:41:14 -07:00
commit 0db9d6ea01

View File

@ -63,22 +63,15 @@ std::string valueToString(UInt value) {
#endif // # if defined(JSON_HAS_INT64) #endif // # if defined(JSON_HAS_INT64)
std::string valueToString(double value) { std::string valueToString(double value) {
// Allocate a buffer that is more than large enough to store the 16 digits of // We need not request the alternative representation
// precision requested below.
char buffer[32];
// Print into the buffer. We need not request the alternative representation
// that always has a decimal point because JSON doesn't distingish the // that always has a decimal point because JSON doesn't distingish the
// concepts of reals and integers. // concepts of reals and integers.
#if defined(_MSC_VER) && defined(__STDC_SECURE_LIB__) // Use secure version with std::stringstream str;
// visual studio 2005 to // Set locale to "C" to always get a '.' instead of a ','
// avoid warning. str.imbue(std::locale::classic());
sprintf_s(buffer, sizeof(buffer), "%.16g", value); str.precision(16);
#else str << value;
snprintf(buffer, sizeof(buffer), "%.16g", value); return str.str();
#endif
return buffer;
} }
std::string valueToString(bool value) { return value ? "true" : "false"; } std::string valueToString(bool value) { return value ? "true" : "false"; }