5

As we know, We can use serialize and unserialize API to convert between bytes and message, at the same time, we can use pack and unpack API to convert between any and message. My question is: what is the difference between any and bytes in protobuf 3.0? Such as store size, speed and so on.

Lobo
  • 81
  • 6

3 Answers3

1

I was also stuck up on this issue. But I could not find any answer to this on the net - the Any Type in Proto3 is not well documented online, especially for C++. So, I tried out both of them and the difference lies in what Any and bytes serialize. While Any serializes any arbitrary Message, bytes serializes any string (in C++).

Here is a code snippet for `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);

However, for bytes you need to pack a string type, instead of the Any object. So, code snippet for bytes may look like:

// 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);

I hope this resolves the query raised in the question. Thanks!

diviquery
  • 569
  • 5
  • 19
1

The only major difference I can see is that Any adds an extra "@type" field, which is a string URL name of the message that it was packed from. Example of the URL field it adds:

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

This adds a non-negligible amount of bytes to your message.

pcdangio
  • 294
  • 2
  • 13
-1

There is no protocol difference between 2.0 and 3.0; in theory your data should be identical.

There may be some small differences relating to how defaults and zeros are handled at the library level - in 3.0 "required" and "optional" don't exist, instead: zeros aren't transmitted (everything is effectively optional with a zero default). This means that previously when you might have explicitly assigned a value of zero, it might have been transmitted. Now it will not be. Of course, this also means that non-zero defaults are simply not possible in 3.0.

Emphasis: everything in the second paragraph is at the serializer level, not the protocol level. The protocol is entirely unchanged.

Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
  • 2
    Thank you for your replying, I want to know the difference between any and bytes in protobuf 3.0. – Lobo Sep 15 '17 at 02:53