【问题标题】:How to add an int array in a protobuf message如何在 protobuf 消息中添加一个 int 数组
【发布时间】:2011-11-16 12:24:03
【问题描述】:

我必须编写一个 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

}

我的方法正确吗?

【问题讨论】:

    标签: java serialization protocol-buffers


    【解决方案1】:

    数组是通过“重复”映射的:

     repeated int32 data = 4;
    

    请注意,您可能需要 sint32/uint32。另请注意,在所有三种情况下都可以使用“打包数组”,这样效率更高;

    repeated int32 data = 4 [packed=true];
    

    【讨论】:

    • 压缩数组的概念是否也适用于双数组?
    • 我可以写可选的重复双数据 = 4 [packed = true];
    • @Raj 标签;数据的大小仅在运行时由那里的数据量决定; protobuf 中没有固定大小的数组。在“打包”的情况下,大小(以字节为单位)是数据的前缀。
    • 更新:developers.google.com/protocol-buffers/docs/proto3 声明“在 proto3 中,标量数字类型的重复字段默认使用打包编码。”
    • @Jason 非常正确(我写了一个不同的回复,然后意识到这个问题纯粹是关于 .proto 架构,而不是我在谈论的东西)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-03-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-23
    相关资源
    最近更新 更多