Open PerMildner opened 3 years ago
Just a note that you can get the number as a string and then convert it yourself using a more suitable mechanism (for example, C++17 std::from_chars
).
Yup, this is a fundamental flaw of the C standard library, and there's no solution except for each library to implement their own strtod(). Locale configuration is global state, so it's not safe for libraries to manipulate it, even temporarily. See here:
https://github.com/mpv-player/mpv/commit/1e70e82baa9193f6f027338b0fab0f5078971fbe
As @boris-kolpackov said, you can access the raw string and parse it yourself. That also covers the case where the number being parsed has greater precision than a double, as JSON isn't limited to IEEE floats.
https://github.com/skeeto/pdjson/blob/67108d883061043e55d0fb13961ac1b6fc8a485c/pdjson.c#L850
strtod() is affected by the current locale and is therefore unsuitable.
For instance, setting the locale to a locale that uses comma instead of period, will incorrectly treat
123.45
as123
(stopping at the period, since the period is not part of the floating point number syntax).On macOS LC_ALL=sv_SE.UTF-8 sets such a locale, the name is different on Linux.
There is no test-case for
json_get_number()
.Reproducer: