Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding string arrays in protocol buffers

I want to add string array in protocol buffer message, which I am not able to do. I have written as below

repeated string data = 1[packed=true];

I got the below error:

[packed = true] can only be specified for repeated primitive fields.

I could able to do it for int arrays with same syntax. I am confused why the string is considered as non-primitive type. Can anyone help me please.? Thanks !!

like image 264
Srikanth Avatar asked Jul 08 '13 15:07

Srikanth


People also ask

How does Protobuf serialize string?

The Protobuf serialization mechanism is given through the protoc application, this compiler will parse the . proto file and will generate as output, source files according to the configured language by its arguments, in this case, C++. You can also obtain more information about, reading the section compiler invocation.

Does Protobuf handle endianness?

Protocol buffers messages always use little-endian encoding. Implementations running on big-endian architectures should be doing the conversions automatically. If you are receiving data in wrong order, I would suggest using protoc --decode_raw to see whether the error occurs on the transmission or reception side.

Are proto2 and proto3 compatible?

Using proto2 Message TypesIt's possible to import proto2 message types and use them in your proto3 messages, and vice versa. However, proto2 enums cannot be used directly in proto3 syntax (it's okay if an imported proto2 message uses them).

What is the difference between proto2 and proto3?

Proto3 is the latest version of Protocol Buffers and includes the following changes from proto2: Field presence, also known as hasField , is removed by default for primitive fields. An unset primitive field has a language-defined default value.


1 Answers

Refer to the section "Specifying Field Rules" of the the Protobuf Documentation. Essentially, packing only makes sense for numeric fields.

Preserving a quote for prosperity:

For historical reasons, repeated fields of basic numeric types aren't encoded as efficiently as they could be. New code should use the special option [packed=true] to get a more efficient encoding.

like image 53
Chris Pitman Avatar answered Oct 04 '22 20:10

Chris Pitman