Home » Articles


Sponsored links

CodeDigest Navigation

Technology News
No News Feeds available at this time.

Community News
No News Feeds available at this time.
JSON serialization and de-serialization in WCF DataContracts

By Balamurali Balaji
Posted On May 23,2009
Article Rating:
Average Rating: 2
No of Ratings: 1
No of Comments: 6
Category: WCF
Print this article.

JSON serialization and de-serialization in WCF DataContracts




With XML becoming a thing of past for inter process communication and data exchange on the Internet infrastructures, JSON is more increasingly getting popular for providing quicker and better packaging of data across the wires. To be specific, as the need for partial rendering of web pages increases with the dynamic exchange of data using AJAX-enabled Web services, JSON has become more compact and simpler means to communicate between client calls and server processes thus providing a rich user experience. Moreover, Windows Communication Foundation (WCF) processes JSON messages using an internal, hidden mapping between JSON data and the XML infoset for data exchange.

This article discusses the features of JSON and how to perform a simple serialization of JSON data manually by an example.


JavaScript Object Notation

JSON (JavaScript Object Notation) is an efficient data encoding format that enables fast exchanges of small amounts of data between client browsers and server components. JSON encodes data using a subset of the object literals of JavaScript.



In WCF services and AJAX enabled web services, JSON encoding has become a better alternative in place of XML for exchange of data and messages. In general, XML elements are mapped into JSON strings while serialization but it would be interesting to know how other parts of XML getting serialized.


How to serialize/de-serialize JSON data?

Currently, in the .NET Framework 3.5, JSON serialization and de-serialization is implemented in WCF and AJAX-enabled Web Services.  Retaining the other forms of serialization techniques such as Binary, SOAP and XML, JSON is newly included in the framework and is handled automatically by Windows Communication Foundation (WCF) when you use data contract types in service operations that are exposed over AJAX-enabled endpoints.


JSON serialization is about serializing .NET type objects into JSON-encoded data and is widely used in situations when writing Asynchronous JavaScript and XML (AJAX)-style Web applications. AJAX support in Windows Communication Foundation (WCF) is optimized for use with ASP.NET AJAX through the ScriptManager control.


JSON de-serialization is about de-serializion of JSON data in to instances of .NET Framework types and objects to work at the client ends.


In cases where you need to directly work with JSON data, DataContractJsonSerializer class is used and it acts as a serialization engine that converts JSON data into instances of .NET Framework types and back into JSON data. The .Net objects that need to be serialized must have a DataContract attribute attached to it and its members must be attached with a DataMember attribute.


DataContractJsonSerializer class consists of WriteObject method to serialize a specific object to JSON data and writes the resultant data to a stream. The ReadObject method reads the document stream in JSON format and returns the deserialized object. But, this class currently has a bigger limitation of that multiple members(fields, methods etc) of the DataContract class cannot be serialized.


Here is a sample application that shows how to serialize and deserialize JSON data. Note that the DataContractJsonSerializer class works independently with out requiring a WCF configuration of your application. This helps us in serializing and de-serializing data manually.


Sample Application:


1.      Create a console application in the name JSONConsole and add a new class file Person.cs


using System.Runtime.Serialization;


namespace JSONConsole



    internal class Person



        internal string name;


        internal int age;



2.      Add reference to the assemblies System.ServiceModel.Web and System.Runtime.Serialization to the project.


3.      In the program.cs code file, write the following in the Main method:


using System.IO;

using System.Runtime.Serialization.Json;

namespace JSONConsole


    class Program


        static void Main(string[] args)


            Person p = new Person();

   = "Bala";

            p.age = 22;

            MemoryStream stream1 = new MemoryStream();

DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(Person));

            ser.WriteObject(stream1, p);

            stream1.Position = 0;

            StreamReader sr = new StreamReader(stream1);

            Console.Write("JSON serialized Person object: ");





Note that the namespace System.Runtime.Serialization.Json is found in the System.ServiceModel.Web due to the usage of DataContractJsonSerializer class in WCF and AJAX related services.


4.      Build the application and see the serialized results as below in the console window.

JSON serialized Person object: {"age":22,"name":"Bala"}


5.      Now, for de-serialization,  add the following code in the main method:


      stream1.Position = 0;

      Person p2 = (Person)ser.ReadObject(stream1);

      Console.WriteLine("Deserialized Person data:");

      Console.WriteLine("Name: " +;




6.      Build the application once again and see the deserialized results:


Deserialized Person data:

Name: Bala





This article has dealt with the new encoding format JavaScript Object Notation (JSON) introduced in .NET Framework 3.5 that performs quicker and faster data exchanges between the client applications and servers. It also tells how to perform serialization of a .Net object to JSON encoding and back in WCF services.

Similar Articles
You can contribute to CodeDiget.Com:
Donate to
Article Feedback
U9HaMw , [url=]jlgpszozpmjy[/url], [link=]xqnvblbsskwr[/link],
U9HaMw , [url=]jlgpszozpmjy[/url], [link=]xqnvblbsskwr[/link],
tJvph3 <a href="">yqagfeinwwcj</a>
tJvph3 <a href="">yqagfeinwwcj</a>
6PQa0j , [url=]iiyixlxrnagl[/url], [link=]aoeametcqrfq[/link],
6PQa0j , [url=]iiyixlxrnagl[/url], [link=]aoeametcqrfq[/link],
mDgsvy <a href="">qaveecrjjaef</a>
mDgsvy <a href="">qaveecrjjaef</a>
So that's the case? Quite a relevtaion that is.
So that's the case? Quite a relevtaion that is.
Congrats on stealing this from