Serialization is the process of converting an object into a byte stream to achieve its long-term retention in memory, database, or file. Its primary purpose is to preserve the state of the object for later use when needed. The opposite process is called deserialization.
Serialization of an Object
To serialize an object, we need a serialized object, a (byte) stream containing the serialized object, and a formatter. Before serializing, we'll look at the System.Runtime.Serialization namespace. The ISerializable interface allows us to make any class A serializable class.
If we write our own class identifier [Serializable] attribute, we can serialize these classes. Serialization will serialize all members of a class unless the member of the class is tagged with [nonserializable].
Types of serialization
Binary (Stream) serialization
SOAP Serialization
Serialization of XML
Binary (Stream) serialization:
Binary (Stream) serialization is a mechanism for writing data to an output stream so that it can be used to automatically re-form the corresponding object. Binary, whose name implies that its necessary information is stored on the storage medium, and these necessary information requires the creation of an exact binary copy of an object. In binary (stream) serialization, the state of the entire object is saved, while the XML serialization is only part of the data being saved. In order to use serialization, we need to introduce System.Runtime.Serialization.Formatters.Binary namespaces. The following code uses the BinaryFormatter class to serialize an object of type string in. Net.
Using system;using system.io;using system.runtime.serialization;using System.Runtime.Serialization.Formatters.Binary; Namespace serializationtest{class Program {static void Main (string[] args) {//serializat Ion of String Object string strobj = "Test String for serialization"; FileStream stream = new FileStream ("C:\\strobj.txt", FileMode.Create, FileAccess.Write, Fileshare.none); BinaryFormatter formatter = new BinaryFormatter (); Formatter. Serialize (stream, strobj); Stream. Close (); Deserialization of String Object FileStream readstream = new FileStream ("C:\\strobj.txt", FileMode.Open, Fil Eaccess.read, FileShare.Read); String readdata = (string) formatter. Deserialize (Readstream); Readstream. Close (); Console.WriteLine (ReadData); Console.ReadLine (); } }}
SOAP Serialization:
The SOAP protocol is an ideal choice for information interaction between heterogeneous applications. We need to add the System.Runtime.Serialization.Formatters.Soap namespace in the application to use SOAP serialization in. Net. The main advantage of SOAP serialization is portability. SoapFormatter serializes an object into a SOAP message or parses a SOAP message and reconstructs the serialized object. The following code is in the. NET uses the SoapFormatter class to serialize the object of the string class.
using system;using system.io;using system.runtime.serialization;using System.Runtime.Serialization.Formatters.Soap; Namespace Serializationtest {class Program {static void Main (string[] args) {//serializa tion of string Object string strobj = "Test String for serialization"; FileStream stream = new FileStream ("C:\\strobj.txt", FileMode.Create, FileAccess.Write, Fileshare.none); SoapFormatter formatter = new SoapFormatter (); Formatter. Serialize (stream, strobj); Stream. Close (); Deserialization of String Object FileStream readstream = new FileStream ("C:\\strobj.txt", FileMode.Open, Fil Eaccess.read, FileShare.Read); String readdata = (string) formatter. Deserialize (Readstream); Readstream. Close (); Console.WriteLine (ReadData); Console.ReadLine (); } }}
XML Serialization:
According to the MSDN description, "XML serialization transforms (serializes) the exposed fields and properties of an object or parameter and the return value of a method into an XML stream that follows the XSD document standard. Because XML is an open standard, XML can be processed by any desired program, regardless of the platform, so XML serialization is used in strongly typed classes with exposed properties and fields, where these occurrences and fields are converted into serialized formats (where XML) are stored or transmitted. ”
We must add System.XML.Serialization references to use XML serialization. The basis for using XML serialization is XmlSerializer. The following code is in the. NET uses the XmlSerializer class to serialize a string object.
using system;using system.io;using System.Xml.Serialization; Namespace serializationtest{class Program {static void Main (string[] args) {//serializat Ion of String Object string strobj = "Test String for serialization"; FileStream stream = new FileStream ("C:\\strobj.txt", FileMode.Create, FileAccess.Write, Fileshare.none); XmlSerializer XmlSerializer = new XmlSerializer (typeof (String)); XmlSerializer. Serialize (stream, strobj); Stream. Close (); Deserialization of String Object FileStream readstream = new FileStream ("C:\\strobj.txt", FileMode.Open, Fil Eaccess.read, FileShare.Read); String readdata = (string) xmlserializer. Deserialize (Readstream); Readstream. Close (); Console.WriteLine (ReadData); Console.ReadLine (); } }}
What is a formatter?
A formatter is used to determine the sequence format of an object. They are designed to serialize an object to the appropriate format before it is transmitted over the network. They provide iformatter interfaces. Two formatting classes are available in. NET: BinaryFormatter and SoapFormatter, both of which inherit the IFormatter interface.
Using serialization
Serialization allows the developer to save the state of an object and refactor the object when needed, and to support object storage and data exchange well. With serialization, developers can use Web service to send objects to a remote application, transfer objects from one domain to another, transfer an object in XML format, pass a firewall, or maintain security or user-specific information between applications, and so on.