Different ways of doing serialization and deserialization in .Net (Asp.net/Asp.net MVC)(Part1)


Nowadays so many times we will get requirement to do serialization and deserialization process in .net application.
Firstly we will know what this process is,

Serialization: It is the process of converting an object into a stream of bytes in order to store the object or transmit it to memory, a database, or a file. Its main purpose is to save the state of an object in order to be able to recreate it when needed.

Deserialization: It is the reverse process of Serialization.

In asp.net we can achieve it using so many ways

1. Using System.Runtime.Serialization.Json Namespace

2. Using System.Web.Script.Serialization; Namespace

3. Using Newtonsoft.Json; Open Source dll

Method 1 : using System.Runtime.Serialization.Json Namespace

In approach we will firstly create the C# object then convert into Json object and revert back to its original state

Step1: Create the C# Class like this and use System.Runtime.Serialization namespace as given below

using System.Runtime.Serialization;

namespace WebApplication1
{
    [DataContract]
    public class Emp
    {
        [DataMember]
        public int Id { get; set; }

        [DataMember]
        public string EmpName { get; set; }

        [DataMember]
        public string EmpAddress { get; set; }
    }
}

Step 2:

write the code in code behind file of asp.net as given below

using System;
using System.IO;
using System.Runtime.Serialization.Json;
using System.Text;

namespace WebApplication1
{
    public partial class Demo : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
        }

        protected void btnMethod1_Click(object sender, EventArgs e)
        {
            //Serilization process using  System.Runtime.Serialization

            Emp objEmp = new Emp()
            {
                Id = 1021,
                EmpName = "Chandradev",
                EmpAddress = "Bangalore"
            };

            DataContractJsonSerializer objJS = new DataContractJsonSerializer(typeof(Emp));
            string json = string.Empty;
            using (MemoryStream objMS = new MemoryStream())
            {
                objJS.WriteObject(objMS, objEmp);
                objMS.Position = 0;
                using (StreamReader sr = new StreamReader(objMS))
                {
                    json = sr.ReadToEnd();
                    Response.Write("<b>Converting to Json </b> " + "</br>");
                    Response.Write("</br>");
                    Response.Write(json);
                    sr.Close();
                    objMS.Close();
                }
            }

            using (var ms = new MemoryStream(Encoding.Unicode.GetBytes(json)))
            {
                Emp objEmp1 = (Emp)objJS.ReadObject(ms);
                Response.Write("</br>");
                Response.Write("</br>");
                Response.Write("<b>Converting Json to .Net Object</b>");
                Response.Write("</br>");
                Response.Write("</br>");
                Response.Write("Id: " + objEmp1.Id + "</br>");
                Response.Write("EmpName: " + objEmp1.EmpName + "</br>");
                Response.Write("EmpAddress: " + objEmp1.EmpAddress + "</br>");
            }
        }
    }
}

Step 3: Now run the application you will see the process of serialization and deserialization.

Summary: This approach will be more suitable in WCF application.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: