Serialization is the process of converting an object into a byte stream to save it in memory, database, or file for a long time. Its main purpose is to save the state of the object for use when needed later. The opposite process is called deserialization.
Serialize an object
To serialize an object, we need a serialized object, a (byte) stream that holds the serialized object and a formatter. Let's take a look at the namespace before serializing. The ISerializable interface allows us to make any class serializable.
If we write our own class identifier [Serializable] attribute, we can serialize these classes. Unless the class members are marked [NonSerializable], serialization serializes all members in the class.
Serialized type
•Binary (stream) serialization
•SOAP serialization
• XML serialization
Binary (stream) serialization:
Binary (stream) serialization is a mechanism for writing data to the output stream so that it can be used to automatically reconstruct the corresponding object. Binary, whose name implies that its necessary information is stored on the storage medium, and this 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 only part of the data is saved in XML serialization. In order to use serialization, we need to introduce a namespace. The following code uses the BinaryFormatter class to serialize objects of type string in .NET.
using System;
using ;
using ;
using ;
namespace SerializationTest
{
class Program
{
static void Main(string[] args)
{
//Serialization of String Object
string strobj = "test string for serialization";
FileStream stream = new FileStream("C:\\", , ,
);
BinaryFormatter formatter = new BinaryFormatter();
(stream, strobj);
();
//Deserialization of String Object
FileStream readstream = new FileStream("C:\\", , ,
);
string readdata = (string)(readstream);
();
(readdata);
();
}
}
}
SOAP serialization:
The SOAP protocol is an ideal choice for information interaction between heterogeneous applications. We need to add namespace in the application to use SOAP serialization in .Net. The main advantage of SOAP serialization is portability. SoapFormatter serializes objects into SOAP messages or parses SOAP messages and reconstructs the serialized object. The following code uses the SoapFormatter class to serialize objects of the string class in .Net.
using System;
using ;
using ;
using ;
namespace SerializationTest
{
class Program
{
static void Main(string[] args)
{
//Serialization of String Object
string strobj = "test string for serialization";
FileStream stream = new FileStream("C:\\", , ,
);
SoapFormatter formatter = new SoapFormatter();
(stream, strobj);
();
//Deserialization of String Object
FileStream readstream = new FileStream("C:\\", , ,
);
string readdata = (string)(readstream);
();
(readdata);
();
}
}
}
XML Serialization:
According to the description of MSDN, "XML serialization converts (serializes) the return value of an object or parameter into an XML stream that follows the XSD document standard. Because XML is an open standard, XML can be processed by any required program, regardless of the platform, XML serialization is used in strongly typed classes with public properties and fields, and its occurrence and fields are converted into a serialized format (here is XML) stored or transmitted."
We have to add references to use XML serialization. The basis for using XML serialization is XmlSerializer. The following code is to use the XmlSerializer class to serialize a string object in .Net.
using System;
using ;
using ;
namespace SerializationTest
{
class Program
{
static void Main(string[] args)
{
//Serialization of String Object
string strobj = "test string for serialization";
FileStream stream = new FileStream("C:\\", , ,
);
XmlSerializer xmlserializer = new XmlSerializer(typeof(string));
(stream, strobj);
();
//Deserialization of String Object
FileStream readstream = new FileStream("C:\\", , ,
);
string readdata = (string)(readstream);
();
(readdata);
();
}
}
}
What is a formatter?
A formatter is used to determine the sequence format of an object. Their purpose is to serialize an object into a suitable format before it is transmitted on the network. They provide IFormatter interface. There are two formatting classes in .NET: BinaryFormatter and SoapFormatter, both inherit the IFormatter interface.
Using serialization
Serialization allows developers to save the state of an object and refactor the object when needed, while also supporting object storage and data exchange well. Through serialization, developers can use Web Service to send objects to remote applications, transfer objects from one domain to another, transfer an object in XML format and pass through a firewall, or maintain security or user-specific information between applications, etc.