Tuesday, May 5, 2015

protobuf-net

 

Protocol Buffers is the name of the binary serialization format used by Google for much of their data communications. It is designed to be: small in size - efficient data storage (far smaller than xml) cheap to process - both at the client and server platform independent - portable between different programming architectures extensible - to add new data to old messages. protobuf-net is a .NET implementation of this, allowing you to serialize your .NET objects efficiently and easily. It is compatible with most of the .NET family, including .NET 2.0/3.0/3.5/4.0, .NET CF 2.0/3.5, Mono 2.x, Silverlight, etc.

See the following samples where you can use it.

Step 1: Install protobuff

To install protobuf-net, run the following command in the Package Manager Console

PM> Install-Package protobuf-net

Step 2: Create the Data Models

Unlike XmlSerializer, the member-names are not encoded in the data - instead, you must pick an integer to identify each member. Additionally, to show intent it is necessary to show that we intend this type to be serialized. See the following sample.

  1. [ProtoContract]
  2.   class Person {
  3.       [ProtoMember(1)]
  4.       public int Id {get;set;}
  5.       [ProtoMember(2)]
  6.       public string Name { get; set; }
  7.       [ProtoMember(3)]
  8.       public Address Address {get;set;}
  9.   }
  10.   
  11.   [ProtoContract]
  12.   class Address {
  13.       [ProtoMember(1)]
  14.       public string Line1 {get;set;}
  15.       [ProtoMember(2)]
  16.       public string Line2 {get;set;}
  17.   }

Notes for Identifiers

  • they must be positive integers
  • they must be unique within a single type
    • but the same numbers can be re-used in sub-types if inheritance is enabled
  • the identifiers must not conflict with any inheritance identifiers lower numbers take less space - don't start 100,000,000
  • the identifier is important; you can change the member-name, or shift it between a property and a field, but changing the identifier changes the data

 

Step 3: Serialize to stream

Class with protobuf functions
  1. public class ProtoBuf
  2.   {
  3.  
  4.      public static MemoryStream Serialize(object content)
  5.      {
  6.          MemoryStream objectStream=new MemoryStream();
  7.          Serializer.Serialize(objectStream,content);
  8.          return objectStream;
  9.      }
  10.  
  11.  
  12.      public static T Deserialize<T>(MemoryStream objectStream)
  13.      {
  14.          objectStream.Position = 0;
  15.          T content = Serializer.Deserialize<T>(objectStream);
  16.          return content;
  17.      }
  18.   }

 

Calling the methods
  1. var person = new Person
  2.           {
  3.               Id = 12345,
  4.               Name = "Fred",
  5.               Address = new Address
  6.               {
  7.                   Line1 = "Flat 1",
  8.                   Line2 = "The Meadows"
  9.               }
  10.           };
  11.  
  12.  
  13.           //serialize to stream
  14.           MemoryStream stream=  ProtoBuf.Serialize(person);
  15.  
  16.  
  17.           //deserilize to person object
  18.           stream.Position = 0;
  19.           Person personObject = ProtoBuf.Deserialize<Person>(stream);

 

Step 3: Serialize and write to a file.

  1. var person = new Person
  2.             {
  3.                 Id = 12345,
  4.                 Name = "Fred",
  5.                 Address = new Address
  6.                 {
  7.                     Line1 = "Flat 1",
  8.                     Line2 = "The Meadows"
  9.                 }
  10.             };
  11.  
  12.  
  13.             using (var file = File.Create("person.bin"))
  14.             {
  15.                 Serializer.Serialize<Person>(file, person);
  16.             }
  17.             
  18.     
  19.             using (var file = File.OpenRead("person.bin"))
  20.             {
  21.                 Person personObject = ProtoBuf.Deserialize<Person>(file);
  22.            
  23.             }

 

for more information in protobuf visit here

Happy Coding !!!