Serialization and Deserialization in WCF

Introductions 

In this article we will discuss various types of serialization and deserialization  in wcf.

Serialization and Deserialization are the two most important techniques of data transmission from one end to another ( client to service and vice verse ).

Serialization : This is the process of converting a .NET object to a linear sequence of bytes for either storage or transmission from one end to another.

Deserialization : This is the process of converting a sequence of bytes to a .NET object. This is just the reverse process of serialization to recreate the .NET object from the bytes.

.NET framwork provides a namespace for this serialization and deserialization of objects, called "System.Runtime.Serialization" .

Behind The Scene 

Types of Serializations and Deserializations in WCF:
  •  DataContractSerializer
  •  XmlSerializer
  •  DataContractJsonSerializer
 Let's understand all the above techniques through a small example.

We will use the below data contract for all the three types of serialization and deserialization.

 
[DataContract]
    Public class Employee
    {
        [DataMember]
        Public string emp_Name;
 
        [DataMember]
        Public int emp_Id;
    }
 

Initialize the class with the below values.

 
Employee emp = new Employee { emp_Name = "Tapan kumar", 
                emp_Id = 001 };
 

 DataContractSerializer :

           This is the default serializer used by .NET framework. 

      Serialization : 

                 In order to use this, first create an object of the Employee class, then we will serialize the object to a memory stream using DataContractSerializer.

 
// create the employee class.
Employee objEmp = new Employee ();
FileStream fs = new FileStream("test.xml", FileMode.Open);

DataContractSerializer ser = new DataContractSerializer( typeof(Employee));

// write the object data to the file stream
XmlDictionaryWriter writer =
    XmlDictionaryWriter.CreateTextWriter(fs ,Encoding.UTF8 );

ser.WriteObject(writer, objEmp);
 

This serialization produces a "test.xml" file similar to;

 
<Employee>
  <emp_Name>Tapan kumar</emp_Name>
  <emp_Id>001</emp_Id>
</Employee>
 

       Deserialization :

                   Here the same will be done but in a reverse manner.

 
// create the employee class.
Employee objEmp = new Employee ();
FileStream fs = new FileStream("test,xml", FileMode.Open);

DataContractSerializer dcs = new DataContractSerializer(typeof(Employee));

// Read the object data from the file stream
XmlDictionaryReader reader =
      XmlDictionaryReader.CreateText(fs, new XmlDictionaryReaderQuotas());

objEmp = (Employee)dcs.ReadObject(reader);
 

    The ReadObject( ) method is used to read from the stream file.

XmlSerializer :

         This one is a prety old method, that is used for serialization and deserialization. Windows Communication Foundation supports it for backwards compatibility.

      Serialization : 

          In order to use this, first create an object of the Employee class, then we will serialize the object to a memory stream using XmlSerializer .      

 
// create the employee class.
Employee objEmp = new Employee ();
FileStream fs = new FileStream("test.xml", FileMode.Open);

XmlSerializer ser = new XmlSerializer( typeof(Employee));

// write the object data to the file stream
XmlDictionaryWriter writer =
    XmlDictionaryWriter.CreateTextWriter(fs ,Encoding.UTF8 );

ser.Serialize(writer, objEmp);
 

This serialization produces a "test.xml" file similar to;

 
<Employee xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
      xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <emp_Name>Tapan kumar</emp_Name>
  <emp_Id>001</emp_Id>
</Employee>
 

       Deserialization :

                   Here the same will be done but in a reverse manner.

 
// create the employee class.
Employee objEmp = new Employee ();
FileStream fs = new FileStream("test.xml", FileMode.Open);

XmlSerializer ser = new XmlSerializer( typeof(Employee));

// Read the object data from the file stream
XmlDictionaryReader reader =
      XmlDictionaryReader.CreateText(ms, new XmlDictionaryReaderQuotas());

objEmp = (Employee)ser.Deserialize(reader);
 


DataContractJsonSerializer :

         This one is the most advance serialization method, and used in a large scale due to its increasing popularity of data representation and for providing quicker and better packaging of data.

      Serialization : 

          In order to use this, first create an object of the Employee class, then we will serialize the object to a memory stream using DataContractJsonSerialization .      

 
// create the employee class.
Employee objEmp = new Employee ();
MemoryStream ms = new MemoryStream();

DataContractJsonSerializer ser = new 
             DataContractJsonSerializer(typeof(Employee));

// write the object data to the memory stream
 
ser.WriteObject(ms, objEmp);
 

This serialization produces a JSON file similar to;

 
JSON serialized Person object: {"emp_Name":"Tapan kumar","emp_Id":"001"}.
 

       Deserialization :

                   Here the same will be done but in a reverse manner.

 
// create the employee class.
Employee objEmp = new Employee ();
MemoryStream ms = new MemoryStream();

DataContractJsonSerializer ser = new 
                 DataContractJsonSerializer(typeof(Employee));

// first write 
 
ser.WriteObject(ms, objEmp);

// Read the object data from the memory stream

objEmp = (Employee)ReadObject(ms); 


Hope, this article gives you some idea about Serialization and Deserialization in WCF.


Happy Coding...

Previous Next

No comments:

Post a Comment