protobuf
protobuf copied to clipboard
Ruby JSON serialize enums as ints
What language does this apply to? Ruby proto3
Describe the problem you are trying to solve. The proto3 docs says that a JSON implementation may provide an option to:
Emit enum values as integers instead of strings: The name of an enum value is used by default in JSON output. An option may be provided to use the numeric value of the enum value instead.
Currently this isn't implemented by the Ruby library.
Describe the solution you'd like As far as I know of this feature is implemented by the C++ and Go libraries. I would like to request this so that Ruby has feature parity with the other supported languages.
Additional context I believe the entry point to Ruby's encode_json method is here: https://github.com/protocolbuffers/protobuf/blob/f425b9f059a57dc5f13a6e6a389fff1a6cc06749/ruby/ext/google/protobuf_c/encode_decode.c#L1477
This is supported at the upb level: https://github.com/protocolbuffers/upb/blob/ada907989e075658b72a6dcb3ec8335b43596cbc/upb/json_encode.h#L48-L49
It would be an easy change to plumb this through the Ruby layer, in addition to the existing options: https://github.com/protocolbuffers/protobuf/blob/7309e8882cb81a92cee4b0be708a61c8f4707224/ruby/ext/google/protobuf_c/message.c#L1146-L1155