Protobuf 3.0中的任何和字节之间的区别是什么?



众所周知,我们可以使用serialize和Unerialize API在bytes和消息之间转换,同时我们可以使用packunpack API在any和消息之间转换。我的问题是:Protobuf 3.0中的anybytes有什么区别?例如商店尺寸,速度等。

我看到的唯一主要区别是,任何添加了一个额外的" @Type"字段,这是包装收到的消息的字符串URL名称。它添加的URL字段的示例:

@type = "type.googleapis.com/packagename.messagename"

这会在您的消息中添加一个不可忽略的字节。

i也被困在这个问题上。但是我在网上找不到任何答案 - 在网上尤其是对于C ,Proto3中的任何类型都没有得到很好的记录。因此,我尝试了它们,区别在于Anybytes序列化。当Any序列化任何任意消息时,bytes序列化任何字符串(在C 中)。

这是" any:

"的代码段
// Proto file containing message description for Foo
#include "foo_proto.grpc.pb.h" 
// Proto file containing message description for AnyMessage, 
// which uses google.protobuf.Any
#include "any_proto.grpc.pb.h"
Foo *foo = new Foo(); // Foo is the message defined in "foo_proto.proto"
// ... Set the variables for message Foo
// Pack Foo into 'Any' message type
Any* any = new Any();
any->PackFrom(*foo);
// Use the above 'Any' message to create AnyMessage object
AnyMessage am;
am.set_allocated_object(any);

但是,对于bytes,您需要打包字符串类型,而不是任何对象。因此,bytes的代码片段看起来像:

// Proto file containing message description for Foo
#include "foo_proto.grpc.pb.h" 
// Proto file containing message description for BytesMessage, 
// which uses the bytes type
#include "bytes_proto.grpc.pb.h"
Foo *foo = new Foo(); // Foo is the message defined in "foo_proto.proto"
// ... Set the variables for message Foo
std::string bytes_string; // Encode the object Foo in this string
// Now, create BytesMessage object
BytesMessage bm;
bm.set_object(bytes_string);

我希望这能解决问题中提出的查询。谢谢!

2.0和3.0之间没有协议差;从理论上讲,您的数据应该相同。

可能存在一些与默认和零在图书馆级别处理的方式有关的微小差异 - 在3.0"必需"one_answers"可选"中不存在:零不是传输的(零都可以有效地可选。零默认值)。这意味着,当您可能已经明确分配了零值时,它可能已被传输。现在不会。当然,这也意味着在3.0中根本不可能进行非零默认值。

重点:第二段中的所有内容都在序列化器级别,而不是协议级别。该协议完全没有变化。

最新更新