Saturday, June 22, 2013

Binary Serialization in C#

Binary Serialization

Binary serialization uses binary encoding to produce compact serialization for uses such as storage or socket-based network streams. It is also known as deep serialization. Because it serialize every thing of an object.

Example

In this example we take an employee class. It has two attribute EmpId and EmpName.


using System;
using System.Runtime.Serialization;

namespace Example_Serialization
{
    [Serializable] 
    public class Employee : ISerializable
    {
        public int EmpId { get; set; }
        public string EmpName { get; set; }
        public void GetObjectData(SerializationInfo info, StreamingContext context)
        {
            info.AddValue("EmpId", EmpId);
            info.AddValue("EmpName", EmpName);           
        }
        public Employee(SerializationInfo info, StreamingContext context)
        {
            EmpId = (int)info.GetValue("EmpId", typeof(int));
            EmpName = (string)info.GetValue("EmpName", typeof(string));
        }

        public Employee()
        {
        }
    }
}  

In the above class  [Serializable] attribute should be applied. Serializable attribute tell compiler that all public and private properties should be serialized. If we don't want to serialize some properties then simply apply [NonSerialized] attribute. Some times we need more customization, in that case we implements ISerializable interface. It override serializations process. It has a method  named GetObjectData which actually serialize the object. And for desrialization we use a constructor where we write our deserialization code.

Now we can serialize and deserialize our employee class. Below code snippet will explain how to serialize our employee class using binary serialization.

using System;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;

namespace Example_Serialization
{   
    class BinarySerialization
    {      
        public void SerializeObj()
        {
            Console.WriteLine("Binary Serialization starting");
            Employee emp = new Employee() { EmpId = 1, EmpName = "Hriday Ranjan Dutta" };
            Stream stream = File.Open("Binaryemployee.txt", FileMode.OpenOrCreate);
            BinaryFormatter bformatter = new BinaryFormatter();
            bformatter.Serialize(stream, emp);
            stream.Close();
            Console.WriteLine("Binary Serialization done");
            Console.ReadLine();
        }
        public void DeserializeObj()
        {
            Console.WriteLine("Binary Deserialization starting");
            Stream stream = File.Open("Binaryemployee.txt", FileMode.Open);
            BinaryFormatter bformatter = new BinaryFormatter();
            Employee emp = (Employee)bformatter.Deserialize(stream);
            stream.Close();
            Console.WriteLine("Employee ID : {0}, Employee Name : {1}",emp.EmpId.ToString(),emp.EmpName);
            Console.WriteLine("Binary Deserialization done");
            Console.ReadLine();
        }       
    }
}


Output Serialized file

 ÿÿÿÿ           LExample_Serialization, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null    Example_Serialization.Employee    <EmpId>k__BackingField <EmpName>k__BackingField    Hriday Ranjan Dutta