ruby-protobuf / protobuf

A pure ruby implementation of Google's Protocol Buffers
https://github.com/ruby-protobuf
MIT License
462 stars 101 forks source link

Emit enum values as integers instead of strings for JSON serialization #396

Closed martinxsliu closed 1 year ago

martinxsliu commented 5 years ago

Hi, is there a way to configure the JSON serializer to emit enum values as integers instead of strings (c.f. https://developers.google.com/protocol-buffers/docs/proto3#json_options). For example, the Go library has the EnumAsInts flag. I've tried splunking through the code but I haven't had any luck.

Without it, consumers of JSON serialized data are not forwards compatible as they cannot deserialize unknown string values.

devxpy commented 4 years ago

Its weird to me that this isn't the default.

m430 commented 1 year ago

+1

film42 commented 1 year ago

I think this might an issue with the google-protobuf gem and not this gem.

irb(main):008:0> b = ::Protobuf::Rpc::DynamicDiscovery::Beacon.new(:beacon_type => "FLATLINE")
=> #<Protobuf::Rpc::DynamicDiscovery::Beacon beacon_type=#<Protobuf::Enum(Protobuf::Rpc::DynamicDiscovery::BeaconType)::FLATLINE=1> s...

irb(main):009:0> b.to_json
=> "{\"beacon_type\":1}"

This issue should be opened here instead https://github.com/protocolbuffers/protobuf