Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Windows Phone 7 Serialization: DataContract JSON

DZone's Guide to

Windows Phone 7 Serialization: DataContract JSON

· Mobile Zone ·
Free Resource

This time I am going to cover DataContract JSON Serialization using DataContractJsonSerializer. This type of serialization is very similar to the DataContract Serialization described in my previous tutorial. DataContract JSON Serialization is mostly used by Windows Communication Foundation (WCF) services, but I will try to apply this approach to Windows Phone 7 environment.

Additional information


Adding references

Keep in mind that DataContractAttribute requires System.Runtime.Serialization namespace to be added to project. To do so right click the project name and choose Add Reference there. Select System.Runtime.Serialization namespace on .NET tab (check image bellow).

eugenedotnet add system runtime serialization namespace for data contract serialization

DataContractJsonSerializer is a class of another assembly System.ServiceModel.Web therefore you need to add this assembly to your project too.

eugenedotnet add servicemodel web namespace for data contract json serialization

Creating a sample class

I am going to use exactly the same class I have created for DataContract Serialization (check code bellow). To allow object to be serialized using DataContract serialization you have to add DataContractAttribute to the class. XML Generation can be controlled using DataMemberAttribute for those properties that need to be serialized and IgnoreDataMemberAttribute for those properties that need to be ignored during serialization. Public properties will be automatically exposed during the serialization.

[DataContractAttribute]
public class SampleData
{
    [DataMember]
    public string ContentText { get; set; }
 
    [DataMember]
    public List<int> SomeItems { get; set; }
 
    public SampleData()
    {
        ContentText = "some text";
        SomeItems = new List<int>() { 1, 2, 3 };
    }
}

Serialization

To serialize a custom object using DataContact JSON Serialization you will need a Stream object to write serialized data to and a custom object for serialization. Simply create an instance of DataContractJsonSerializer based on object type and pass stream and custom object as input parameters to WriteObject method(check code bellow).

public static void Serialize(Stream streamObject, object objForSerialization)
{
    if (objForSerialization == null || streamObject == null)
        return;
 
    DataContractJsonSerializer ser = new DataContractJsonSerializer(objForSerialization.GetType());
    ser.WriteObject(streamObject, objForSerialization);
}

Deserialization

Deserialization is similar to serialization, but to deserialize an object out of a stream you need to call ReadObject method of DataContractJsonSerializer instance.

public static object Deserialize(Stream streamObject, Type serializedObjectType)
{
    if (serializedObjectType == null || streamObject == null)
        return null;
 
    DataContractJsonSerializer ser = new DataContractJsonSerializer(serializedObjectType);
    return ser.ReadObject(streamObject);
}

Testing

As for all previous serialization tutorials I have created a code to test both serialization and deserialization.

public static void Test()
{
    // serialization
    MemoryStream ms = new MemoryStream();
    DataContractJSONSerializationHelper.Serialize(ms, new SampleData());
 
    ms.Position = 0;
    // deserialization
    var sampleData = DataContractJSONSerializationHelper.Deserialize(ms, typeof(SampleData));
 
    ms.Close();
}

I like the size of generated serialized data comparing to other serialization approaches. Probably I am going to try that on more complex objects, but current result should look like that:

{"ContentText":"some text","SomeItems":[1,2,3]}



Topics:

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}