This article discusses the features of
JSON and how to perform a simple serialization of JSON data manually by 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
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 getting serialized.
How to serialize/de-serialize JSON
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
JSON serialization is about serializing
.NET type objects into JSON-encoded data and is widely used in situations when
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.
1. Create a console application in the name JSONConsole and add a new
class file Person.cs
internal class Person
internal string name;
internal int age;
2. Add reference to the assemblies System.ServiceModel.Web and System.Runtime.Serialization to the