Introduction
How does Google Protobuf work?
When should we use Protobuff?
Generic Serialization Method
/// <summary>
/// Generic action for serializing the object using google protocol buffer
/// </summary>
/// <typeparam name="TValue"></typeparam>
/// <param name="value"></param>
/// <returns></returns>
public string Serialize < TValue > (TValue value) {
try {
var result = JsonFormatter.Default.Format((IMessage) value); //Convertion process
return result;
} catch (Exception ex) {
//log here
throw ex;
}
}Generic DeSerialization Method
This method will receive a string type as input and deserialize it into a generic .proto type.
public TResult Deserialize < TResult > (string value) {
try {
System.Type type = typeof(TResult);
var typ = Assembly.GetExecutingAssembly().GetTypes().FirstOrDefault(t => t.Name == type.Name);
var descriptor = (MessageDescriptor) typ.GetProperty("Descriptor", BindingFlags.Public | BindingFlags.Static).GetValue(null, null);
var response = descriptor.Parser.ParseJson(value);
return (TResult) response;
} catch (Exception ex) {
throw ex;
}
}Reference
For .proto compiler and message structure and all other specifications please refer to the below official website.
Note
I've attached a sample application with this article. If you want more explanation or a live example please download the projects and refer to them.
I hope this will be helpful for you. Enjoy ;)


Sagar LadPosted Sep 24, 2021, 4:09 PM EDT
Informative article
Venkatasubbarao PolisettyTOP 100Posted Sep 24, 2021, 2:07 PM EDT
Informative and good to know this