Asp.Net Serialization & Deserialization with C#.Net and VB.Net

By
Advertisement
While sending data (objects) over the network the need for serialization arises. The reason is our network infrastructure and hardware understands bits and bytes but not the objects. In this tutorial i am going to explain how to serialize and deserialize data without using any third party libraries i.e. using Serialize() method in the JavaScriptSerializer() static class in System.Web.Script.Serialization namespace provided by the .Net Framework itself.
Before you know how to serialize and deserialization data it is better to know what it means.

Serialization:

It is the process of converting objects into byte array or it is the process of in memory representation of an object to a disk based format which can be in any form like binary, JSON, BSON, XML, etc.
DeSerialization:

Deserialization is the reverse process, in which you get an object from the disk based format which can be in any form like binary, JSON, BSON, XML, etc.

Our Example:

Here to explain the process i have created a new project and I have created a class file called Employee with the below mentioned properties.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace SerializationDeserializaton
{
    public class Employee
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string Department { get; set; }
        public string Designation { get; set; }
        public DateOfBirth DOB = new DateOfBirth();
    }
    public class DateOfBirth
    {
        public int Date { get; set; }
        public int Month { get; set; }
        public int Year { get; set; }
    }
}

And then i added an aspx page, then included the System.Web.Script.Serialization namespace. I have created new instance of the employee class and assigned some values to the object. After that using JavaScriptSerializer().Serialize() method i have serialized the object. Below is the entire code i used.

using System;
using System.Web.Script.Serialization;

namespace SerializationDeserializaton
{
    public partial class Serialization : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            // Creating Instance(object) of employee class
            Employee empObj = new Employee();
           
            // Assinging values to employee object
            empObj.FirstName = "Kannadasan";
            empObj.LastName = "Govindasamy";
            empObj.Designation = "Tech Lead"; ;
            empObj.Department = "Development";
            empObj.DOB.Date = 30;
            empObj.DOB.Month = 4;
            empObj.DOB.Year = 1988;
           
            // Serialize Employee Object
            var json = new JavaScriptSerializer().Serialize(empObj);
            Response.Write(json.ToString());
        }
    }
}

The sample output will look like below.
Asp.Net Serialization & Deserialization with C#.Net and VB.Net - Output 1

For deserialize the serialized data i have created a another aspx page with the below design.
And now using JavaScriptSerializer().Deserialize<T>() method i have deserialized the json into employee object and binded the form fields. Below is the code i used.

using System;
using System.Web.Script.Serialization;

namespace SerializationDeserializaton
{
    public partial class De_Serialization : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            var json = "{ DOB: { Date: 30,Month: 4,Year:1988},FirstName: \"Kannadasan\",LastName: \"Govindasamy\",Department: \"Development\",Designation:\"Tech Lead\"}";
            Employee empObj = new Employee();
            empObj = new JavaScriptSerializer().Deserialize<Employee>(json);
            if (empObj != null)
            {
                lblFirstName.Text = empObj.FirstName;
                lblLastName.Text = empObj.LastName;
                lblDepartment.Text = empObj.Department;
                lblDesignation.Text = empObj.Designation;
                lblDateOfBirth.Text = empObj.DOB.Date + "/" + empObj.DOB.Month + "/" + empObj.DOB.Year;
            }
        }
    }
}

Now if you run the code you will get the below output.
Asp.Net Serialization & Deserialization with C#.Net and VB.Net - Output 2
Source Code:
You May Also Like:


0 comments:

Post a Comment

Online Casino