Search code examples
protobuf-netprotocol-buffers

MessageParser.ParseFrom - GUID issue


I have generated proto file "Tutotial.Person" which contains a field called ID type GUID. I have a DTO called Person class in the C# project and I want to populate Tutorial.Person class from person object. Everything looks good but GUID. After parsing the Guid value looks like this "\t{}]2�p�D\u0011�O[X�]o". I am pretty new to this proto thing (as you may notice from my naive example below). Could you tell me what I would need to do to get right GUID?

      static void Main(string[] args) {
            var person = new Person {
                Name = "Fred",
                Id = Guid.NewGuid()
            };

            var parser = new MessageParser<Tutorial.Person>(() => new Tutorial.Person());
            using (var stream = new MemoryStream())
            {
                Serializer.Serialize(stream, person);

                var d = parser.ParseFrom(stream.ToArray());
            }  
        }

Proto file:

syntax = "proto3";
package tutorial;


message Person {
  string name = 1;
  string id = 2;
}


Solution

  • Protobuf doesn't have a well-known type for storing guids. Protobuf-net made up a layout, but it isn't a flat string. It is more complex than that, but the details aren't useful.

    Short version: if you want to round-trip between platforms / libraries: stick to types that have native protobuf representations. In this case, string would be a fair choice, rather than Guid.