简体   繁体   English

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

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

I want th serialize int/int64/double/float/uint32/uint64 into protobuf, which one should I use?我想将 int/int64/double/float/uint32/uint64 序列化为 protobuf,我应该使用哪一个? which one is more effective?哪个更有效?

For example:例如:

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;
        ...
   };
};

In your case, you'd better use oneof .在您的情况下,您最好使用oneof

You can not pack from or unpack to a built-in type, eg double, int32, int64, to google.protobuf.Any .您不能从内置类型打包或解包到google.protobuf.Any ,例如 double、int32、int64。 Instead, you can only pack from or unpack to a message, ie a class derived from google::protobuf::Message .相反,您只能打包或解包到消息,即从google::protobuf::Message派生的 class 。

google.protobuf.Value , in fact, is a wrapper on oneof : 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;
  }
}

Also from the definition of google.protobuf.Value , you can see, that there's no int32, int64, or unint64 fields, but only a double field.同样从google.protobuf.Value的定义中,您可以看到,没有 int32、int64 或 unint64 字段,而只有一个double字段。 IMHO (correct me, if I'm wrong), you might lose precision if the the integer is very large.恕我直言(纠正我,如果我错了),如果 integer 非常大,您可能会失去精度。 Normally, google.protobuf.Value is used with google.protobuf.Struct .通常, google.protobuf.Valuegoogle.protobuf.Struct一起使用。 Check google/protobuf/struct.proto for detail.检查google/protobuf/struct.proto了解详细信息。

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

相关问题 在python文件中使用google.protobuf.Any - Using google.protobuf.Any in python file 如何对重复的 google.protobuf.any 进行编码? - How to encode a repeated google.protobuf.any? Protobuf字段-弃用参数为google.protobuf.Any而不是保留的参数 - Protobuf Fields - Deprecating a parameter as google.protobuf.Any instead of reserved google.protobuf.Any 带有 grpc 的字段始终为零 - google.protobuf.Any field with grpc is always nil 如何在 gRPC nodejs 客户端中解压 google.protobuf.Any 类型? - How to unpack an google.protobuf.Any type in gRPC nodejs client? 如何在 Go 中使用原始类型的 `google.protobuf.Any`? - How to use `google.protobuf.Any` in Go with primitive types? 如何在 typescript 中使用 google.protobuf.Any 类型? - How to use google.protobuf.Any type in typescript? Marshal/Unmarshal google.protobuf.Any proto 消息 - Marshal/Unmarshal google.protobuf.Any proto message 在结构体中具有 go interface{} 字段的 protobuf 文件中使用 google.protobuf.Value,反之亦然 - Use google.protobuf.Value in a protobuf file with go interface{} field in a struct and vice versa protobufjs 编码一个包装消息,其中包含一个类型为 google.protobuf.Any 的内部字段 - protobufjs encode a wrapper message which includes an inner field with type google.protobuf.Any
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM