feat: allow enum strings in json serialization and deserialization#107
feat: allow enum strings in json serialization and deserialization#107software-dov merged 7 commits intogoogleapis:masterfrom software-dov:stringy
Conversation
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.
|
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. |
busunkim96
left a comment
There was a problem hiding this comment.
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.
|
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 Report
@@ Coverage Diff @@
## master #107 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 20 20
Lines 811 838 +27
Branches 136 139 +3
=========================================
+ Hits 811 838 +27
Continue to review full report at Codecov.
|
) 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.
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.