[英]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.Value
與google.protobuf.Struct
一起使用。 檢查google/protobuf/struct.proto了解詳細信息。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.