Menu

#43 Locale problems on windows systems when using double

0.5.0
closed-fixed
nobody
Value (7)
5
2015-03-02
2012-02-02
gerdba
No

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
...

Related

Bugs: #61

Discussion

  • Marcus D. Hanwell

    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.

     
  • aqpage

    aqpage - 2014-04-09

    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)

     
  • Michi

    Michi - 2015-03-02

    To say that calling setlocale() isn't ideal is an understatement. This is completely out of the question for any library that uses jsoncpp.

     
  • Christopher Dunn

    • status: open --> closed-fixed
     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.