Serialization in C#
Serialization is the process of converting an object into stream of bytes in order to persist it to memory, a database or a file. Its main purpose is to save the state of an object for the later uses when needed. The reverse process is call deserialization.
Making an Object Serializable
To serialize an object, we need an object to be serialized, a stream to contain serialized object and a Formatter. For Serialization we are going to look at the System.Runtime.Serialization
namespace. The ISerializable interface allows us to make any class Serializable.
We can serialize our own class if we mark them with [Serializable] attribute. This serializes all member of class, except those marked as [NonSerializable].
Types of Serialization
- Binary Serialization
- SOAP Serialization
- XML Serialization
Binary serialization is a mechanism which writes the data to the output stream such that it can be used to re-construct the object automatically. The term binary in its name implies that the necessary information that is required to create an exact binary copy of the object is saved onto the storage media. In Binary serialization the entire object state is saved while in XML serialization only some of the object data is saved.For binary serialization we need to import System.Runtime.Serialization.Formatters.Binary namespace. Following code serializes .NET string object by using BinaryFormatter class.
For communicating between applications having heterogeneous architecture SOAP protocol is ideal. In order to use SOAP serialization in .Net, we have to add a reference System.Runtime.Serialization.Formatters.Soap in application. The main advantage of SOAP serialization is its portability. The SoapFormatter serializes the object into SOAP messages or parses the SOAP messages and reconstruct the serialized object. Following code serializes .NET string object by using SoapFormatter class.
According to MSDN, "XML serialization converts (serializes) the public fields and properties of an object or the parameters and returns values of methods, into an XML stream that conforms to a specific XML Schema definition language (XSD) document. XML serialization results in strongly typed classes with public properties and fields that are converted to a serial format (in this case, XML) for storage or transport. Because XML is an open standard, the XML stream can be processed by any application, as needed, regardless of platform."
In order to use XML serialization, we have to add a reference System.XML. Serialization. The basic that we need to use for XML serialization is XmlSerializer. Following code serializes .NET string object by using XmlSerializer class.
What is Formatters?
A Formatter is used to determine the serialization format for object. They are object that are used to serialize the the object into appropriate format before they are transmitted over the network. They exposes the IFormatter interface. There are two formatter classes provided within .NET, the BinaryFormatter
and the SoapFormatter. Both these classes extend the
Use of Serialization
Serialization allows the developer to save the state of an object and recreate it as needed, providing storage of objects as well as data exchange. Through serialization, a developer can perform actions like sending the object to a remote application by means of a Web Service, passing an object from one domain to another, passing an object through a firewall as an XML string, or maintaining security or user-specific information across applications.