Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert C# object list to a protocol buffer list?

I am Trying to get a list of my userDTO objects and convert it into protobuf candidate list, but i am not able to figure out how to do it

C# object

public class UserDTO
  {
    public string UserNumber { get; set; }
    public string FirstName { get; set; }
    public string LastName  { get; set; }
    public string UserId { get; set; }
    public string EmailId { get; set; }
  }

ProtoBuf

 message Candidate {
     string userNumber = 1;
     string userId = 2;
     string firstName = 3;
     string lastName = 4;
     string emailId = 5;
    }

message CandidateList{
      repeated Candidate candidateList = 1;
}

List Function(This is incomplete)

 public CandidateList GetUsersRpc()
     {                  
    List<UserDTO> userList = _repository.GetUsersRpc();

       if (userList.Count > 0)
        {
        foreach (var user in userList)
            {

            }
         }
          return ;
      }   
   }
like image 255
Harsh Nagalla Avatar asked Jan 22 '19 07:01

Harsh Nagalla


People also ask

How do you convert Celsius to Fahrenheit easy?

To convert temperatures in degrees Celsius to Fahrenheit, multiply by 1.8 (or 9/5) and add 32.

What is the formula to C?

The relationship between Fahrenheit and Celsius is expressed with the formula, °C = (°F - 32) × 5/9; where C represents the value in Celsius and F represents the value in Fahrenheit.

How do you convert Celsius to normal values?

Celsius to Fahrenheit Conversion FormulaMultiply the °C temperature by 1.8. Add 32 to this number. This is the answer in °F.


1 Answers

It is possible that I'm misunderstanding the question here, so feel free to correct me on any parts that I've misunderstood.

If we use protobuf-net (I'm just citing that due to familiarity and convenience), then the following would work:

  [ProtoContract]
  public class UserDTO
  {
    [ProtoMember(1)]
    public string UserNumber { get; set; }
    [ProtoMember(3)]
    public string FirstName { get; set; }
    [ProtoMember(4)]
    public string LastName  { get; set; }
    [ProtoMember(2)]
    public string UserId { get; set; }
    [ProtoMember(5)]
    public string EmailId { get; set; }
  }

then assuming you already have your data in a Stream (if using byte[], a MemoryStream will work):

var userList = Serializer.Deserialize<List<UserDTO>>(source);

will give you a List<UserDTO>, making the (correct) assumption that each element is a repeated Candidate with field 1. If you want to me more specific, you can run the entire proto schema through code-gen to get the full schema - for example via protobuf-net's protogen. Hit "Generate" and you get an additional CandidateList element that represents the root object (rather than being implicit). Then you would use:

var root = Serializer.Deserialize<CandidateList>(source);
var userList = root.candidateLists;

In both cases, the same approach with Serialize instead of Deserialize will work to generate protobuf data from the input.

like image 159
Marc Gravell Avatar answered Sep 28 '22 15:09

Marc Gravell