Skip to content
This repository was archived by the owner on Feb 23, 2026. It is now read-only.

feat: allow enum strings in json serialization and deserialization#107

Merged
software-dov merged 7 commits intogoogleapis:masterfrom
software-dov:stringy
Aug 28, 2020
Merged

feat: allow enum strings in json serialization and deserialization#107
software-dov merged 7 commits intogoogleapis:masterfrom
software-dov:stringy

Conversation

@software-dov
Copy link
Copy Markdown
Contributor

For protobuf messages that contain enum fields, it is now possible to
specify that enum variants should be serialized as names and not as integers.

E.g.

json_str = MyMessage.to_json(my_message, enum_strings=True)

Similarly, serialization from json that uses this convention is now supported.

This is useful for interoperation with other data sources that do use
strings to define enum variants in json serialization; and for
debugging, where visually inspecting data structures can be helpful,
and variant names are more informative than numerical values.

For protobuf messages that contain enum fields, it is now possible to
specify that enum variants should be serialized as names and not as integers.

E.g.

json_str = MyMessage.to_json(my_message, enum_strings=True)

Similarly, serialization from json that uses this convention is now supported.

This is useful for interoperation with other data sources that do use
strings to define enum variants in json serialization; and for
debugging, where visually inspecting data structures can be helpful,
and variant names are more informative than numerical values.
@google-cla google-cla bot added the cla: yes This human has signed the Contributor License Agreement. label Aug 27, 2020
@software-dov
Copy link
Copy Markdown
Contributor Author

Please ignore the style check failure: Black got updated recently, and it screwed with various wrappings that were previously okay.

Once I have approval, I can either re-run black on the the entire code base, or I can punt that into a subsequent review.

Copy link
Copy Markdown
Contributor

@busunkim96 busunkim96 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To align with the proto3 JSON spec I would prefer defaulting to the stringy representation, but at this point that would be a breaking change.

Only other minor comment is that I'd prefer to use protobuf's name for this option (use_integers_for_enums) to help folks already used to that interface.

@software-dov
Copy link
Copy Markdown
Contributor Author

In general, I'm not inclined to recreate and pass through all the json serialization options. I agree, changing the behavior to be stringy by default would be a breaking change at this point.

@codecov
Copy link
Copy Markdown

codecov bot commented Aug 28, 2020

Codecov Report

Merging #107 into master will not change coverage.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff            @@
##            master      #107   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files           20        20           
  Lines          811       838   +27     
  Branches       136       139    +3     
=========================================
+ Hits           811       838   +27     
Impacted Files Coverage Δ
proto/marshal/marshal.py 100.00% <ø> (ø)
proto/marshal/rules/dates.py 100.00% <ø> (ø)
proto/marshal/rules/enums.py 100.00% <ø> (ø)
proto/_file_info.py 100.00% <100.00%> (ø)
proto/enums.py 100.00% <100.00%> (ø)
proto/fields.py 100.00% <100.00%> (ø)
proto/marshal/rules/struct.py 100.00% <100.00%> (ø)
proto/message.py 100.00% <100.00%> (ø)
proto/modules.py 100.00% <100.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 310dc18...7a04768. Read the comment docs.

@software-dov software-dov merged commit a082f85 into googleapis:master Aug 28, 2020
@software-dov software-dov deleted the stringy branch August 28, 2020 18:43
busunkim96 added a commit that referenced this pull request Aug 28, 2020
busunkim96 added a commit that referenced this pull request Aug 28, 2020
…ialization" (#110)

* Revert "feat: allow enum strings in json serialization and deserialization (#107)"

This reverts commit a082f85.
software-dov added a commit that referenced this pull request Sep 2, 2020
)

For protobuf messages that contain enum fields, it is now possible to
specify that enum variants should be serialized as names and not as integers.

E.g.

json_str = MyMessage.to_json(my_message, enum_strings=True)

Similarly, serialization from json that uses this convention is now supported.

This is useful for interoperation with other data sources that do use
strings to define enum variants in json serialization; and for
debugging, where visually inspecting data structures can be helpful,
and variant names are more informative than numerical values.

Re-addition of #107 with correct support for nested enum definitions
this time.

Includes new tests and modifications to existing tests that take into account
the fact that Enums now share many of the same properties as messages:
their definition adds an entry to the file descriptor proto, out-of-order field
references to enums require resolution, and out-of-file references require
proto-file dependency definition.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

cla: yes This human has signed the Contributor License Agreement.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants