Skip to content

PHP Json serialization of floats is locale aware and producing invalid Json #18808

@finwe

Description

@finwe

Probably a regression of this issue #8179

What version of protobuf and what language are you using?
Version: v4.28.2 installed with composer as a dependency of googleads/google-ads-php v24.0.0
Language: PHP

What operating system (Linux, Windows, ...) and version?
Linux Mint

What runtime / compiler are you using (e.g., python version or gcc version)
PHP 8.2

What did you do?
Steps to reproduce the behavior:

  • Have a default locale set to cs_CZ.utf8
  • Make a google ads api call that returns floats
  • Try calling serializeToJsonString on resulting stream

What did you expect to see

  • Resulting JSON is valid

What did you see instead?

  • The resulting JSON is invalid with commas as decimal separators

The error is at src/Google/Protobuf/Internal/GPBJsonWire.php:161 where the sprintf format is %.8g where g seems to be locale aware.

When using

$locale = setlocale(LC_ALL, 0);
setlocale(LC_ALL, 'C');
$result[] = Json::decode($googleAdsRow->serializeToJsonString(), Json::FORCE_ARRAY);
setlocale(LC_ALL, $locale);

The error is not present.

Anything else we should know about your project / environment
PHP installed is from the ondrej PPA

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions