Skip to content

Commit 0db9d6e

Browse files
committed
Merge pull request open-source-parsers#7 from cdunn2001/fix-locale
Use std::stringstream instead of snprintf() for double->string conversion
2 parents 06dcb1f + b8aaa03 commit 0db9d6e

File tree

1 file changed

+9
-16
lines changed

1 file changed

+9
-16
lines changed

src/lib_json/json_writer.cpp

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -63,22 +63,15 @@ std::string valueToString(UInt value) {
6363
#endif // # if defined(JSON_HAS_INT64)
6464

6565
std::string valueToString(double value) {
66-
// Allocate a buffer that is more than large enough to store the 16 digits of
67-
// precision requested below.
68-
char buffer[32];
69-
70-
// Print into the buffer. We need not request the alternative representation
71-
// that always has a decimal point because JSON doesn't distingish the
72-
// concepts of reals and integers.
73-
#if defined(_MSC_VER) && defined(__STDC_SECURE_LIB__) // Use secure version with
74-
// visual studio 2005 to
75-
// avoid warning.
76-
sprintf_s(buffer, sizeof(buffer), "%.16g", value);
77-
#else
78-
snprintf(buffer, sizeof(buffer), "%.16g", value);
79-
#endif
80-
81-
return buffer;
66+
// We need not request the alternative representation
67+
// that always has a decimal point because JSON doesn't distingish the
68+
// concepts of reals and integers.
69+
std::stringstream str;
70+
// Set locale to "C" to always get a '.' instead of a ','
71+
str.imbue(std::locale::classic());
72+
str.precision(16);
73+
str << value;
74+
return str.str();
8275
}
8376

8477
std::string valueToString(bool value) { return value ? "true" : "false"; }

0 commit comments

Comments
 (0)