Unexpected 0-th value enum JSON serialization
See original GitHub issueThe testing message is defined as follows:
syntax = "proto2";
message MessageWithEnum {
enum EnumType {
FOO = 0;
BAR = 1;
}
optional EnumType value = 1;
}
We observe the following serialization with pbandk:
Source message | current JSON serialization | expected JSON serialization |
---|---|---|
MessageWithEnum() |
{"value":null} |
{} |
MessageWithEnum(value=FOO) |
{} |
{"value":"FOO"} |
MessageWithEnum(value=BAR) |
{"value":"BAR"} |
OK! |
The bug is reproduced in a unit test here: https://github.com/streem/pbandk/pull/234
I know that proto2 doesn’t really have a JSON spec defined, but other protobuf implementations (java, python, swift) would all behave as expected in this example.
In proto3, we observed the following serialization behavior:
Source message | current JSON serialization | expected JSON serialization |
---|---|---|
TestAllTypesProto3() |
{} |
OK! |
TestAllTypesProto3(optionalNestedEnum = FOO) |
{} |
{"value":"FOO"} |
TestAllTypesProto3(optionalNestedEnum = BAR) |
{"value":"BAR"} |
OK! |
- Do you agree that the current behavior is unexpected?
- Would you be open for us contributing a fix for it?
Thank you for your time 😃
Issue Analytics
- State:
- Created a year ago
- Comments:9 (9 by maintainers)
Top Results From Across the Web
c# - How can I ignore unknown enum values during json ...
You can solve this problem with a custom JsonConverter . Here is one I put together using a few pieces from the StringEnumConverter...
Read more >Enum with a value of zero deserializes into default value
First (which has a value of 0) and then serialize and deserialize, I get ModeEnum.Third out of it, as obviously it considers a...
Read more >How to correctly serialize/deserialize an enum with custom ...
I'm hoping someone can give me some insight into what I'm doing wrong here. I have an enum type, and I want to...
Read more >JSON Schema Serializer and Deserializer
json.schema.spec.version Indicates the specification version to use for JSON schemas derived from objects. Valid values are one of the following strings: ...
Read more >Enumeration class and JSON Serialization - Hi, I'm Ankit
In this post, I will explain how we can serialize an Enumeration class using a custom JSON Converter for both System.Text.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@antongrbin It’s released now!
@antongrbin Thank you for the thorough fix and tests!