Skip to main content

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

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:


Comments

Popular posts from this blog

Sort Dictionary Based On Value In Asp.Net And C#.Net | Convert Dictionary into KeyValuePair or KeyValuePair into Dictionary.

In this tutorial i am going to explain about how to sort dictionary object based on value in asp.net and C#.Net or convert unsorted dictionary to sorted dictionary object in C#.Net and VB.Net or Convert Dictionary into KeyValuePair or KeyValuePair into Dictionary.

Code To Convert rupees(numbers) into words using C#.Net

Introduction: In my previous article I have explained about how to validate emailid using javascript . In this article I am going to explain about code used to convert rupees(numbers) into words using C#.Net . Explanation: For explanation purpose I have a page. It has a textbox to input the numbers. And when you click on the convert to words button then it will convert the input numbers into words and shows it in the below label. Below is the C# code used to do this functionality. public static string NumbersToWords( int inputNumber) {     int inputNo = inputNumber;     if (inputNo == 0)         return "Zero" ;     int [] numbers = new int [4];     int first = 0;     int u, h, t;     System.Text. StringBuilder sb = new System.Text. StringBuilder ();     if (inputNo < 0)     { ...

C# code to send mail using smtp from gmail,yahoo mail and live mail

Introduction: In my previous article I have explained about   How to bind/Unbind events in jQuery . In this article I am going to explain about how to send mail from ASP.Net using gmail,yahoomail and live mail credentials. Explanation: First Include the below namespaces in your code behind file. using System; using System.Net; using System.Net.Mail;