What is Serialization?
Serialization is the process of bringing an object into a form that it can be written on stream. It's the process of converting the object into a form so that it can be stored on a file, database, or memory; or, 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?
As the name suggests, deserialization is the reverse process of serialization. It is the process of getting back the serialized object so that it can be loaded into memory. It resurrects the state of the object by setting properties, fields etc.
Types
- Binary Serialization
- XML Serialization
- JSON Serialization
Here I will be giving you an example of how to serialize and deserialize an object using binary formatter or xml formatter.
Create a new Windows Form Application and add few controls to it as shown below.
Now make a class named Employee:
Put [Serializable] on top of the class. For those attributes which you don't want to serialize put [NonSerialized] on them.
Here you will find the file.
Change the filename from "employee.binary" to "employee.xml".
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace WindowsFormsApplication1
- {
- [Serializable]
- public class Employee
- {
- private string Name;
- public string name
- {
- get
- {
- return Name;
- }
- set
- {
- Name = value;
- }
- }
- private string Phone;
- public string phone
- {
- get
- {
- return Phone;
- }
- set
- {
- Phone = value;
- }
- }
- private DateTime DoB;
- public DateTime dob
- {
- get
- {
- return DoB;
- }
- set
- {
- DoB = value;
- }
- }
- private string Department;
- public string department
- {
- get
- {
- return Department;
- }
- set
- {
- Department = value;
- }
- }
- private int Salary;
- public int salary
- {
- get
- {
- return Salary;
- }
- set
- {
- Salary = value;
- }
- }
- [NonSerialized]
- public string additionalInfo;
- }
- }
Add a click event for Serialize button. When clicking on the serialize button we want to serialize the object of class Employee and store it in a file named "employee.binary".
- private void Serialize_Click(object sender, EventArgs e)
- {
- Employee emp = new Employee {
- name = textBoxName.Text,
- phone = textBoxPhone.Text,
- dob = dateTimePickerDoB.Value,
- department = textBoxDepartment.Text,
- salary = Convert.ToInt32(textBoxSalary.Text),
- additionalInfo = "We don't want it to serialize"
- };
- BinaryFormatter bf = new BinaryFormatter();
- FileStream fsout = new FileStream("employee.binary", FileMode.Create, FileAccess.Write, FileShare.None);
- try
- {
- using (fsout)
- {
- bf.Serialize(fsout, emp);
- label6.Text = "Object Serialized";
- }
- }
- catch
- {
- label6.Text = "An error has occured";
- }
- }
Use BinaryFormatter to serialize the object in BinaryFormat. Make a file using FileStream named "employee.binary". In this file your serialized object will be stored. bf.Serialize(fsout, emp) will serialize the object "emp" and store it in file "employee.binary".
The next thing is to write the code to deserialize the object. Add a click event for Deserialize button. When clicking on the Deserialize button we want to deserialize the object and show its values on screen.
- private void Deserialize_Click(object sender, EventArgs e)
- {
- Employee emp = new Employee();
- BinaryFormatter bf = new BinaryFormatter();
- FileStream fsin = new FileStream("employee.binary", FileMode.Open, FileAccess.Read, FileShare.None);
- try
- {
- using (fsin)
- {
- emp = (Employee) bf.Deserialize(fsin);
- label6.Text = "Object Deserialized";
- textBoxName.Text = emp.name;
- textBoxPhone.Text = emp.phone;
- dateTimePickerDoB.Value = emp.dob;
- textBoxDepartment.Text = emp.department;
- textBoxSalary.Text = emp.salary.ToString();
- }
- }
- catch
- {
- label6.Text = "An error has occured";
- }
- }
Use BinaryFormatter to deserialize the object from file "employee.binary". After the object is deserialized update the values of text boxes.
Now let's execute our program and add some values to text boxes as shown below.
Click on Serialize.
The message says that the object is serialized. To make sure that it is serialized go to,
WindowsFormsApplication\WindowsFormsApplication\bin\Debug\employee.binary
Here you will find the file.
Now to deserialize the object, remove the text from the text boxes and change the date to something else.
Click on Deserialize and you will get the following.
Serialize object in XML format,
For xml serialization use XmlSerializer instead of BinaryFormatter.
- XmlSerializer xs = new XmlSerializer(typeof(Employee));
and use [Xmlgnore] instead of [NonSerialized] in Employee class. The rest will be the same.
No comments:
Post a Comment