protobuf icon indicating copy to clipboard operation
protobuf copied to clipboard

Ruby JSON serialize enums as ints

Open martinxsliu opened this issue 5 years ago • 1 comments

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

martinxsliu avatar May 24 '19 23:05 martinxsliu

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

haberman avatar Sep 01 '22 20:09 haberman