pactl: fix invalid JSON output by overriding LC_NUMERIC

When the --format json parameter is given on the command line, we
attempt to produce a JSON output for most commands.

Our implementation of the JSON serialization uses vsnprintf to output
numbers. Unfortunately, vsnprintf is affected by the locale and more
specifically the LC_NUMERIC variable.

When LC_NUMERIC is set to, for instance, fr_FR.UTF-8, floating-point
numbers are output with a comma as the decimal separator, which is then
considered invalid JSON.

 $ LC_NUMERIC=fr_FR.UTF-8 pactl --format json list sinks | jq .
 parse error: Objects must consist of key:value pairs at line 1, column 435

This is the token which failed to parse:

 }},"balance":0,00,"base_volume":{

Fixed by overriding the LC_NUMERIC value when we request JSON output.

Signed-off-by: Olivier Gayot <olivier.gayot@sigexec.com>
Part-of: <https://gitlab.freedesktop.org/pulseaudio/pulseaudio/-/merge_requests/702>
This commit is contained in:
Olivier Gayot 2022-05-16 00:25:43 +02:00 committed by PulseAudio Marge Bot
parent 4f2c8ec002
commit c3c84a3691

View file

@ -2746,6 +2746,7 @@ int main(int argc, char *argv[]) {
format = TEXT;
} else if (pa_streq(opt_format, "json")) {
format = JSON;
setlocale(LC_NUMERIC, "C");
} else {
pa_log(_("Invalid format value '%s'"), opt_format);
goto quit;