Deserting data into protobuf class

I am trying to deserialize data from Mongodb to MyClass that protoc created (version 3).

I am using MongoDB.Driver (version 4) BsonSerializer.Deserialize (result, MyClass.GetType ()); What uses the XmlSerializer to deserialize data.

The problem is how protobuf presents its collections. The list is created as Google.Protobuf.Collections.RepeatedField with no setter . XmlSerializer cannot deserialize fields without settings. How can I solve this problem so that it does not become an ugly hack?

Parameters I came up with:

  • Create your own Serializer that MongoDB can use to handle properties without customization.
  • Create your own class generator that adds a public setter.
  • Create a new class as a mapping between the serialized MongoDB data and the protobufs class.

Option 1 seems intimidating; option 2 makes me create a new plug, which is a pain to maintain. Option 3 is the easiest to date, but also messy, I would prefer not to create another level of difficulty if there are better ways to handle this.

So my question is:

Are there any other ways to solve this problem? Is there something I don't have that is already built in, or maybe I'm missing something trivial?

Edit: This is a snippet of what protoc verison 3 generates:

/// <summary>Field number for the "Recipients" field.</summary>
public const int RecipientsFieldNumber = 5;
private static readonly pb::FieldCodec<string> _repeated_recipients_codec = 
    pb::FieldCodec.ForString(42);
private readonly pbc::RepeatedField<string> recipients_ = new   pbc::RepeatedField<string>();
public pbc::RepeatedField<string> Recipients {
  get { return recipients_; }
}

It comes from this proto file:

syntax = "proto3";
package DataModels.Data;

message MailTemplateMessage {
    string UUID = 1;
    string SubjectLine = 2; 
    string Body = 3;
    string Sender = 4;
    repeated string Recipients = 5;
}
+4
source share

All Articles