JSON serialization and de-serialization in WCF DataContracts

Explains how to use JSON serialization and deserialization in WCF DataContracts.

Introduction

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 a 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.

JSON and XML

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 get 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 into instances of .NET Framework types and objects to work at the client ends.

In cases where you need to directly work with JSON data, the 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. A .NET objects that needs to be serialized must have a DataContract attribute attached to it, and its members must be attached with a DataMember attribute.

The DataContractJsonSerializer class consists of a 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 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

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

Output:

Deserialized Person data:
Name: Bala
age=22

Conclusion

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 client applications and servers. It also tells how to perform serialization of a .NET object to JSON encoding and back in WCF services.