If we are using values of type double in a json object, there are some serializing and
deserializing problems. This happens when the client running on a locale like german or spanish.
Please adapt your code like this:
jsonwriter.cpp:
...
std::string valueToString( double value )
{
char buffer[32];
#if defined(_MSC_VER) && defined(__STDC_SECURE_LIB__) // Use secure version with visual studio 2005 to avoid warning.
_locale_t common_loc = _create_locale(LC_NUMERIC, "C");
_sprintf_s_l(buffer, sizeof(buffer), "%#.16g", common_loc, value);
#else
std::string lc_num = setlocale(LC_NUMERIC, NULL);
setlocale(LC_NUMERIC, "C");
sprintf(buffer, "%#.16g", value);
setlocale(LC_NUMERIC, lc_num.c_str());
#endif
char* ch = buffer + strlen(buffer) - 1;
...
jsonreader.cpp
...
bool
Reader::decodeDouble( Token &token )
{
double value = 0;
const int bufferSize = 32;
int count;
int length = int(token.end_ - token.start_);
if ( length <= bufferSize )
{
Char buffer[bufferSize];
memcpy( buffer, token.start_, length );
buffer[length] = 0;
#if defined(_MSC_VER) && defined(__STDC_SECURE_LIB__)
_locale_t common_loc = _create_locale(LC_NUMERIC, "C");
count = _sscanf_l( buffer, "%lf", common_loc, &value );
#else
std::string lc_num = setlocale(LC_NUMERIC, NULL);
setlocale(LC_NUMERIC, "C");
count = sscanf( buffer, "%lf", &value );
setlocale(LC_NUMERIC, lc_num.c_str());
#endif
}
else
{
std::string buffer( token.start_, token.end_ );
#if defined(_MSC_VER) && defined(__STDC_SECURE_LIB__)
_locale_t common_loc = _create_locale(LC_NUMERIC, "C");
count = _sscanf_l( buffer.c_str(), "%lf", common_loc, &value );
#else
std::string lc_num = setlocale(LC_NUMERIC, NULL);
setlocale(LC_NUMERIC, "C");
count = sscanf( buffer.c_str(), "%lf", &value );
setlocale(LC_NUMERIC, lc_num.c_str());
#endif
...
I was just looking into this too, although calling setlocale isn't an ideal solution as this would affect all threads for the running program.
This bug affects linux as well, under locales with comma used as decimal separator. This completely breaks arrays, for example.
As for the solution, Marcus is right in that setlocale isn't ideal. For linux there is uselocale (see here for an example: https://sourceware.org/ml/libc-locales/2013-q2/msg00067.html) and in windows there are the _l variants of printf functions (http://msdn.microsoft.com/en-us/library/2ts7cx93.aspx)
To say that calling setlocale() isn't ideal is an understatement. This is completely out of the question for any library that uses jsoncpp.
This is fixed in versions
0.8.*
and1.4.*
.I don't think we need to alter the reader as the JSON standard requires dots, not commas.