Serialization and Deserialization
19:38What is Serialization?
Serialization is a process of converting the object into a form so that it can be stored on a file, database, or memory, it can be transferred across the network. Its main purpose is to save the state of the object so that it can be recreated when needed.What is Deserialization?
Deserialization is the reverse process of serialization. It is the process of getting back from serialized object so that it can be loaded into memory.Types of Serialization
There are mainly 3 type:I) Binary Serialization
II) XML Serialization
III) JSON Serialization
Example
he Lets create a simple application to explain Serialization and Deserialization:We have create a simple form like below:
After that we need to create a new class EmployeeDetails
[Serializable] public class EmployeeDetails { private string FirstName; private string LastName; private string Phone; public string firstName { get { return FirstName; } set { FirstName = value; } } public string lastName { get { return LastName; } set { LastName = value; } } public string phone { get { return Phone; } set { Phone = value; } } [NonSerialized] public string additionalInfo; }We have put [Serializable] on the top of the class and which method are not serialize put [NonSerialized] attribute.
Now run this application and fill all the fields. After click Serialize button use BinaryFormatter to serialize the object in BinaryFormat. Make a file using FileStream named Employee.Binary. In this file our serialized object will be stored binF.Serialize(fs, empDetail); will serialize the object empDetail and store it in file Employee.Binary.
Serialize button code:
private void btnSerialization_Click(object sender, EventArgs e) { EmployeeDetails empDetail = new EmployeeDetails { firstName = txtFirstName.Text, lastName = txtLastName.Text, phone = txtPhone.Text }; BinaryFormatter binF = new BinaryFormatter(); FileStream fs = new FileStream("Employee.Binary", FileMode.Create, FileAccess.Write, FileShare.None); try { using (fs) { binF.Serialize(fs, empDetail); MessageBox.Show("Serialization completed!"); } } catch (Exception) { throw; } }If we see under the bin folder. We have found binary file name Employee.Binary.
Now we have clear the value of the textbox and click Deserialize button. We have see that the previous value will come the proper place.
Now if we click Deserialize button use BinaryFormatter to deserialize the object from file Employee.Binary. After the object is deserialized update the values of text boxes.
Deserialize button code:
private void btnDeserialization_Click(object sender, EventArgs e) { EmployeeDetails empDetail = new EmployeeDetails(); BinaryFormatter binF = new BinaryFormatter(); FileStream fs = new FileStream("Employee.Binary", FileMode.Open, FileAccess.Read, FileShare.None); try { using (fs) { empDetail = (EmployeeDetails)binF.Deserialize(fs); txtFirstName.Text = empDetail.firstName; txtLastName.Text = empDetail.lastName; txtPhone.Text = empDetail.phone; MessageBox.Show("Deserialization completed!"); } } catch (Exception) { throw; } }
Serialize Object In XML Format
1) For xml Serialization we have need to use XmlSerializer instead of BinaryFormatterXmlSerializer xmls = new XmlSerializer(typeof(EmployeeDetails));2) Change the filename from Employee.Binary to Employee.xml.
3) We have need to change [Xmlgnore] instead of [NonSerialized] in EmployeeDetails class.
0 comments