Thursday, August 11, 2011

Serialization in .Net – Part-1 (What is Serialization)

Above figure shows that, the object is serialized to a stream, which carries not just the data, but information about the object's type, such as its version, culture, and assembly name. From that stream, it can be stored in a database, a file, or memory.

The advantage of serialization is the ability to transmit data across the network in a cross-platform-compatible format, as well as saving it in a persistent or non-persistent storage medium in a non-proprietary format. 

Serialization in .NET is provided by the System.Runtime.Serialization namespace. This namespace contains an interface called IFormatter which in turn contains the methods Serialize and De-serialize that can be used to save and load data to and from a stream.

In order to implement serialization in .NET, we basically require a stream and a formatter. While the stream acts as a container for the serialized object(s), the formatter is used to serialize these objects onto the stream.
Serialization is used by Remoting, Web Services SOAP for transmitting data between a server and a client.

Its main purpose is to save the state of an object in order to be able to recreate it when needed. The reverse process is called deserialization.

In other word you can say Deserialization is the process of converting stream of bytes into object.

The Serializable Attribute 

In order for a class to be serializable, it must have the attribute SerializableAttribute set and all its members must also be serializable, except if they are ignored with the attribute NonSerializedAttribute. However, the private and public members of a class are always serialized by default. The SerializationAttribute is only used for the binary serialization.

The SerializableAttribute is useful for situations where the object has to be transported to other application domains. It needs to be applied irrespective of whether the class implements the ISerializable interface. If this attribute is not set in that case, then when we try to serialize an object the CLR throws a SerializationException.

Advantages and Disadvantages of Serialization


  1. Modification of XML documents without using the DOM.

  2. Passing an object from one application to another.

  3. Passing an object from one domain to another.

  4. Passing an object through a firewall as an XML string


    The primary disadvantage of serialization can be attributed to the resource overhead (both the CPU and the IO devices) that is involved in serializing and de-serializing the data and the latency issues that are involved for transmitting the data over the network. Further, serialization is quite slow. Moreover, XML serialization is insecure, consumes a lot of space on the disk and it works on public members and public classes and not on the private or internal classes. Therefore, it compels the developer to allow the class to be accessed by the outside world.

    Types of Serialization:

    .NET provides classes through its System.Runtime.Serialization namespaces that can be used for serializing and de-serializing objects.

    Serialization can be divided into following types:
    ·         Binary Serialization: Binary serialization allows the serialization of an object into a binary stream and restoration from a binary stream into an object.
    ·         XML Serialization: XML serialization allows the public properties and fields of an object to be reduced to an XML document that describes the publicly visible state of the object.
    ·         SOAP Serialization: SOAP serialization is similar to XML serialization in that the objects being serialized are persisted as XML.
    ·         Custom Serialization: If the default serialization classes are insufficient due to the requirements, then this can be customized by implementing the ISerializable interface.