[英]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.Value
与google.protobuf.Struct
一起使用。 Check google/protobuf/struct.proto for detail.检查google/protobuf/struct.proto了解详细信息。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.