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

Windows Phone 7 Serialization: Binary Serialization

DZone's Guide to

Windows Phone 7 Serialization: Binary Serialization

· Mobile Zone
Free Resource

Download this comprehensive Mobile Testing Reference Guide to help prioritize which mobile devices and OSs to test against, brought to you in partnership with Sauce Labs.

Many of you probably noticed that Windows Phone 7 does not support standard Binary Serialization (using BinaryFormatter from System.Runtime.Serialization namespace), like full version of .NET Framework does, so I have decided to create my own custom solution for Binary serialization and deserialization using BinaryWriter and BinaryReader classes. This post covers my solution partly.

Additional Information


Adding a reference

Probably this step could be optional if you decide to modify my code, but currently my approach to Binary serialization requires System.Runtime.Serialization namespace to be added to project for creating a SampleData class described bellow. To add a reference 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 binary serialization

 

Creating a sample class

I am going to use almost the same class I’ve created for the DataContract Serialization tutorial. To allow properties to be serialized you have to apply DataMemberAttribute to them.

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 };
    }
}

Creating CustomBinarySerializer class

My goal was to make Binary serialization and deserialization look similar to DataContract and XML Serialization, meaning that the should be a constructor that accepts type as parameter and two methods WriteObject and ReadObject. For this reasons I have created CustomBinarySerializer class. Currently, this class handles only string and List<int> types (for my SampleData class described above). Feel free to add other handled types and, perhaps, modify this class by creating extention methods for those types like described in this article. Partly implemented version of CustomBinarySerializer looks like that (pay attention that this class serialize and deserialize only those properties that are marked with DataMemberAttribute):


public class CustomBinarySerializer
{
 
    private List<PropertyInfo> serializableProperties = new List<PropertyInfo>();
    private Type serializableObjectType;
 
    public CustomBinarySerializer(Type objectType)
    {
        serializableObjectType = objectType;
        serializableProperties = GetMarkedProperties(objectType);
    }
 
    private List<PropertyInfo> GetMarkedProperties(Type type)
    {
        return (from property in type.GetProperties()
                where property.GetCustomAttributes(true)
                .Where((x) => x is System.Runtime.Serialization.DataMemberAttribute).Count() > 0
                select property
                ).ToList();
    }
 
    #region Write
 
    public void WriteObject(Stream stream, object graph)
    {
        if (stream == null || graph == null)
            return;
 
        BinaryWriter bw = new BinaryWriter(stream);
 
        foreach (PropertyInfo pi in serializableProperties)
        {
            var value = pi.GetValue(graph, null);
 
            if (pi.PropertyType == typeof(string))
            {
                bw.Write(value as string ?? string.Empty);
            }
            else if (pi.PropertyType == typeof(List<int>))
            {
                WriteIntegerList(bw, value as List<int>);
            }
        }
    }
 
    private void WriteIntegerList(BinaryWriter bw, List<int> list)
    {
        if (list == null || !list.Any())
        {
            bw.Write(0);
        }
        else
        {
            bw.Write(list.Count);
            list.ForEach(x => bw.Write(x));
        }
    }
 
    #endregion Write
 
    #region Read
 
    public object ReadObject(Stream stream)
    {
        if (stream == null)
            return null;
 
        BinaryReader br = new BinaryReader(stream);
 
        object deserializedObject = Activator.CreateInstance(serializableObjectType);
 
        foreach (PropertyInfo pi in serializableProperties)
        {
            if (pi.PropertyType == typeof(string))
            {
                pi.SetValue(deserializedObject, br.ReadString(), null);
            }
            else if (pi.PropertyType == typeof(List<int>))
            {
                pi.SetValue(deserializedObject, ReadIntegerList(br), null);
            }
        }
        return deserializedObject;
    }
 
    private List<int> ReadIntegerList(BinaryReader br)
    {
        List<int> list = new List<int>();
        int count = br.ReadInt32();
 
        int index = count;
        while(index > 0){
            list.Add(br.ReadInt32());
            index--;
        }
        return list;
    }
 
    #endregion Read
 
}

Serialization

To serialize an object you need to create an instance of CustomBinarySerializer class (passing a type of serialized object as an input parameter) and then write serialized data to a stream object using WriteObject method, that accepts stream and object for serialization.

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

Deserialization

To deserialize your object you need to create an instance of CustomBinarySerializer class again (based on a type of serialized object) and then read serialized data using ReadObject method, that accepts an instance of Stream class as parameter.

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

Testing

You can use method bellow to test Binary serialization and deserialization. SampleData object after deserialization should be exactly the same as the one before the serialization.

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

Here is how serialized SampleData object looks like during the debug:

eugenedotnet windows phone 7 binary serialization results


Source: http://www.eugenedotnet.com/2010/12/windows-phone-7-serialization-binary-serialization/

Analysts agree that a mix of emulators/simulators and real devices are necessary to optimize your mobile app testing - learn more in this white paper, brought to you in partnership with Sauce Labs.

Topics:

Opinions expressed by DZone contributors are their own.

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

{{ parent.tldr }}

{{ parent.urlSource.name }}