Serialization:
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:
- Facilitate the transportation of an object through a network
- Create a clone of an object
Serialization Disadvantages:
- 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.
- The latency issues that are involved for transmitting the data over the network.
- 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:
- Binary Serialization
- SOAP Serialization
- XML Serialization
- 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.
using System;
{
[Serializable]
class person
{
public int Pid { get; set; }
public string Paddress { get; set; }
public string Pname { get; set; }
}
}
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.
Double Clkick on Serialization Button and write code
private void button1_Click(object sender, EventArgs e)
{
person p1 = new person();
p1.Pid = Convert.ToInt32(textBox_pid.Text);
p1.Pname = textBox_pname.Text;
p1.Paddress = textBox_add.Text;
FileStream fs = new FileStream(@"c:\serialization.txt", FileMode.Create, FileAccess.Write);
BinaryFormatter bf = new BinaryFormatter();
bf.Serialize(fs, p1);
MessageBox.Show("Serialized Successfully");
fs.Close();
}
Double Clkick on De-Serialization Button and write code
private void button2_Click(object sender, EventArgs e)
{
FileStream fs = new FileStream(@"c:\serialization.txt", FileMode.Open, FileAccess.Read);
BinaryFormatter bf = new BinaryFormatter();
person p1 = new person();
p1 = (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.
Double Click on Serialization Button
private void button1_Click(object sender, EventArgs e)
{
person p1 = new person();
p1.Pid = Convert.ToInt32(textBox_pid.Text);
p1.Pname = textBox_pname.Text;
p1.Padd = textBox_add.Text;
FileStream fs = new FileStream(@"c:\serialization.xml", FileMode.Create, FileAccess.Write);
XmlSerializer xs = new XmlSerializer(typeof(personinfo.person));
xs.Serialize(fs, p1);
MessageBox.Show("Serialized");
fs.Close();
}
Double Click on De-Serialization Button
private void button2_Click(object sender, EventArgs e)
{
FileStream fs = new FileStream(@"z:\serialization.xml", FileMode.Open, FileAccess.Read);
XmlSerializer xs = new XmlSerializer(typeof(personinfo.person));
person p1 = new person();
p1 = (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.
Double Click On Serialization Button
private void button1_Click(object sender, EventArgs e)
{
person p1 = new person();
p1.Pid = Convert.ToInt32(textBox_pid.Text);
p1.Pname = textBox_pname.Text;
p1.Padd = textBox_add.Text;
FileStream fs = new FileStream(@"c:\serialized.soap", FileMode.Create, FileAccess.Write);
SoapFormatter sf = new SoapFormatter();
sf.Serialize(fs, p1);
MessageBox.Show("Serialized");
fs.Close();
}
Double Click On De-Serialization Button
private void button2_Click(object sender, EventArgs e)
{
FileStream fs = new FileStream(@"c:\serialized.soap", FileMode.Open, FileAccess.Read);
SoapFormatter sf = new SoapFormatter();
person p1 = new person();
p1 = (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();
}
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);
}
}
0 Comments