Search code examples
c#protobuf-net

C# create proto file from c# class


I'm trying to generate protobuf file from my c# class for some external collaboratosr. The issue is: i have many nullable value (int?, double?)

but when i doing this :

string protoOutput = Serializer.GetProto<Test>();

with class

[ProtoContract]
public class Test
{
    [ProtoMember(1)]
    public int? TEST { get; set; }

    [ProtoMember(2)]
    public DateTime SDF { get; set; }
}

the proto serializer output is:

 syntax = "proto3";
import "google/protobuf/timestamp.proto";

message Test {
   int32 TEST = 1;
   .google.protobuf.Timestamp SDF = 2;
}

how can i configure the serializer in order to have a :

google.protobuf.Int32Value

for my int? property

Thanks in advance


Solution

  • protobuf-net does not currently advertise data as Int32Value, and even if it did: that isn't how int? gets encoded, so: that would be bad.

    What we should perhaps do here, however, is make use of the (fairly recent) proto3 presence tracking feature (which didn't exist when that code was last touched). To explain this manually, this means: "add optional", i.e.

    syntax = "proto3";
    import "google/protobuf/timestamp.proto";
    
    message Test {
       optional int32 TEST = 1;
       .google.protobuf.Timestamp SDF = 2;
    }
    

    which provides a mechanism to detect "no value", while still being wire-compatible with the relevant encoding. For today, I would say: add optional by hand.