Showing posts with label Serialization. Show all posts
Showing posts with label Serialization. Show all posts

Wednesday, June 13, 2012

Object Serialization in the .NET Framework

Introduction


Serialization can be defined as the process of storing the state of an object instance to a storage medium. During this process, the public and private fields of the object and the name of the class, including the assembly containing the class, is converted to a stream of bytes, which is then written to a data stream. When the object is subsequently deserialized, an exact clone of the original object is created.


When implementing a serialization mechanism in an object-oriented environment, you have to make a number of tradeoffs between ease of use and flexibility. The process can be automated to a large extent, provided you are given sufficient control over the process. For example, situations may arise where simple binary serialization is not sufficient, or there might be a specific reason to decide which fields in a class need to be serialized. The following sections examine the robust serialization mechanism provided with the .NET Framework and highlight a number of important features that allow you to customize the process to meet your needs.

JSON – Serialization and Deserialization

What is JSON?


JSON is another format of expressing data, just like XML. But, JSON is very simpler than XML, and tiny than XML. So, it is becoming popular in the web world to choose the JSON notation over XML since JSON notation are usually shorter, and less data to be transmitted if at all they were to be.


Okay, I understood a little about JSON. Give me an example!


 I know an example will get your understanding much better. Below is a simple example of how an object can be expressed in JSON notation. Let’s take a classical example of a Person object, and expressing myself as an object.


{

   “firstName”: “Rakki”,

   “lastName”:”Muthukumar”,

   “department”:”Microsoft PSS”,

   “address”: {

      “addressline1”: “Microsoft India GTSC”,

      “addressline2”: “PSS - DSI”,

      “city”: “Bangalore”,

      “state”: “Karnataka”,

      “country”: “India”,

      “pin”: 560028

   }

   “technologies”: [“IIS”, “ASP.NET”,“JavaScript”,“AJAX”]

}

In the above example, address is another object inside my Person, and technologies is an array or strings.