Was this helpful?
Serialization is the technique used by the .NET runtime. The runtime serialization functionality resides in the System.Runtime.Serialization namespace. It involves converting all of the object's data into a byte stream. It allows you to treat all of the object's data as a single value. The entire state of the object's data is represented by a series of bytes.
Your application can write the serialized data to a target stream. For instance, you can persist objects in some kind of storage. It can be a disk file or a database. It can even be written to a MemoryStream.
The serialization stream includes the following:
The application can then deserialize the stream for later use. Deserialization converts the byte stream back into individual data members. The object is recreated from its serialized representation.
Uses of Serialization
Serialization is typically used in the following scenarios:
Shallow and Deep Serialization
.NET framework offers two basic types of serialization.
a. Shallow serialization: Only the public fields and read-write properties of the object are serialized into a byte stream.
b. Deep serialization: The entire state of the object is serialized including the values of its private fields. The entire object graph is serialized as well i.e. if the object holds a reference to other objects, even those are serialized. For e.g. Order containing a reference to Customer and various Items.
IFormatter interface and its implementations
The IFormatter interface resides in the System.Runtime.Serialization namespace. It provides the Serialize() and Deserialize() methods that encapsulate the serialization and deserialization functionality respectively.
A formatter specifies the serialization format for the objects. All formatters implement the IFormatter interface. The .NET framework provides certain built-in formatters. For example, SoapFormatter and BinaryFormatter are the concrete formatter classes which can be used to serialize and derserialize the objects. They differ in the way they format the byte stream.
To achieve binary serialization, you need to use the BinaryFormatter which resides in the System.Runtime.Serialization.Formatters.Binary namespace. It uses the deep serialization technique and generates a data stream in binary format. It is useful either for storage or for sending data across the network. The stream produced is more compact when compared to the other methods.
To achieve SOAP serialization, you need to use the SoapFormatter which resides in the System.Runtime.Serialization.Formatters.Soap namespace. It also uses the deep serialization technique. It generates a data stream in XML format based on the SOAP-standard i.e. SOAP-protocol specifics are also incorporated during the serialization process.
XML is designed to exchange data across heterogeneous systems. SOAP messages piggyback on HTTP and are able to penetrate Internet firewalls. Therefore this serialization format comes handy when you need to send the data through firewalls or heterogeneous systems. Note that BinaryFormatter is not appropriate in such situations.
For Part 2, click this link: Serialization in .NET - Part 2
|posted||Apr 08, 2016|
|active||Apr 08, 2016|
|•||Serialization in .NET - Part 3|
|•||Serialization in .NET - Part 2|
|•||Reading appsettings.json in a .NET Core console application|
|•||Going forward: Should I rewrite my HtmlHelpers into TagHelpers?|
|•||How do I get the response from an asynchronous Ajax call?|
|•||When to use 'ref' and 'out' keywords - specifically with strings?|