10 May 2009

Serialization in .NET

This article is about serialization facility in .NET framework and particularly Object Serialization. So lets start from scratch and define first what is serialization ?

Serialization is the process of storing objects to the file or isolated file and later use it in the same application or same process or in another application on remote machine. It is just like you store data to a file and later use it in another application or same application. The difference here is you are storing objects not some random data and later recreate same object via deserialization.

This process is made simple via .NET frameworks serialization classes. The namespace we are looking here is System.Runtime.Serialization. Serialization save a lot of development time. There are three different kind of serialization provided by .NET framework.

1. Object Serialization
2. XML Serialization
3. Custom Serialization

In this article we will look at Object Serialization.

To serialize an object is very simple process.
1. Create a stream object that can hold the serialized object.
2. Create a BinaryFormatter object (namespace System.Runtime.Serialization.Formatters.Binary).
3. Call BinaryFormatter.Serialize method to serialize the object and output it to a stream.

Lets see it in example. I use C# as a programming language here.

// string object data that is to be serialized.
string data = "This data is to be serialized";

// Create a file stream to save data.
FileStream fs = new FileStream("mySerialiedData.dat",FileMode.Create);

//Create BinaryFormatter object
BinaryFormatter bf = new BinaryFormatter();

// use bf object to serialize data.
bf.Serialize(fs,data);

//close file stream
fs.Close();


That's it the data object is serialized and stored in file mySerializedData.dat.

You can deserialize it using BinaryFormatter's Deserialize method. You need to cast the object back to proper data type.

FileStream fs = new FileStream("mySerializedData.dat",FileMode.Open);
BinaryFormatter bf = new BinaryFormatter();
string data = (string) bf.Deserialize(fs); // cast it back to string object
fs.close();

You can serialize and deserialize any object using same technique.

You can make the custom class Serializable and Deserializable by using [Serializable] attribute.

For example:

[serializable]
class Product
{
public int id;
public string name;
public float price;
public double tax;
}

If tax is calculated using value of price than there is no need to serialize it to reduce the size of serialized object. To do this declare it as a [NonSerialized] attribute and implement IDeserializationCallback interface to calculate it when deserializing the object.


For example:

[serializable]
class Product : IDeSerializationCallback
{
public int id;
public string name;
public float price;
[NonSerialized]public double tax;

void IDeserializationCallback.OnDeserialization(object sender)
{
tax = price*0.1;
}
}

Now the value of tax will be available to the application that deserialize the object of Product class via OnDeserializing callback method.

This is how the serialization works in .NET environment. Isn't it really simple? So enjoy serializing. And by the way the closest resemblance to serialization is the teleportation in science fiction !

No comments:

Post a Comment