Tuesday, 7 October 2014

Serialization in .Net Framework

Serialization is a process of converting an object to text stream so that it can be is easily transmittable over the network or can be continued in a persistent storage location. This storage location can be a physical file, database 
or ASP.NET Cache.

Serialization Advantages

1. Facilitate the transportation of an object through a network
2. Create a clone of an object

Serialization Disadvantages

1. The primary disadvantage of serialization can be attributed to the resource overhead (both the CPU and the IO devices) that is involved in serializing and de-serializing the data.
2. The latency issues that are involved for transmitting the data over the network.
3. Serialization is quite slow. 


Moreover, XML serialization is insecure, consumes a lot of space on the disk and it works on public members and public classes and not on the private or internal classes.Therefore, it compels the developer to allow the class to be
accessed to the outside world. 

Types of Serialization

Serialization can be of the following types:

1. Binary Serialization
2. SOAP Serialization
3. XML Serialization
4. Custom Serialization

Following is an example on the binary, SOAP, And XML Serialization.In my example i have created dll and using it in my code.following is the code of dll file.In this i have declared the person class as serializable.

namespace personinfo
{
    [Serializable]
    public class person
    {
        int pid;
        public int Pid
        {
             get { return pid; }
             set { pid = value; }
        }
        string pname, padd;

        public string Padd
        {
            get { return padd; }
            set { padd = value; }
        }

        public string Pname
        {
            get { return pname; }
            set { pname = value; }
        }
        

    }
}


Binary Serialization:

It writes the contents of an object into binary form to a file.It serializes and deserializes an object in binary format.It serializes each and every data member of the class irrespective of the access specifier. In the example i have entered the value in textbox and that value is serialized and then deserialized to show it back in textboxes.

 private void button_bin_ser_Click(object sender, EventArgs e)
        {
            personinfo.person p1 = new personinfo.person();
            p1.Pid = Convert.ToInt32(textBox_pid.Text);
            p1.Pname = textBox_pname.Text;
            p1.Padd = textBox_add.Text;
            FileStream fs = new FileStream(@"z:\serialized.txt", FileMode.Create);
            BinaryFormatter bf = new BinaryFormatter();
            bf.Serialize(fs, p1);
            MessageBox.Show("Serialized");
            fs.Close();

        }


        private void button_bin_deser_Click(object sender, EventArgs e)
        {
            FileStream fs = new FileStream(@"z:\serialized.txt", FileMode.Open);
            BinaryFormatter bf = new BinaryFormatter();
            personinfo.person p1 = new personinfo.person();
            p1 = (personinfo.person)bf.Deserialize(fs);
            textBox_pid.Text=p1.Pid.ToString();
            textBox_pname.Text= p1.Pname;
            textBox_add.Text = p1.Padd;
            MessageBox.Show("Deserialized");
            fs.Close();
        }


XML Serialization:

It writes the contents of an object into a XML file.It serializes and deserializes an object in XML format.It can serialize only public data member of the class. In the example i have entered the value in textbox and that value is serialized and then deserialized to show it back in textboxes.

        private void button_xml_ser_Click(object sender, EventArgs e)
        {
            personinfo.person p1 = new personinfo.person();
            p1.Pid = Convert.ToInt32(textBox_pid.Text);
            p1.Pname = textBox_pname.Text;
            p1.Padd = textBox_add.Text;
            FileStream fs = new FileStream(@"z:\serialized.xml", FileMode.Create);
            XmlSerializer xs = new XmlSerializer(typeof(personinfo.person));
            xs.Serialize(fs, p1);
            MessageBox.Show("Serialized");
            fs.Close();
        }

        private void button_xmldeser_Click(object sender, EventArgs e)
        {
            FileStream fs = new FileStream(@"z:\serialized.xml", FileMode.Open);
            XmlSerializer xs = new XmlSerializer(typeof(personinfo.person));
            personinfo.person p1 = new personinfo.person();
            p1 = (personinfo.person)xs.Deserialize(fs);
            textBox_pid.Text = p1.Pid.ToString();
            textBox_pname.Text = p1.Pname;
            textBox_add.Text = p1.Padd;
            MessageBox.Show("Deserialized");
            fs.Close();
        }


SOAP Serialization:

It writes the contents of an object into platform-agnostic format.It serializes and deserializes an object into sOAP message.It serializes each and every data member of the class irrespective of the access specifier. In the example i have entered the value in textbox and that value is serialized and then deserialized to show it back in textboxes.

        private void button_soapdesr_Click(object sender, EventArgs e)
        {
            personinfo.person p1 = new personinfo.person();
            p1.Pid = Convert.ToInt32(textBox_pid.Text);
            p1.Pname = textBox_pname.Text;
            p1.Padd = textBox_add.Text;
            FileStream fs = new FileStream(@"z:\serialized.soap", FileMode.Create);
            SoapFormatter sf = new SoapFormatter();
            sf.Serialize(fs, p1);
            MessageBox.Show("Serialized");
            fs.Close();
        }

        private void button_soap_deser_Click(object sender, EventArgs e)
        {
            FileStream fs = new FileStream(@"z:\serialized.soap", FileMode.Open);
            SoapFormatter sf = new SoapFormatter();
            personinfo.person p1 = new personinfo.person();
            p1 = (personinfo.person)sf.Deserialize(fs);
            textBox_pid.Text = p1.Pid.ToString();
            textBox_pname.Text = p1.Pname;
            textBox_add.Text = p1.Padd;
            MessageBox.Show("Deserialized");
            fs.Close();
        }
    

Namespaces used are:

using System.Xml.Serialization; -> for xml
using System.Runtime.Serialization.Formatters.Binary; -> for binary
using System.Runtime.Serialization.Formatters.Soap; -> for soap


Custom Serialization

In some cases, the default serialization techniques provided by .NET may not be sufficient in real life. This is when we require implementing custom serialization. It is possible to implement custom serialization in .NET by implementing the ISerializable interface. This interface allows an object to take control of its own serialization and de-serialization process.

Example of Custom Serialization:
public class Employee: ISerializable
 {
   private int empCode;
   private string empName;
   protected Employee(SerializationInfo serializationInfo, StreamingContext
     streamingContext)
   {
     this.empCode = serializationInfo.GetInt32("empCode");
     this.empName = serializationInfo.GetString("empName");
   }
   public void ISerializable.GetObjectData(SerializationInfo serializationInfo,
     StreamingContext streamingContext)
   {
     serializationInfo.AddValue("empCode", this.empCode);
     serializationInfo.AddValue("empName", this.empName);
   }
 }



Note: XML serialization is insecure, consumes a lot of space on the disk and it works on public members and public classes and not on the private or internal classes. Therefore, it compels the developer to allow the class to be accessed to the outside world.