Unnecessary being that deserialization ismore than a switch on the type. The Union approach also puts all the fields on one object, as opposed to separating by the type field - this is what I wanted to confirm as unsupported by protobuf.
On Tue, May 10, 2016, 10:22 Tim Kientzle <[email protected]> wrote: > I’m not sure what you think is “unnecessary”. > > Are you trying to avoid actually declaring the types of a, b, c, d, etc? > > If so, you could use google.protobuf.Struct (which can parse a JSON object > in a generic way without requiring you to create a .proto at all) or any of > a number of other popular JSON parsing libraries. > > But if you want the type-safety that protobuf provides, then you have to > actually tell the protoc compiler the type of every field. In return, you > can skip a lot of checking in your code. > > Cheers, > > Tim > > > On May 10, 2016, at 7:08 AM, Marco <[email protected]> wrote: > > > > That does work, but it seems like it creates a lot of unnecessary work > if there are many fields. > > > > Is there a better way to handle a schema more like this? > > > > { type: "error", message: "wrong"} or { type:"ok", a: 1, b:2, c:3, d:4, > ... }. > > > > On Monday, May 9, 2016 at 3:12:19 PM UTC-4, Feng Xiao wrote: > > > > > > On Fri, May 6, 2016 at 8:18 PM, Marco <[email protected]> wrote: > > I'm looking to generate de/serialization for json tagged union messages > from an external rest api, eg. > > { type: "error", message: "wrong"} or { type:"ok", response: { a: 1, b: > 2}}. > > > > Reading the docs it seems like an any valued field the type field > modified json_name would work similarly, although this isn't supported. > > > > Is there an existing .proto message that would map to those messages? > Perhaps with some annotation to this more obvious schema, or being able to > specify a custom tag to determine which case of a oneof is in use (instead > of the default enum value)? > > How about something like: > > > > message Response { > > string type = 1; > > message Content { > > int32 a = 1; > > int32 b = 2; > > } > > oneof OneofResponse { > > string message = 2; > > Content response = 3; > > } > > } > > > > ? > > > > message Error { > > string message = 1; > > } > > > > message Second { > > int64 response = 1; > > } > > > > message MyUnion { > > oneof Either { > > Error err = 1; > > Ok ok = 2; > > } > > } > > > > > > > > > > -- > > You received this message because you are subscribed to the Google > Groups "Protocol Buffers" group. > > To unsubscribe from this group and stop receiving emails from it, send > an email to [email protected]. > > To post to this group, send email to [email protected]. > > Visit this group at https://groups.google.com/group/protobuf. > > For more options, visit https://groups.google.com/d/optout. > > > > > > -- > > You received this message because you are subscribed to the Google > Groups "Protocol Buffers" group. > > To unsubscribe from this group and stop receiving emails from it, send > an email to [email protected]. > > To post to this group, send email to [email protected]. > > Visit this group at https://groups.google.com/group/protobuf. > > For more options, visit https://groups.google.com/d/optout. > > -- You received this message because you are subscribed to the Google Groups "Protocol Buffers" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To post to this group, send email to [email protected]. Visit this group at https://groups.google.com/group/protobuf. For more options, visit https://groups.google.com/d/optout.
