Skip to content

Add proto3_json option for conformant JSON enum serialization#680

Open
smparkes wants to merge 1 commit intodanielgtaylor:masterfrom
smparkes:fix/proto3-json-enums
Open

Add proto3_json option for conformant JSON enum serialization#680
smparkes wants to merge 1 commit intodanielgtaylor:masterfrom
smparkes:fix/proto3-json-enums

Conversation

@smparkes
Copy link
Copy Markdown

[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:

  • 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

Summary

Add support for strict proto3 JSON

Checklist

  • If code changes were made then they have been tested.
    • I have updated the documentation to reflect the changes.
  • This PR fixes an issue.
  • This PR adds something new (e.g. new method or parameters).
    • This change has an associated test.
  • This PR is a breaking change (e.g. methods or parameters removed/renamed)
  • This PR is not a code change (e.g. documentation, README, ...)

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>
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.

1 participant