Saturday, June 14, 2008

Serialization

What is serialization?

Serialization is the process of saving the current state of any object into persistent storage (like file system), so that it can be retrieved later (Deserialize) and re create the same object.

For example, you have an object called student. You assign some values into the properties of this student object and serialze this object into a file. You can keep this file and de serialize any time later to re produce your student to the saved state.

What is serializable class ?

If a class can be serialized by using the builtin serialization features in .NET, that class is called serializable class. Most of the classes provided by .NET Framework is serializable.

Different types of Serialization

There are two broad categories of serialization in .NET

1. XML Serialization
2. Binary Serialization

XML Serialization serializes the object into an xml file. This file is human readable and can be shared with other applications.

Binary serialization is more efficient, but the serialized file is in binary format. It may not make any sense for a human being to open this file and understand what it contains. It is a stream of bytes.

Can I make my own classes serializable ?

Yes, it is very easy to make any class serializable. Simply add an attribute called 'Serializable' just above the class declaration and your class is serializable - means anybody can write few lines of C# code to save the state of the instance of your class into disk and retrieve (deserialize) later.

[Serializable]
public class MyClass
{
public string name;
public string adress;
}

How to serialize classes ?

The following samples demonstrate how to do XML Serialization and Binary Serialization using .NET classes.

We are serializing an 'ArrayList' object into disk and deserializing it again from the disk into another array list.

If you are not familiar with ArrayList - it is a collection class provided by .NET Framework, which can hold a list of any objects. The sample shown below uses an ArrayList which holds a list of strings.

XML Serialization

ArrayList itemsToSerialize = new ArrayList();
itemsToSerialize.Add ( "john" );
itemsToSerialize.Add ( "smith" );

XmlSerializer serializer = new XmlSerializer( typeof(ArrayList) );

TextWriter writer = new StreamWriter( @"MyApplicationData.xml" );
serializer.Serialize( writer, itemsToSerialize );
writer.Close();

In the first step, we are creating an array list and adding two strings into it. next step is to create an instance of XmlSerializer class, which is provided by .NET Framework to help serialize any objects. In the constructor of XmlSerializer class, we have to specify what type of object we want to serialize using this. Since we are going to serialize an ArrayList, we are specifying 'typeof(ArrayList)'.

Next, we are going to do the real thing - saving the state of the ArrayList into an XML file. We will create a TextWriter object and specify the xml file name to which we will save the ArrayList.

The following lines will save the arraylist 'itemsToSerialize' into the file 'MyApplicationData.xml':

TextWriter writer = new StreamWriter( @"MyApplicationData.xml" );
serializer.Serialize( writer, itemsToSerialize );
writer.Close();

Now, you can check the folder where your application executable is. A file with the name 'MyApplicationData.xml' will be created and you can open the file in any text editor to see the content.

Deserialization

We have serialized an arraylist into an xml file. This means, the state of the arraylist is saved into the xml file. So, we should be able to retrieve the same arraylist from this xml file. The following code will deserialize the arraylist from the xml file.

Stream stream = new FileStream( @"MyApplicationData.dat", System.IO.FileMode.Open );

IFormatter formatter = new BinaryFormatter();
ArrayList itemsDeserialized = (ArrayList)formatter.Deserialize( stream );

stream.Close();

Just few lines of code ! The above code will read the XML file 'MyApplicationData.dat' and restore the arraylist 'itemsDeserialized' - means the list of items we originally saved into the XML file will be loaded back to the array list. Now the arraylist 'itemsDeserialized' will contain two strings 'john' 7 'smith' whcih we saved as part of the serialization process.

Binary Serialization

Binary serialization is pretty much same as XML serialization, except that the data stored in a stream of bytes, not in XML format. See the sample code :

ArrayList itemsToSerialize = new ArrayList();
itemsToSerialize.Add ( "john" );
itemsToSerialize.Add ( "smith" );

Stream stream = new FileStream( @"MyApplicationData.dat", System.IO.FileMode.Create );
IFormatter formatter = new BinaryFormatter();
formatter.Serialize( stream, itemsToSerialize );

stream.Close();

The above code sample will serialize the arraylist 'itemsToSerialize' into the file 'MyApplicationData.dat'.

Deserializing the binary data

The objects serialized using binary formatter can be deserialized the same way.

Stream stream = new FileStream( @"MyApplicationData.dat", System.IO.FileMode.Open );

IFormatter formatter = new BinaryFormatter();
ArrayList itemsDeserialized = (ArrayList)formatter.Deserialize( stream );

stream.Close();

The above sample will read the file 'MyApplicationData.dat' and restore the arraylist 'itemsDeserialized'.

Where is Serialization used

Serialization has lot of uses in application development. One important purpose is to transmit data between application domains. Webservices and remoting uses serialization/deserialization technique to exchange data.

When you call a webservice and pass parameters, the parameters are serialized into XML data and send to the server. When the webservice return data, the result is serialized into XML and returned to the caller. This means, you can pass only serializable data in web service calls.

In Remoting, you can choose either Binary or SOAP(XML) formatting. Depending on what format you choose, data is serialized using xml or binary format and exchanged between the server and client.

In addition to the above, you can use serialization as a way to store the application data into files. For example, you want to save the last logged in user name, but you don't have a database. You can serialize the user object itself or serialize just the name of the user into disk. When you start the application next time, you can deserialize the user name from the serialized data. Many of the applications use this serialization mechanism to remember the user settings and application data.

Summary

The classes provided by .NET Framework make it very easy to persist the state of any serializable object and restore it to the same state. In the above samples, we have used an ArrayList object. Instead of ArrayList, we can serialize/de serialize any serializable objects including custom classes.

Sample Application

The attached sample application demonstrates both XMl serialization and Binary serialization. The sample project has a list box. You can add/remove items to the listbox. When you press the serialize button, the items in the listbox is copied to an ArrayList object and this array list is serialized.

When you press the de serialize button, the data is de serialzed into an Arraylist object and the listbox is loaded from this array list.

Note that the data serialized will persist even after you close the application. So, you can add few items into the list box in the sample project, serialize them into file and close the application. When you re start the application, you can restore the state of your list box you saved last.
A notable difference between Binary serialization and XML serialization is that Binary serialization preserves instance identity while XML serialization does not. In other words, in Binary serialization the entire object state is saved while in XML serialization only some of the object data is saved. Binary serialization can handle graphs with multiple references to the same object; XML serialization will turn each reference into a reference to a unique object.

No comments: