Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option to print enums as ints in Ruby JSON encoder #6384

Closed

Conversation

spiffyy99
Copy link

Fixes #6178

Adds the option to print enums as int values instead of the default strings for Ruby (similar to the option present for C++).

Example:

test.proto

enum TestEnum {
  Default = 0;
  Foo = 1;
}
enum TestMessage {
  TestEnum test_enum = 1;
}

code.rb

mymessage = TestMessage.new(:test_enum => 1)
puts TestMessage.encode(mymessage, {always_show_enums_as_ints: true})

will output:

{"testEnum":1}

@TeBoring
Copy link
Contributor

Please send changes of upb to https://github.com/protocolbuffers/upb first.

@TeBoring
Copy link
Contributor

Any update?

@spiffyy99
Copy link
Author

@TeBoring yes, it seems like this repo (the ruby sections) have deprecated interfaces compared to the upb repo, mentioned here, doesn't seem like the changes here can be added to the upb repo (correct me if i'm wrong).

@TeBoring
Copy link
Contributor

There is some ongoing work to update upb in ruby: #5866

Please send change to upb first. Once you are done, we probably have updated the upb in ruby.

@TeBoring
Copy link
Contributor

We have update upb in ruby. Could you try again?

@acozzette
Copy link
Member

@spiffyyeng I'm going to close this since there has been no activity in a long time, but feel free to comment here if you would still like to pursue this change.

@acozzette acozzette closed this Oct 13, 2021
@danielmorrison
Copy link

I'd personally love to see it, but don't know the internals enough yet to attempt it myself.

@haberman
Copy link
Member

haberman commented Sep 1, 2022

This would be an easy change.

We already support two options to the JSON encoder:

if (RTEST(rb_hash_lookup2(hash_args,
ID2SYM(rb_intern("preserve_proto_fieldnames")),
Qfalse))) {
options |= upb_JsonEncode_UseProtoNames;
}
if (RTEST(rb_hash_lookup2(hash_args, ID2SYM(rb_intern("emit_defaults")),
Qfalse))) {
options |= upb_JsonEncode_EmitDefaults;
}

We would just need to add support for the upb_JsonEncode_FormatEnumsAsIntegers option: https://github.com/protocolbuffers/upb/blob/ada907989e075658b72a6dcb3ec8335b43596cbc/upb/json_encode.h#L48-L49

@MQuy
Copy link
Contributor

MQuy commented Jan 26, 2023

hi @haberman, I created a pull request based on your suggestion. Would love to have your feedback #11673

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Ruby JSON serialize enums as ints
8 participants