Introduction
Protobuf-net is a faster .NET library for serialization and deserialization based on Google's
Protocol Buffers. It is designed to be a language neutral, platform neutral, extensible way of serializing structured data for use in communications protocols and efficient data storage (far smaller than xml). You can define how you want your data to be structured once, then you can use special generated source code to easily write and read your structured data to and from a variety of data streams and using a variety of languages. It allows you to serialize your .NET objects efficiently and easily. It is compatible .NET 2.0/3.0/3.5/4.0, .NET CF 2.0/3.5, Mono 2.x, Silverlight, etc.
It contains a few attributes which have significance with serializing an object:
- [ProtoContract]
ProtoContract is a sealed class which inherits from Attribute class. It needs to be defined above a class and indicates that this class will serialize.
- [ProtoMember(N)]
ProtoMember is a sealed class which inherits from Attribute class. It needs to be defined above a property/field and indicates that this field will serialize. Here N represents the number in which order it will serialize. It will start numbering at 1 even if your class uses ProtoMember(N) explicitly on some fields because it serializes in alphabetical order.
- [ProtoIgnore]
ProtoContract is a sealed class which inherits from Attribute class. It needs to be defined above a property/field and indicates that this field will ignore (won’t serialize) when serialization occurs.
Protobuf-net is not compatible with the standard .NET serialization system which means,
- It ignores the [Serializable] attribute
- It does not support custom serialization using ISerializable
We will discuss following points,
- Installation of protobuf-net
- Serializing an object
- Deserialize byte array to an object
- Advantages of protobuf-net
Installation
To install protobuf-net, you need to use NuGet Package Manager which provides the latest version of the dll. Follow Figure 1 which shows how to install the dll.
Figure 1: Install through Nuget Package Manager
or
TOOLS -> Library Package Manager -> Package Manager Console
Install-Package protobuf-net
Note
If you are using VS 2012/2013 or lower, then you need to provide version in command.
Install-Package protobuf-net -Version 2.0.0.668
Before starting demonstration, we need to decorate the entity. Let's say Project is the entity which has two fields, ID and Name. ProtoContract attribute defines to make class serializable and Protomember defines to make members/properties as serializable.
- [ProtoContract(SkipConstructor = true)]
- public class Project
- {
- [ProtoMember(1)]
- public int ID { get; set; }
-
- [ProtoMember(2)]
- public string Name { get; set; }
- }
Serialization
Serialization is the process of converting an object into a stream of bytes to store the object or transmit it to memory, database or file. Its aim is to save the state of an object in order to be able to recreate it in the future when needed. The below code-snippet defines a method (ProtoSerialize()) which receives an object. Then it calls Serialize() to convert the object to a stream of bytes through MemoryStream object.
- public static byte[] ProtoSerialize<T>(T record) where T : class
- {
- if (null == record) return null;
-
- try
- {
- using (var stream = new MemoryStream())
- {
- Serializer.Serialize(stream, record);
- return stream.ToArray();
- }
- }
- catch
- {
- // Log error
- throw;
- }
- }
The following figure shows the result after Serialization has happened. It converts the Project object to bytes array through ProtoSerialize() method.
Figure 2: Result after Serialization
Deserialization
Deserialization is the process of converting streams of bytes into an object. It is the reverse process of Serialization (discussed above). The below code-snippet defines Deserialize method(ProtoDeserialize()) with byte array as parameter. Then it converts byte array to MemoryStream object. Next it calls Deserialize() with passing MemoryStream object as parameter and it returns an actual object.
- public static T ProtoDeserialize<T>(byte[] data) where T : class
- {
- if (null == data) return null;
-
- try
- {
- using (var stream = new MemoryStream(data))
- {
- return Serializer.Deserialize<T>(stream);
- }
- }
- catch
- {
- // Log error
- throw;
- }
- }
The following figure shows the result after Deserialization has happened. It converts the bytes array to Project object through ProtoDeserialize() method.
Figure 3: Result after Deserialization
Advantages
- Very fast processing
- Hard to decode without knowing schema
- Very useful for internal APIs
- Easy Language Interoperability
- Generic request/response allows more flexibility
Performance comparison
Figure 4: Serialization Performance graph of libraries
Conclusion
In this article, we discussed about serialization and deserialization using protobuf-net dll. We can perform the same functionality in different ways like DataContractSerializer, JSON.NET, JavaScriptSerializer etc. However, protobuf-net provides better performance as compared to other methods. So use protobuf-net dll whenever you develop internal APIs and perform serialization/deserialization.