简体   繁体   English

有关Google协议缓冲区的查询

[英]Query regarding Google Protocol Buffer

message Person {
    required string name = 1;
    required int32 id = 2;
    optional string email = 3; 
}

The above is a snippet from addrbook.proto file mentioned in Google Protocol Buffer tutorials. 上面是Google协议缓冲区教程中提到的addrbook.proto文件的摘录。 The requirement is that, the application that is being developed will need to decode binary data received from a socket.For example,name, id and e-mail represented as binary data. 要求是,正在开发的应用程序将需要对从套接字接收的二进制数据进行解码。例如,以二进制数据表示的名称,ID和电子邮件。 Now, id can be read as an integer. 现在,id可以读取为整数。 But I am really not sure how to read name and email considering that these two can be of variable lengths. 但是我真的不确定如何读取姓名和电子邮件,因为这两个长度可能会有所不同。 (And unfortunately, I do not get lengths prefixed before these two fields) Application is expected to read these kind of data from a variety of sources. (不幸的是,我没有在这两个字段之前添加长度前缀)期望应用程序从各种来源读取此类数据。 Our objective is to make a decoder/adapter for the different types of data originating from these sources. 我们的目标是为源自这些来源的不同类型的数据制作一个解码器/适配器。 Then there could be different types of messages from same source too. 然后也可能有来自同一来源的不同类型的消息。 Thanks in advance 提前致谢

But I am really not sure how to read name and email considering that these two can be of variable lengths. 但是我真的不确定如何读取姓名和电子邮件,因为这两个长度可能会有所不同。

The entire point of a serializer such as protobuf is that you don't need to worry about that . 诸如protobuf之类的序列化程序的全部要点是, 您不必为此担心 Specifically, in the case of protobuf strings are always prefixed by their length in bytes (using UTF-8 encoding, and varint encoding of the length). 具体来说,对于protobuf字符串,始终以字节长度为前缀(使用UTF-8编码,以及长度的varint编码)。

And unfortunately, I do not get lengths prefixed before these two fields 不幸的是,我没有在这两个字段之前添加长度

Then you aren't processing protobuf data. 那样您就不会处理protobuf数据。 Protobuf is a specific data format, in the same way that xml or json is a data format. Protobuf是一种特定的数据格式,就像xml或json是一种数据格式一样。 Any conversation involving "protocol buffers" only makes sense if you are actually discussing the protobuf format, or data serialized using that format. 仅当您实际上在讨论协议格式或使用该格式序列化的数据时,任何涉及“协议缓冲区”的对话才有意义。

Protocol buffers is not an arbitrary data handling API. 协议缓冲区不是任意的数据处理API。 It will not allow you to process data in any format other than protobuf. 它不允许您处理除protobuf以外的任何格式的数据。

It sounds like you might be trying to re-implement Protocol Buffers by hand; 听起来您可能正在尝试手动重新实现协议缓冲区。 you don't need to do that (though I'm sure it would be fun). 您不需要这样做(尽管我确定这会很有趣)。 Google provides C, Java, and Python implementations to serialize and de-serialize content in protobuf format as part of the Protocol Buffers project. Google提供了C,Java和Python实现,可以将协议缓冲区项目中protobuf格式的内容序列化和反序列化。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM