protocol buffers - What is the expected JSON serialization of the "oneof" protobuf field?

Assuming I have a .proto with structure like this:

syntax = "proto3";

message Foo {
  ...
}
message Bar {
  ...
}

message Msg {
  string baz = 1;
  oneof some_union {
     Foo foo = 2;
     Bar bar = 3;
  }
}

What is the expected way to serialise a message of this kind ? The JSON Mapping section of the spec is not very clear.

I can see at least two ways to represent it, which is the right one ?

First way: have a single element at the "top level", ignore the others:

{
  "baz" : 0,
  "foo" : { ... }
}

Second way: have an "unmbrella" property with the name of the union, and give it a single field.

{
  "baz" : 0,
  "some_union": {
    "foo" : { .... }
  }
}

What should I expect ?

This question and all comments follow the "Attribution Required."

All Answers

Answers Loading...
Leave a Reply
You must be logged in to post a answer.
About the author