You are on page 1of 11

Object Serialization

Definition
Object Serialization is the process of
reducing an object instance into a format
that can be stored to disk or transported
over network.

Why Serializing Needed?
In the Object Oriented programming paradigm,
applications are modeled around object.
All objects have a state which may change during
lifetime of the object. This state is maintained in
memory. Later it is recreated to its current state as
requirement.
The object is persisted to disk using particular formatter
and can be sent across network to another machine
which then receives stream of data and deserializes the
object with its state unchanged.

.NET Serialization
.Net offers very efficient and simple ways for
serializing an object.
Serialization is often used with streams ,which in
turn are used for reducing and writing to files
and network locations.
.Net provides classes and Methods to serialize
and deserialize an object.
How .Net Serialization Work?
Use of Namespace:
System.Runtime.Serialization
System.IO
System.Runtime.Serialization.Formatters
System.Runtime.Serialization.Formatters.Binary
Two types of serialization
Binary serialization
XML serialization


Role of Formatter
Formatter is used to determine the serialization
format for objects.All formatters expose an
interface called the Iformatter interface.
BinaryFormatter:-It provides binary
encoding for compact serialization either for
storage or for socket-based network stream.It is
not used when data is meant to be passed
through firewall.

The .Net Framework also include the abstract formatter class
that can be used as a base class for custom formatters.This
class inherits from the IFormatter interface.
Implement Iserializable interface for binary serialization.
To serialize an object following are required:
1. The object that is to be serialized
2. A stream to hold the serialized object.
3. A formatter which is used to serialize the
object.
Note:-One must ensure that all base classes of a
class is serializable or else it will give
unexpected results.
Deserialization
Deserialization involves restoring an object back
to the state when it was serialized.
.Net uses Deserialize() method of the
Binaryformatter class.

Selective serialization
By default all members of a class can be
serialized including private members.To reduce
the amount of data to be serialized
one can selectively eliminate members that are
not going to reconstituted with the help of
attribute [NonSerialized]

Example
Using system;
Namespace serializatiion_1
{
[Serializable]
Public class test
{
Public string name;
Public int phonenumber;
[NonSerialized]
Public string address;
}
}
Differences
Binary Serialization
Stores all members both
public and private
Produces compact serialized
representation
objects with little overhead.
Makes interoperation
difficult,as object state is
stored in a proprietary
format.

XML serialization
Only stores public
members .
Produces larger serialized
representation of
objects,with more
overhead.
Interoperation is
easier,because it uses
open standards such as
xml and SOAP.

You might also like