Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the expected JSON serialization of 'oneof' protobuf field?

Tags:

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 ?

like image 826
phtrivier Avatar asked May 19 '17 12:05

phtrivier


Video Answer


1 Answers

I write some test code, it shows the first is the right one.

like image 65
vr3C Avatar answered Oct 12 '22 00:10

vr3C