-
Notifications
You must be signed in to change notification settings - Fork 15.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[PHP] allow encoding enums as integers for json (#12707)
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=#12707 from brettmc:php-json-enum-as-int 515a083 PiperOrigin-RevId: 725806573
- Loading branch information
1 parent
0717c15
commit 40ec76e
Showing
12 changed files
with
227 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
// Protocol Buffers - Google's data interchange format | ||
// Copyright 2008 Google Inc. All rights reserved. | ||
// | ||
// Use of this source code is governed by a BSD-style | ||
// license that can be found in the LICENSE file or at | ||
// https://developers.google.com/open-source/licenses/bsd | ||
|
||
#include "print_options.h" | ||
|
||
#include "php.h" | ||
|
||
zend_class_entry* options_ce; | ||
|
||
void PrintOptions_ModuleInit() { | ||
zend_class_entry tmp_ce; | ||
|
||
INIT_CLASS_ENTRY(tmp_ce, "Google\\Protobuf\\PrintOptions", NULL); | ||
options_ce = zend_register_internal_class(&tmp_ce); | ||
|
||
// Define constants | ||
zend_declare_class_constant_long(options_ce, "PRESERVE_PROTO_FIELD_NAMES", | ||
sizeof("PRESERVE_PROTO_FIELD_NAMES") - 1, | ||
PRESERVE_PROTO_FIELD_NAMES); | ||
zend_declare_class_constant_long(options_ce, "ALWAYS_PRINT_ENUMS_AS_INTS", | ||
sizeof("ALWAYS_PRINT_ENUMS_AS_INTS") - 1, | ||
ALWAYS_PRINT_ENUMS_AS_INTS); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
// Protocol Buffers - Google's data interchange format | ||
// Copyright 2008 Google Inc. All rights reserved. | ||
// | ||
// Use of this source code is governed by a BSD-style | ||
// license that can be found in the LICENSE file or at | ||
// https://developers.google.com/open-source/licenses/bsd | ||
|
||
#ifndef PHP_PROTOBUF_PRINT_OPTIONS_H_ | ||
#define PHP_PROTOBUF_PRINT_OPTIONS_H_ | ||
|
||
#define PRESERVE_PROTO_FIELD_NAMES (1 << 0) | ||
#define ALWAYS_PRINT_ENUMS_AS_INTS (1 << 1) | ||
|
||
// Registers the PHP PrintOptions class. | ||
void PrintOptions_ModuleInit(); | ||
|
||
#endif // PHP_PROTOBUF_PRINT_OPTIONS_H_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?php | ||
|
||
// Protocol Buffers - Google's data interchange format | ||
// Copyright 2008 Google Inc. All rights reserved. | ||
// | ||
// Use of this source code is governed by a BSD-style | ||
// license that can be found in the LICENSE file or at | ||
// https://developers.google.com/open-source/licenses/bsd | ||
|
||
namespace Google\Protobuf; | ||
|
||
class PrintOptions | ||
{ | ||
const PRESERVE_PROTO_FIELD_NAMES = 1 << 0; | ||
const ALWAYS_PRINT_ENUMS_AS_INTS = 1 << 1; | ||
} |
Oops, something went wrong.