I see. Another option is to send some kind of identifier preceding the message (just like the total size of the message), right?
-- Chris On Dec 3, 5:23 pm, Adam Vartanian <flo...@google.com> wrote: > > I just started looking into protobuf for a project of mine. > > From the Java Api page I could not really find how to parse a > > generated (compiled .proto is present) but "unknown" message. > > > So for example: I have messages types MessageA and MessageB. The > > client component receives some bytes representing a message of type A > > OR B. Do I have to add information of the type of message that's been > > send, or is there an easy way of automatically parsing the message > > like: > > > byte[] b; //hols byte representation of message > > Message message = foo(b); //parse message > > if(message instanceof MessageA) > > System.out.println("was type A"); > > else > > System.out.println("was type B"); > > No, there's no way to do this, because the wire format doesn't include > information about the type of message. It's even possible that the > same set of bytes could be a valid message of both types. > > The usual way to handle this is to create a wrapper message that can > hold either, like so: > > // Only one field may be filled out > message AorB { > optional MessageA message_a; > optional MessageB message_b; > > } > > And then you can parse it via: > > AorB result = AorB.parseFrom(data); > if (result.hasMessageA()) { > System.out.println("Was type A");} else if (result.hasMessageB()) { > > System.out.println("Was type B"); > > } > > - Adam -- You received this message because you are subscribed to the Google Groups "Protocol Buffers" group. To post to this group, send email to proto...@googlegroups.com. To unsubscribe from this group, send email to protobuf+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/protobuf?hl=en.