protocol-buffersprotobuf-c

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

Solution

  • In your case, you'd better use oneof.

    You can not pack from or unpack to a built-in type, e.g. double, int32, int64, to google.protobuf.Any. Instead, you can only pack from or unpack to a message, i.e. 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.