DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
The Latest "Software Integration: The Intersection of APIs, Microservices, and Cloud-Based Systems" Trend Report
Get the report

Windows Phone 7 Serialization: Binary Serialization

Jevgeni Tšaikin user avatar by
Jevgeni Tšaikin
·
Nov. 13, 11 · Interview
Like (0)
Save
Tweet
Share
8.04K Views

Join the DZone community and get the full member experience.

Join For Free

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

  • BinaryReader
  • BinaryWriter
  • WP7 Serialization Comparison (by Kevin Marshall)


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/
Serialization Windows Phone

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • What Is Advertised Kafka Address?
  • Orchestration Pattern: Managing Distributed Transactions
  • Implementing PEG in Java
  • A Gentle Introduction to Kubernetes

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: