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

C# Extension Methods Example

In this article i am going to explain about Extension Methods  with example. Extension methods enable you to "add" methods to existing types without creating a new derived type, recompiling, or otherwise modifying the original type. Extension methods are a special kind of static method, but they are called as if they were instance methods on the extended type. For client code written in C# and Visual Basic, there is no apparent difference between calling an extension method and the methods that are actually defined in a type. If you do implement extension methods for a given type, remember the following points: An extension method will never be called if it has the same signature as a method defined in the type. Extension methods are brought into scope at the namespace level. For example, if you have multiple static classes that contain extension methods in a single namespace named Extensions, they will all be brought into scope by the using Extensions; directive. ...

VBScript equivalent string functions in C#.Net

C# Method VBScript Description IndexOf() InStr Returns the position of the first occurrence of one string within another. The search begins at the first character of the string - InStrRev Returns the position of the first occurrence of one string within another. The search begins at the last character of the string ToLower() LCase Converts a specified string to lowercase SubString() Left Returns a specified number of characters from the left side of a string Length() Len Returns the number of characters in a string TrimStart() LTrim Removes spaces on the left side of a string TrimEnd() RTrim Removes spaces on the right side of a string Trim() Trim Removes spaces on both the left and the right side of a string SubString() M...