You specify how you want the information you're serializing to be structured by defining protocol buffer message types in .proto files. Each protocol buffer message is a small logical record of information, containing a series of name-value pairs. The detailed language guide can be found
As a walk-through, we create an Echo message which has three fields.
content: type is strnig and multiplicity is [1..1]
value: type is int32 and multiplicity is [0..1]
value_array: type is int32 and multiplicity is [0..*]
so with this in mind, you may come up with the following definition.
package sample;
message Echo {
required string content = 1;
optional int32 value = 2;
repeated int32 value_array = 3;
}
and one word on the union approach.
A key difference between GPB and XML is that GPB messages are not self describing. This means that the process receiving the message has to know what type it is before parsing. While there are cases where there is only one type of message being sent on a connection, the majority of applications send and receive multiple types of messages. So how do you make sure that the recipient application knows how to decode your message? One solution is to create a wrapper message that has one optional field for each possible message type. For example, if you have message types Foo, Bar, and Baz, you can combine them with a type like:
message OneMessage {
optional Foo foo = 1;
optional Bar bar = 2;
optional Echo echo = 3;
}
本文介绍了如何通过定义Protocol Buffers消息类型来指定序列化信息的结构。Protocol Buffers是一种高效的数据交换格式,通过.proto文件定义消息结构,包括字段名称、类型及可选性等。文章还探讨了如何处理不同类型的消息并确保接收方能正确解析。
496

被折叠的 条评论
为什么被折叠?



