如何在protobuf消息中添加int数组

jav*_*Man 31 java serialization protocol-buffers

我必须编写一个protobuf消息,它应该有1个整数变量和一个整数数组.

package protobuf;

message myProto {

optional uint32 message_id =1;
optional int update = 2;
//here I have to add a array of integers
//can I write like     optional int[] array =3;
//or should I use      optional repeated array;
//where array is another message with int variable

}
Run Code Online (Sandbox Code Playgroud)

我的方法是否正确?请帮我

谢谢

Mar*_*ell 40

数组通过"重复"映射:

 repeated int32 data = 4;
Run Code Online (Sandbox Code Playgroud)

请注意,您可能需要sint32/uint32.还要注意,在所有三种情况下都可以使用"打包阵列",这样更有效;

repeated int32 data = 4 [packed=true];
Run Code Online (Sandbox Code Playgroud)

  • 更新:https://developers.google.com/protocol-buffers/docs/proto3声明"在proto3中,标量数字类型的重复字段默认使用压缩编码." (3认同)
  • @Raj标签; 数据的大小仅在运行时由存在多少数据来确定;protobuf中没有固定大小的数组。在“打包”的情况下,大小(以字节为单位)以数据为前缀。 (2认同)