Generic Implementation For Serializer/Deserializer Using Google Protocol Buffer

Introduction
 

As we are aware, Protocol Buffers (Protobuf) is a free and open-source cross-platform library used to serialize structured data. In this article, we will explain the generic way of serializing and deserialize an object using Google protobuff. Since there are already so many guidelines for how to serialize and deserialize the object using protobuff. So we might have written a method to serialize and deserialize objects using google protobuf. In some sort of situation where there would multiple .proto formats and it needs to process with already written code. But serialization and deserialization are tightly coupled with .proto class type.
 
In case we need to serialize or deserialize for another .proto format, then obviously we have to write another method for that .proto format. It will become more duplicate code. To reduce the code redundancy, we have a generic way to process the multiple .proto type object. Let's see it in action.
 

How does Google Protobuf work?

 
The Protobuff is a binary transfer format, meaning the data is transmitted as a binary. This improves the speed of transmission more than the raw string because it takes less space and bandwidth. Since the data is compressed, the CPU usage will also be less.
 

When should we use Protobuff?

 
It is recommended you use Protobuff when you need fast serialization or deserialization. Type safety is important. Schema adherence is required. Protobuff performs up to 6 times faster than JSON.
 

Generic Serialization Method

 
In this method we could input any .proto type object and output as string type result.
/// <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.

Google ProtoBuf Reference

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 ;)


Similar Articles