Friday, 1 August 2014

Object Serialization

Why would you want to use serialization?
The two most important reasons are to persist the state of an object to a storage medium so an exact copy can be recreated at a later stage, and to send the object by value from one application domain to another. For example, serialization is used to save session state in ASP.NET and also it is used for gaming applications.

What is serialization?
Serialization is the process of converting an object into a stream of bytes in order to store the object or transmit it to memory, a database, or a file. Its main purpose is to save the state of an object in order to be able to recreate it when needed. The reverse process is called deserialization.

Serialization explained
The namespace which is used to read and write files is System.IO. For Serialization we are going to look at the System.Runtime.Serialization namespace. The ISerializable interface allows you to make any class Serializable.

Here are the following steps that we are going to do to create a serializable class and test it.
  • Create a custom class named Employee and assign properties.
  • Define the serialization functions.
  • Create a main class and instantiate our Employee class.
  • Serialize the object to a sample file.
  • DE serialize the values by reading it from the file.
Defining Employee class and properties
Our custom class Employee should be derived from the ISerializable interface and should hold the Serializable attribute. Here is the code snippet.

using System;

using System.IO;

using System.Runtime.Serialization;

using System.Runtime.Serialization.Formatters.Binary;



namespace MyObjSerial
{
    [Serializable()]    //Set this attribute to all the classes that want to serialize
    public class Employee : ISerializable //derive your class from ISerializable
    {
        public int EmpId;
        public string EmpName;
       
        //Default constructor
        public Employee()
        {
            EmpId = 0;
            EmpName = null;
        }
        }
}

Define Serialization functions
Now we need two functions: One to say how to serialize Employee objects and another to say how to deserialize them. For serialization we override the GetObjectData() function provided by the ISerializable interface. For deserialization we provide a special constructor with the serialization parameters as arguments. This constructor will be called when we deserialize our file to object (which will be shown later).

One of the important parameters is the SerializationInfo object. This object holds a name-value pair for the properties to be serialized. You can decide which property should be serialized and which not in the GetObjectData() function. All the properties that are added to this SerializationInfo parameter will be serialized. Here are the codes for the two functions. Add them to our Employee class.

//Deserialization constructor.
public Employee(SerializationInfo info, StreamingContext ctxt)

{

    //Get the values from info and assign them to the appropriate properties

    EmpId = (int)info.GetValue("EmployeeId", typeof(int));

    EmpName = (String)info.GetValue("EmployeeName", typeof(string));
}
        
//Serialization function.
public void GetObjectData(SerializationInfo info, StreamingContext ctxt)
{
    //You can use any custom name for your name-value pair. But make sure you
    // read the values with the same name. For ex:- If you write EmpId as "EmployeeId"
    // then you should read the same with "EmployeeId"
    info.AddValue("EmployeeId", EmpId);
    info.AddValue("EmployeeName", EmpName);
}


That’s it. You have created your own class which is now serializable. Now let’s see how to write an instance of Employee to a special file with a custom .osl extension. And we also see how to read back Employee object from the file.

Create a main class and instantiate our Employee class
Following is the code snippet for ObjSerial class which holds our application's main entry point.

//Main class

public class ObjSerial

{

    public static void Main(String[] args)

    {

        //Create a new Employee object
        Employee mp = new Employee();
        mp.EmpId = 10;
        mp.EmpName = "Omkumar";
                
        //Add code below for serialization
    }
}


Serialize the object to a sample file
For serializing, let’s open a stream object and give a sample file name EmployeeInfo.osl. Note, the demo exe file has this same name. So when you run ObjSerial.exe, the EmployeeInfo.osl file will be created under the folder where you copied the exe file. Add the following code to our ObjSerial class. Once a stream is open we create a BinaryFormatter and use the Serialize method to serialize our object to the stream. What Serialize method would do? It converts our object into binary format and streams it in.

// Open a file and serialize the object into it in binary format.
// EmployeeInfo.osl is the file that we are creating. 
// Note:- you can give any extension you want for your file
// If you use custom extensions, then the user will now 
// that the file is associated with your program.
Stream stream = File.Open("EmployeeInfo.osl", FileMode.Create);
BinaryFormatter bformatter = new BinaryFormatter();

Console.WriteLine("Writing Employee Information");

bformatter.Serialize(stream, mp);
stream.Close();

Deserialize the values by reading it from the file
Now we read the created file and cast the return value to our Employee class for further usage. For reading we again create a BinaryFormatter to read the object in binary form. We then use the Deserialize method which converts the stream of bytes to an Object object. This object can then be easily casted to our Employee class.

//Clear mp for further usage.
mp = null;

//Open the file written above and read values from it.
stream = File.Open("EmployeeInfo.osl", FileMode.Open);
bformatter = new BinaryFormatter();

Console.WriteLine("Reading Employee Information");

mp = (Employee)bformatter.Deserialize(stream);
stream.Close(); 
Console.WriteLine("Employee Id: {0}",mp.EmpId.ToString());
Console.WriteLine("Employee Name: {0}",mp.EmpName);

About Author:
Steven Pinto is technology geek and loves to write on technology. He works in Systems Plus Pvt. Ltd. and actively contributes to technology. To more of interesting topics written by Steven, follow http://mad4teck.blogspot.in/

1 comment: