繁体   English   中英

google.protobuf.Any 和 google.protobuf.Value 有什么区别?

[英]What the difference between google.protobuf.Any and google.protobuf.Value?

我想将 int/int64/double/float/uint32/uint64 序列化为 protobuf,我应该使用哪一个? 哪个更有效?

例如:

message Test {
    google.protobuf.Any   any   = 1;   // solution 1
    google.protobuf.Value value = 2;   // solution 2
};

message Test {                         // solution 3
   oneof Data {
        uint32   int_value    = 1;
        double   double_value = 2;
        bytes    string_value = 3;
        ...
   };
};

在您的情况下,您最好使用oneof

您不能从内置类型打包或解包到google.protobuf.Any ,例如 double、int32、int64。 相反,您只能打包或解包到消息,即从google::protobuf::Message派生的 class 。

google.protobuf.Value实际上是oneof的包装器:

message Value {
  // The kind of value.
  oneof kind {
    // Represents a null value.
    NullValue null_value = 1;
    // Represents a double value.
    double number_value = 2;
    // Represents a string value.
    string string_value = 3;
    // Represents a boolean value.
    bool bool_value = 4;
    // Represents a structured value.
    Struct struct_value = 5;
    // Represents a repeated `Value`.
    ListValue list_value = 6;
  }
}

同样从google.protobuf.Value的定义中,您可以看到,没有 int32、int64 或 unint64 字段,而只有一个double字段。 恕我直言(纠正我,如果我错了),如果 integer 非常大,您可能会失去精度。 通常, google.protobuf.Valuegoogle.protobuf.Struct一起使用。 检查google/protobuf/struct.proto了解详细信息。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM