Saturday, 15 February 2014

Serialization VS Deserialization concepts of programming in c#

 

In simple words, we can say an object conversion in text stream is serialization and text stream conversion in object of a class is called deserialization. Or we can say XML serialization is the process of converting an object's public properties and fields to a serial format (in this case, XML) for storage or transport. Deserialization re-creates the object in its original state from the XML output.  Let's say I have a complex object which has a hierarchy of classes. Now in case we need XML stream from this object, we will go for serialization. In the same manner, we have XML string and we want to convert this in a class and object format, we will go for deserialization.

 

Why Serialization and Deserialization?

Let's say we have a very complex object and we need XML format for our XSLT rendering on HTML page. Then we have one option that we will write a XML file in the disk after parsing object variable and than load the XML file in XmlDocument object. But is it really a good approach? No, of course not. Why so. This is because in large applications, we have so many users and we will be writing files for every one. This will take lots of space as well as it is risky that might be files are shared among the users or any human being can read that file.


So what do we do now? Yes at this time, go with serialization, get an XML string and just load it to XmlDocument.

 

How Do We Achieve This?

 

XML Serialization

 

For XML Serialization, we need to use System.Xml.Serialization, because we need to use XmlSerialization class which is provided in System.Xml.Serialization.  To understand this, we assume one example of Category and Items. We have two classes. One is Category and another is Items. Category has CategoryID, Category Name and Array of Item. While Item has Item ID, Item Name, Item Price and Item Quantity in Stock.

 

image

 

 

image

 

image

 

 

image

 

 

 

image

 

 

image

 

 

 

Binary Serialization and Deserialization

 

image

 

image

 

 

image

 

 

 

SOAP Serialization and Deserialization

 

image

image

 

 

 

image

 

 

 

JSON Serialization and Deserialization

JSON (JavaScript Object Notation) is an efficient data encoding format that enables fast exchanges of small amounts of data between client browsers and AJAX-enabled Web services.

This topic demonstrates how to serialize .NET type objects into JSON-encoded data and then deserialize data in the JSON format back into instances of .NET types using the DataContractJsonSerializer. This example uses a data contract to demonstrate serialization and deserialization of a user-defined Person type.

Normally, JSON serialization and deserialization is handled automatically by Windows Communication Foundation (WCF) when you use data contract types in service operations that are exposed over AJAX-enabled endpoints. However, in some cases you may need to work with JSON data directly - this is the scenario that this topic demonstrates.

 

To define the data contract for a Person

 

  1. Define the data contract for Person by attaching the DataContractAttribute to the class and DataMemberAttribute attribute to the members you want to serialize. For more information about data contracts, see Designing Service Contracts.

image

 

 

To serialize an instance of type Person to JSON

 

image

 

To deserialize an instance of type Person from JSON

 

image

 

 

 

JavaScriptSerializer Class

The JavaScriptSerializer class is used internally by the asynchronous communication layer to serialize and deserialize the data that is passed between the browser and the Web server. You cannot access that instance of the serializer. However, this class exposes a public API. Therefore, you can use the class when you want to work with JavaScript Object Notation (JSON) in managed code.

To serialize an object, use the Serialize method. To deserialize a JSON string, use the Deserialize or DeserializeObject methods. To serialize and deserialize types that are not natively supported by JavaScriptSerializer, implement custom converters by using the JavaScriptConverter class. Then register the converters by using the RegisterConverters method.

 

clip_image004

 

 

 

Json.NET

Json.NET is a popular high-performance JSON framework for .NET.

 

Benefits and Features:

  •     Flexible JSON serializer for converting between .NET objects and JSON
  •     LINQ to JSON for manually reading and writing JSON
  •     High performance, faster than .NET’s built-in JSON serializers
  •     Write indented, easy to read JSON
  •     Convert JSON to and from XML
  •     Supports .NET 2, .NET 3.5, .NET 4, .NET 4.5, Silverlight, Windows Phone and Windows 8 Store etc.

 

clip_image006

clip_image008

clip_image010

clip_image012

 

 

Blogger Labels: Serialization,Deserialization,concepts,conversion,text,properties,storage,output,hierarchy,manner,XSLT,HTML,option,disk,XmlDocument,users,Achieve,System,XmlSerialization,example,Category,Items,CategoryID,Name,Array,Item,Price,Stock,Binary,SOAP,JSON,data,Person,Define,DataContractAttribute,DataMemberAttribute,information,Service,Contracts,instance,serialize

1 comment:

  1. Really awesome blog. Your blog is really useful for me. Thanks for sharing this informative blog. Keep update your blog.

    www.imarksweb.org

    ReplyDelete