Skip to content

Conversation

@1bronze
Copy link

@1bronze 1bronze commented Jan 15, 2026

This PR addresses issue #18808 regarding locale-dependent JSON serialization in PHP.

Problem

When the system environment is set to a locale that uses a comma as a decimal separator (like de_DE), float and double values were being serialized into JSON as 3,14 instead of 3.14. This produced invalid JSON that could not be parsed by other systems.

Solution

  • Updated php/src/Google/Protobuf/Internal/GPBJsonWire.php to use sprintf("%.*h", ...) for formatting values. This ensures that the decimal separator is always a dot (.), making the output locale-independent.
  • Added a new test case testJsonEncodeFloatLocaleIndependent in php/tests/EncodeDecodeTest.php. This test explicitly sets the locale to de_DE to verify that the fix works correctly and produces valid JSON.

Verification

  • Added new regression test.
  • Verified that all PHP tests pass locally: vendor/bin/phpunit php/tests/EncodeDecodeTest.php

Fixes #18808

Use `sprintf` with `%h` specifier to format float and double values.
This ensures the decimal separator is always a dot (.), regardless
of the system's current locale settings (e.g. de_DE).
This fixes an issue where floats were serialized with commas (e.g. "3,14")
resulting in invalid JSON.

Fixes protocolbuffers#18808
@1bronze 1bronze requested a review from a team as a code owner January 15, 2026 09:56
@1bronze 1bronze requested review from bshaffer and removed request for a team January 15, 2026 09:56
@google-cla
Copy link

google-cla bot commented Jan 15, 2026

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

@bshaffer bshaffer added the 🅰️ safe for tests Mark a commit as safe to run presubmits over label Jan 15, 2026
@github-actions github-actions bot removed the 🅰️ safe for tests Mark a commit as safe to run presubmits over label Jan 15, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

2 participants