Tuesday, September 28, 2010

Object Serialization Using C# .net

Serialization is the Process of Converting an object into a linear Sequence of byte that can be Transferred or stored.
Desterilizing is the process of converting Serialized byte stream into a object.

Choosing serialization Format
There are 2 type of Serialization object .
  1. Binary Formatter -: this will located on System.Runtime.Serialization.Formatters.Binary Namespase. This is most efficient way to do that. But it can read only in the .net framework
  2. SoapFormatter -: this located on System.Runtime.Serialization.Formatters.Soap names pace. This is xml based formatter and most reliable way to serializable object that transmit across net work. This can be read non .net framework. This is successfully traversal across firewall than binary formatter. This is not support version compatibility of .net framework.
Object Serialization Step:

  • Create a stream object to hold serialized output.
  • Create a binary formatter object (its locate in System.Runtime.Serialaization.Formatter.Binary)
  • Call the BinaryFormatter or SoapFormatter Serialize Method to Serialized object and output result to the stream.

C# Code :


FileStream fs = new FileStream("SerializedData.Dat", FileMode.Create);

BinaryFormatter bf = new BinaryFormatter(); // binary formatter

//SoapFormatter bf = new SoapFormatter(); // for soap Formatter

bf.Serialize(fs, System.DateTime.Now);

fs.Close();


Object are DeSerialization Step:

  1. Create a stream object to read the serialized output
  2. Create a Binary Formatter object.
  3. Create a new object to store the Deserialized data
  4. Call the BinaryFormatter.Deserialize method to deserialize the object, and cast it to the correct type.
Ex :
FileStream fsRead = new FileStream("SerializedData.Dat", FileMode.Open); BinaryFormatter bfR = new BinaryFormatter();
//SoapFormatter bf = new SoapFormatter(); // for soap Formatter
DateTime data;
data =(DateTime) bfR.Deserialize(fsRead);
fsRead.Close();
Console.WriteLine(data);
Console.ReadKey();

Note :

The runtime proceeds through the deserialization process sequentially, starting at the beginning and working its way through to the end. The process gets complicated if an object in the serialized stream refers to another object. If an object references another object, the Formatter queries the ObjectManager to determine whether the referenced object has already been deserialized (a backward reference), or whether it has not yet been deserialized (a forward reference). If it is a backward reference, the Formatter immediately completes the reference. However, if it is a forward reference, the Formatter registers a fixup with the ObjectManager. A fixup is the process of finalizing an object reference after the referenced object has been deserialized. Once the referenced object is deserialized, ObjectManager completes the reference.

Create a Class that can be serialized

Class can be Serialized and De-serialized by adding Serializable attribute to the class. When your class is serializable run time its all the attribute will be serialized include private variable.

[Serializable]

class ShoppingCartItem

{

public int ProductID;

public string ProductName;

public decimal Quantity;

private decimal total;

public decimal price;

public ShoppingCartItem(int _productId, string _productName, decimal _Quantity, decimal _price)

{

ProductID = _productId;

ProductName = _productName;

Quantity = _Quantity;

price =_price;

total = Quantity * price;

}

}

Above class all the attribute will be serialized. In the total calculated in the dynamic. So that we no need to serialized total. Then we can reduced the storage, bandwith requirement when object are serialized. Use following way to non Serialized to attribute in the class.

[NonSerialized] public decimal total;

When deserialized total does not be initialized. So that we need to calculated total before deserialized. We can be used initialized non serialized object before deserialized use IDeserializationCallback interface and implement IDeserializationCallback.OnDeserialization method.

[Serializable]

class ShoppingCartItem : IDeserializationCallback

{

public int ProductID;

public string ProductName;

public decimal Quantity;

[NonSerialized] public decimal total;

public decimal price;

public ShoppingCartItem(int _productId, string _productName, decimal _Quantity, decimal _price)

{

ProductID = _productId;

ProductName = _productName;

Quantity = _Quantity;

price =_price;

total = Quantity * price;

}

void IDeserializationCallback.OnDeserialization(Object sender)

{

total = Quantity * price;

}

}