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

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)     { ...

Puzzles for kids - 12 Days Of Christmas

According to the traditional song, on the first day of Christmas (25th December), my true love sent to me: . A partridge in a pair tree On the second day of Christmas (26th December), my true love sent to me THREE presents: . Two turtle doves . A partridge in a pear tree On the third day of Christmas (27th December and so on) my true love sent to me SIX presents: . Three French hens . Two turtle doves . A partridge in a pear tree This carries on until the the twelfth day of Christmas, when my true love sends me: Twelve drummers drumming Eleven pipers piping Ten lords a-leaping Nine ladies dancing Eight maids a-milking Seven swans a-swimming Six geese a-laying Five gold rings Four calling birds Three French hens Two turtle doves A partridge in a pear tree After the twelve days of Christmas are over, how many presents has my true love sent me altogether? Our Solution: 1 + 3 + 6 + 10 + 15 + 21 + 28 + 36 + 45 + 55 + 66 + 78 = 364 presents Which is really interesting when you think ...

Asp.Net DataTable Manipulation - Add, Update,Delete & Sort DataTable in C#.Net & VB.Net

In the asp.net developer's life it is very common to come across the datatable manipulation. So here i have decided to explain the datatable manipulation like adding records to datatable, editing and updating datatable records,deleting records from datatable based on the condition and finally sorting datatable based on columns.