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
- [x] Added new regression test.
- [x] Verified that all PHP tests pass locally: `vendor/bin/phpunit php/tests/EncodeDecodeTest.php`
Fixes#18808Closes#25329
COPYBARA_INTEGRATE_REVIEW=https://github.com/protocolbuffers/protobuf/pull/25329 from khdkkhdd:18808-php-float-fix 5de303962e
PiperOrigin-RevId: 911348844
Unfortunately, because of inherent design choices in the ProtoJSON format, Field Masks of fields which contain an underscore followed by an uppercase letter or number cannot be successfully round tripped through the ProtoJSON representation.
The behavior today is that PHP-upb errors on this case but doesn't explain to the user why it has the check that it does. Pure-PHP produces JSON that will just not round-trip parse back.
This change improves the error message in the upb case, and adds a log-warning in the the pure-PHP case with a similar message.
Fixes https://github.com/protocolbuffers/protobuf/issues/25786
PiperOrigin-RevId: 908697453
This is a reopening of https://github.com/protocolbuffers/protobuf/issues/6035, where it was requested that someone contribute a PR. I chose the option name `EMIT_DEFAULTS` to match the ruby client and existing upd option. I tried to be relatively comprehensive with the tests, but let me know if there's anything stylistically or logically you want changed.
Locally the tests run happily:
```
php % git rev-parse HEAD
7cb3b2d9f60bdffce69c01143ca15c46d2eaa900
php % php -dextension=ext/google/protobuf/modules/protobuf.so -d error_reporting="E_ALL & ~E_DEPRECATED" vendor/bin/phpunit --bootstrap tests/force_c_ext.php tests/EncodeDecodeTest.php
PHPUnit 8.5.26 #StandWithUkraine
............................................................... 63 / 147 ( 42%)
............................................................... 126 / 147 ( 85%)
..................... 147 / 147 (100%)
Time: 49 ms, Memory: 6.00 MB
OK (147 tests, 1150 assertions)
```
Closes#23985
COPYBARA_INTEGRATE_REVIEW=https://github.com/protocolbuffers/protobuf/pull/23985 from SpencerMalone:php-default-value-option 6d8bc74056
PiperOrigin-RevId: 821657589
The php extension already had an implementation for encoding enums as integers, but it was not exposed, so add a bitmask param to `Message::serializeToJsonString()` to enable it as well as `preserve_proto_field_names`.
Converted the existing boolean parameter to preferentially be an integer (with boolean handling for BC), which accepts a bitmask of `Google\Protobuf\PrintOptions::*`, eg `PrintOptions::ALWAYS_PRINT_ENUMS_AS_INTS | PrintOptions::PRESERVE_PROTO_FIELD_NAMES`
`PrintOptions` class name and constant names were chosen to align with protobuf-cpp's implementation.
Implemented `preserve_proto_fieldnames` in the native version, to match the existing implementation in the extension (with tests).
Closes#12707
COPYBARA_INTEGRATE_REVIEW=https://github.com/protocolbuffers/protobuf/pull/12707 from brettmc:php-json-enum-as-int 515a0839b7
PiperOrigin-RevId: 725806573
Protobuf php lib encodes 123_000_000 nano like this: 2000-01-01T00:00:00.**123**Z but then it gets decoded into 123 nanoseconds instead of 123_000_000.
There were issue opened some time ago that also describes this behaviour #4335Closes#12396
COPYBARA_INTEGRATE_REVIEW=https://github.com/protocolbuffers/protobuf/pull/12396 from kindratmakc:bugfix/inconsistent-timestamp-json-encode-decode df47c96567
PiperOrigin-RevId: 603118615
* Updated PHP to the new version of upb.
This is a large change, as the upb API surface has been
renamed to follow Google style more closely.
* Fixed utf8_range.
* Updated Ruby for new utf8_range.
* Picked up new upb for PHP, with spelling fixes.
* Fixed the 32-bit build.
* [PHP] Fixed $msg->setMessage(null) to properly clear the message.
Fixes: https://github.com/protocolbuffers/protobuf/issues/8457
* Changed pure PHP to throw TypeError, and added a test for null.
* Added more tests and fixed null in setter for oneof.
* Fixed a bunch of incorrect arginfo and a few incorrect error messages.
* Passes mem check test with no leaks!
* WIP.
* Fix build warning that was causing Bazel build to fail.
* Added compatibility code for PHP <8.0.
* Added test_valgrind target and made tests Valgrind-clean.
* Updated Valgrind test to fail if memory leaks are detected.
* Removed intermediate shell script so commands are easier to cut, paste, and modify.
* Passing all Valgrind tests!
* Hoist addref into ObjCache_Get().
* Removed special case of map descriptors by keying object map on upb_msgdef.
* Removed all remaining RETURN_ZVAL() macros.
* Removed all explicit reference add/del operations.
* Added REFCOUNTING.md to Makefile.am.
* Port for php8
* Port php c extension for php8
* Update composer.json
* Drop php7.0 support
* Update phpunit for php7.1 in c extension test
* Add back support for php7.0
* Add badge for php8 continuous build
* Only ported c extension to php8.
* Didn't fixed the issue of throwing warnings for missing arginfo in bundled files.
* Tests not fixed, because syntax of phpunit (<7 vs >9.3) are not compatible.
* In next release, needs to drop php5 and php7.0 support (in order to use phpunit > 7)
2020-08-11 19:30:46 -07:00
Renamed from php/tests/encode_decode_test.php (Browse further)