This article is compatible with the latest version of Silverlight.
Recently I had to work with JSON files and more especially with serialization and deserialization using Silverlight and .NET. The information I found was very limited, so I had to figure it out by myself. When I managed, I decided to write it down for people who would have had the same problems as me.
Introduction
Let’s start with a short introduction of what JSON is. It stays for JavaScript Object Notation and is used as an alternative of the XML. Here is a simple example for a JSON file:
{"FirstName":"Martin","LastName":"Mihaylov"} for a single object
And
[{"FirstName":"Martin","LastName":"Mihaylov"},{"FirstName":"Emil","LastName":"Stoychev"}] for multiple objects.
It looks like an array. Depending on the object that is serialized it could look really complicated.
Serializing
In .Net there is class DataContractJsonSerializer that works great for serializing and deserializing JSON files. To use it you must add a referrence to the System.ServiceModel.Web, although it's in the System.Runtime.Serialization.Json namespace. Before we start with the serialization we have to create a class that can be serialized to JSON. I am going to use a very simple example for this article:
[DataContract]
public class Person
{
[DataMember]
public string FirstName { get; set; }
[DataMember]
public string LastName { get; set; }
}
In order to be serializable with DataContractJsonSerializer we have to set a [DataContract] attribute. The properites that will be used by the serialization must have [DataMember] attributes. Note: To use these attributes add a reference to System.Runtime.Serialization;
Now we are ready to begin with the serialization. Let's create a method that takes our object as an argument and returns a string in JSON format:
public static string SerializeToJsonString( object objectToSerialize )
{
using( MemoryStream ms = new MemoryStream() )
{
DataContractJsonSerializer serializer =
new DataContractJsonSerializer( objectToSerialize.GetType() );
serializer.WriteObject( ms, objectToSerialize );
ms.Position = 0;
using( StreamReader reader = new StreamReader( ms ) )
{
return reader.ReadToEnd();
}
}
}
We create an object of DataContractJsonSerializer type and in the constructor we set the type that will be serialized. Then using the WriteObject method our object is serialized and written to a stream. After that we use a StreamReader to read the stream and here is our JSON string.
Deserializing
Now let's try to deserialize an object of type Person. We create the following method:
public static Person DeserializeToPerson( string jsonString )
{
using( MemoryStream ms = new MemoryStream( Encoding.Unicode.GetBytes( jsonString ) ) )
{
DataContractJsonSerializer serializer =
new DataContractJsonSerializer( typeof( Person ) );
return ( Person )serializer.ReadObject( ms );
}
}
Here we create a stream from the json string and pass it to the ReadObject method of the serializer. And our object is deserialized.
If you want to deserialize a list of objects it's done the same way:
public static List<Person> DeserializeToListOfPersons( string jsonString )
{
using( MemoryStream ms = new MemoryStream( Encoding.Unicode.GetBytes( jsonString ) ) )
{
DataContractJsonSerializer serializer =
new DataContractJsonSerializer( typeof( List<Person> ) );
return ( List<Person> )serializer.ReadObject( ms );
}
}
The only thing that's different is the type. Here comes the question: Can we have only one method and reuse it independent of the type? Yes, we can using a template method. Here is how the method looks now:
public static T Deserialize<T>( string jsonString )
{
using( MemoryStream ms = new MemoryStream( Encoding.Unicode.GetBytes( jsonString ) ) )
{
DataContractJsonSerializer serializer = new DataContractJsonSerializer( typeof( T ) );
return ( T )serializer.ReadObject( ms );
}
}
"T" is a type argument and we use it to make the method independent of the type. When we call the method, we can specify it:
List<Person> persons = Deserialize<List<Person>>( jsonString )
To learn more about that read this article.
Summary
These are the basic things needed to serialize and deserialize object to/from JSON with .NET using DataContractJsonSerializer. So go ahead and find some JSON files to try it!