jsoncpp icon indicating copy to clipboard operation
jsoncpp copied to clipboard

Unable to parse numbers if std::locale() changes the decimal

Open DosMike opened this issue 1 year ago • 0 comments

Describe the bug Hi, I'm not living in an English speaking country and as such I naturally set std::locale("") to make cpp use familiar number representations instead (This changes the decimal from a dot to a comma). There seems to be a problem where jsoncpp uses a string stream to parse decimals, and the input stream correctly uses the default locale set before, but this now differs from the locale required by Json.

To Reproduce Steps to reproduce the behavior:

  1. std::locale::global(std::locale("de_DE.UTF-8"));
  2. jsonReader.parse("[123.45]", root, false);

Expected behavior Should parse the number with dot as decimal without any issue.

Desktop (please complete the following information):

  • OS: Linux
  • Meson N/A
  • Ninja N/A

Additional context The solution is to imbue the streams with the local required by json like is.imbue(std::local("en_US.UTF-8")) for the reader: https://github.com/open-source-parsers/jsoncpp/blob/89e2973c754a9c02a49974d839779b151e95afd6/src/lib_json/json_reader.cpp#L590 https://github.com/open-source-parsers/jsoncpp/blob/89e2973c754a9c02a49974d839779b151e95afd6/src/lib_json/json_reader.cpp#L1624

Don't currently know if the writer is affected with sprintf: https://github.com/open-source-parsers/jsoncpp/blob/89e2973c754a9c02a49974d839779b151e95afd6/src/lib_json/json_writer.cpp#L127

Workaround is to temporarily set the locale to en_US manually

DosMike avatar Sep 11 '24 08:09 DosMike