Thursday, October 24, 2013

Serializing & deserializing an object to & from JSON


Dot net framework 3.5 has native support for serializing an object into JSON string.to illustrate this we will create a console application,in this console application we will first create a simple entity class called device with properties devicename,brandname,price,resolution & devicecode.
  Now in our console's main we will create a LIST based collection from our Device class.
 To serialise this collection object we are using functionality from system.web.script namespace which requires adding reference to corresponding dll.
  We created two static function one or purpose of serializing namely SerializeJsonToString & other for purpose of deserializing namely DeSerializeJsonStringToObj.
  For serializing we need to create an object of JavaScriptSerializer class which has method Serialize() which will take an object and convert it into equivalent JSON string.
   For deserialization we will use  Deserialize() method to which we first need which type of object we are deserializing in our case it is List<Device>  which is LIST based collection of Device objects.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Script.Serialization;

namespace JsonSerialization
{
    class Program
    {
        static void Main(string[] args)
        {
            Device Note3 = new Device { DeviceName = "Samsung Galaxy Note III", BrandName = "Samsung", Price = "43,787", Resolution = "1024X768", DeviceCode = "GTN623" };
            Device Note2 = new Device { DeviceName = "Samsung Galaxy Note II", BrandName = "Samsung", Price = "33,250", Resolution = "800X600", DeviceCode = "GTN523" };
            Device Mega = new Device { DeviceName = "Samsung Galaxy Mega", BrandName = "Samsung", Price = "23,787", Resolution = "600X425" ,DeviceCode="GTN123"};
            Device Lumia = new Device { DeviceName = "Nokia Lumia 520", BrandName = "Nokia", Price = "20,787", Resolution = "600X425" };

            List<JsonSerialization.Device> DeviceList = new List<JsonSerialization.Device>();
            DeviceList.AddRange(new Device[] { Note3, Note2, Mega, Lumia });

            string SerializedString = SerializeJsonToString(DeviceList);

            List<Device> DeserialzedDeviceList = new List<Device>();
            DeserialzedDeviceList = DeSerializeJsonStringToObj(SerializedString);
      
            Console.ReadKey();
        }

        public static string SerializeJsonToString(List<JsonSerialization.Device> obj2Serialize)
        {
            JavaScriptSerializer oSerializer = new JavaScriptSerializer();
            string sJSON = oSerializer.Serialize(obj2Serialize);
            return sJSON;
        }
        public static List<Device> DeSerializeJsonStringToObj(string jsonString)
        {
            JavaScriptSerializer oSerializer = new JavaScriptSerializer();
            List<Device> DeserialzedDeviceList  = oSerializer.Deserialize<List<Device>>(jsonString);
            return DeserialzedDeviceList;
        }
    }

    //entity class
    public class Device
    {
        public string DeviceName 
        { 
            get; set; 
        }
        public string BrandName
        {
            get; set; 
        }
        public string DeviceCode 
        { 
            get; set; 
        }
        public string Price
        {
            get;
            set;
        }

        public string Resolution
        {
            get;
            set;
        }
    }
}

No comments:

Post a Comment