Steps
1.write a simple programm in c++ using jsoncpp that does :
a) create a Value of type double ("myDouble",3.5)
b) print Value using FastWriter or StyledWriter (or Value.toStyledString)
2.set locale to fr_FR.UTF-8 in a terminal (i guess all locales that use by default the "," character as decimal separator
3.execute program from the terminal
Result
I expect to see {"myDouble":3.5}
but I see {"myDouble":3,500000000000000}
Which version
OS: Fedora 18
jsoncpp: 0.6.0-0.9.rc2
Remarks
With a local as en_US, there is no problem.
Maybe there is an option that i didn't see, if it's so, sorry
Maybe it's not a bug, i guess it can appear logical that the library uses the default decimal separator of the local... But when a server expects a specific format for double, whatever the local is, the problem can be a real issue, and the workaround of changing locale is not really a good idea :/ Just for information, rapidjson behaves quite the same, boost doesn't.
It's clear that this issue wasn't considered, because the code of valueToString(double) assumes that the decimal point is a point, and truncates zeroes based on that, so this truncation doesn't happen when it's a comma.
Using the locale like this is inherent in printf which is used for the conversion (sprintf or sprintf_s for Windows). It would probably be best to use _sprintf_s_l instead of _sprintf_s and use a locale with a point, or to make this configurable. sprintf_l should work similarly on other platforms (using xlocale.h).
Here's the code I used to fix this:
_locale_t locale = _create_locale(LC_NUMERIC, "English");
_sprintf_s_l(buffer, sizeof(buffer), "%#.16g", locale, value);
_free_locale(locale);
Last edit: ET 2013-07-18
Hey, guys! Maybe it's too late for that, but I did some fix for that, please take a look if you can:
https://github.com/sklyadnev/json-cpp/commit/869e5ff29bbc5ce51e6712012b18e2e031bffe49
Comma is completely bad thing in double json values to my mind.
This is a duplicate of [#43]
Related
Bugs:
#43