简体   繁体   中英

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? 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 .

You can not pack from or unpack to a built-in type, eg double, int32, int64, to google.protobuf.Any . Instead, you can only pack from or unpack to a message, ie a class derived from google::protobuf::Message .

google.protobuf.Value , in fact, is a wrapper on 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. IMHO (correct me, if I'm wrong), you might lose precision if the the integer is very large. Normally, google.protobuf.Value is used with google.protobuf.Struct . Check google/protobuf/struct.proto for detail.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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