Mohammad Moghimi
Mohammad Moghimi

Reputation: 4686

Reading non-packed repeated field with packed=true proto file

I just realized that there is an option for a more efficient use of protocol buffers to read/write arrays of primitives. I already have protobuf files without packed=true. My question is what happens when I add this option to my .proto and compile it. Will I be able to read the previously saved protobufs (that are not saved with this option)?

repeated int32 samples = 4 [packed=true];

Upvotes: 2

Views: 2020

Answers (2)

Kenton Varda
Kenton Varda

Reputation: 45171

As of version 2.3.0 (Jan 2010), the Google-authored implementations of Protobufs (in C++, Java, and Python) can accept data in either format when parsing. The packed setting only tells the implementation which format to use when writing. See the changelog (first item under v2.3.0).

Versions 2.1 (when packed fields were introduced) and 2.2 do not have this property -- the parsers in these versions only accept [packed=true] fields in packed format, and non-[packed=true] fields in non-packed format.

Third-party implementations may or may not support this feature. You'll have to check their docs, or maybe write a test.

Upvotes: 4

Kerrek SB
Kerrek SB

Reputation: 477100

I don't have a 100% certain answer, and you should be able to verify this with a quick test, but if you look at the wire format specification, you'll find the wire format of packed fields. However, since the tag name and wire type is clearly identifiable, you should always be able to read a serialized protobuf, even if something crazy happens and you have multiple packed values and multiple unpacked values, all for the same field. They would just all be concatenated. The packed options should mainly dictate how a protobuf is written.

Do make a test case to be sure, though.

And of course your reader needs to support at least Proto 2.1.0.

Upvotes: 0

Related Questions