Add proto3_json option for conformant JSON enum serialization#680
Open
smparkes wants to merge 1 commit intodanielgtaylor:masterfrom
Open
Add proto3_json option for conformant JSON enum serialization#680smparkes wants to merge 1 commit intodanielgtaylor:masterfrom
smparkes wants to merge 1 commit intodanielgtaylor:masterfrom
Conversation
The proto3 JSON spec requires enum values to use their original name from the .proto file (e.g. "SUIT_HEARTS"). betterproto stripped the enum type prefix for Python ergonomics (PR danielgtaylor#187), which also changed JSON output to use stripped names ("HEARTS") — breaking interop with other proto3 JSON implementations like protobuf-es. Add a proto3_json parameter to to_dict() that, when True, uses the original .proto enum names. The default (False) preserves existing behavior for backwards compatibility. Changes: - Add proto_name attribute to Enum members (original .proto name) - Add _proto_names_ class dict populated by codegen template - Add proto3_json parameter to to_dict() (default False) - Update from_string() to accept both Python and proto names - Update from_dict() to accept integer enum values - Update codegen template to emit _proto_names_ mapping - Update codegen model to carry proto_name through EnumEntry - Update README JSON section with proto3_json documentation - Add test_proto3_json.py with serialization, deserialization, round-trip, and backwards compatibility tests Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
[This is similar to #589 but that's pretty old. Tried to make this very straightfoward.]
The proto3 JSON spec requires enum values to use their original name from the .proto file (e.g. "SUIT_HEARTS"). betterproto stripped the enum type prefix for Python ergonomics (PR #187), which also changed JSON output to use stripped names ("HEARTS") — breaking interop with other proto3 JSON implementations like protobuf-es.
Add a proto3_json parameter to to_dict() that, when True, uses the original .proto enum names. The default (False) preserves existing behavior for backwards compatibility.
Changes:
Summary
Add support for strict proto3 JSON
Checklist