What is serialization and deserialization?

Source: Internet
Author: User
Tags object serialization

What is serialization and deserialization ???

Serialization and deserialization are often heard. In fact, a simple explanation is that serialization stores an object in a file or database field, deserialization is to convert the file to the original object at an appropriate time.

When two processes perform remote communication, they can send different types of data to each other. Regardless of the type of data, it is transmitted on the network in the form of binary sequence. The sender needs to convert the object to a byte sequence before it can be transmitted over the network. The receiver needs to restore the byte sequence to an object. The process of converting an object to a byte sequence is called object serialization. The process of restoring a byte sequence to an object is called object deserialization.

Object serialization has two main purposes:1) store the object's byte sequence permanently on the hard disk, usually in a file; we often need to save the object's field value to the disk, and retrieve the data later. Although this can be done without serialization, this method is often cumbersome and error-prone, and will become more and more complex when you need to trace the object hierarchy. Imagine writing a large business application containing a large number of objects. programmers have to write code for each object, this allows you to save fields and attributes to the disk and restore these fields and attributes from the disk. Serialization provides a quick way to easily achieve this goal. The Common Language Runtime (CLR) manages the distribution of objects in the memory. the. NET Framework uses reflection to provide an automatic serialization mechanism. After the object is serialized, the class name, assembly, and all data members of the class instance are written to the storage media. Objects usually use member variables to store references to other instances. After the class is serialized, the serialization engine tracks all serialized reference objects to ensure that the same object is not serialized multiple times .. The serialization architecture provided by the. NET Framework can automatically and correctly process object charts and circular references. The only requirement for object charts is that all objects referenced by objects being serialized must be marked as serializable (see Basic serialization ). Otherwise, an exception occurs when the serialization program attempts to serialize unlabeled objects. When deserializing A serialized class, the class is re-created and the values of all data members are automatically restored.
2) transmits the object's byte sequence over the network. The object is valid only in the application domain of the created object. Unless the object is derived from marshalbyrefobject or marked as serializable, any attempt to pass the object as a parameter or return it as a result will fail. If the object is marked as serializable, the object will be automatically serialized, transmitted from one application domain to another application domain, and then deserialized, in this way, an exact copy of the object is generated in the second application domain. This process is usually called value-based mail. If the object is derived from marshalbyrefobject, the object reference, not the object itself, is passed from one application domain to another application domain. You can also mark the object derived from marshalbyrefobject as serializable. When this object is used remotely, the formatting program that is responsible for serialization and is pre-configured as surrogateselector controls the serialization process and replaces all objects derived from externalbyrefobject with a proxy. If it is not pre-configured as surrogateselector, the serialization architecture will follow the following standard serialization rules.

The main functions of serialization and deserialization are as follows:1. Read the information of the last saved object at the next startup of the process.

2. Transmit data between different AppDomains or processes

3. Data Transmission in Distributed Application Systems

.............

Common serialization methods:

1. binaryformatter

2. soapformatter

3. XML serialization

Usage:

The usage of binaryformatter is roughly as follows:

// Binaryformatter serializes the object to the file list <string> inputlist = new list <string> () {"Fashion Trends", "http://gz168168.taobao.com ", "Welcome to the technical-focused program yuan to see my online shop"}; using (filestream fswriter = new filestream (@ "gz168168.tmp", filemode. create, fileaccess. write) {binaryformatter BF = new binaryformatter (); // serialize BF. serialize (fswriter, inputlist );}
// Binaryformatter deserializes the data in the file to list <string> outputlist = new list <string> (); Using (filestream fsreader = new filestream (@ "gz168168.tmp", filemode. open, fileaccess. read) {binaryformatter BF = new binaryformatter (); // deserialization outputlist = (list <string>) BF. deserialize (fsreader );}

The usage of XML serialization is roughly as follows:

// Serialize XML to test. in the XML file list <string> inputlist = new list <string> () {"Fashion Trends", "http://gz168168.taobao.com"}; using (filestream fswriter = new filestream (@ "test. XML ", filemode. create, fileaccess. write) {xmlserializer xs = new xmlserializer (typeof (list <string>); Xs. serialize (fswriter, inputlist );}
// From test. list <string> outputlist = new list <string> (); Using (filestream fsreader = new filestream (@ "test. XML ", filemode. open, fileaccess. read) {xmlserializer xs = new xmlserializer (typeof (list <string>); outputlist = Xs. deserialize (fsreader) as list <string> ;}

Summary:

The two functions are as follows:

Serialization:

1. Get a storage object type

2. Create a file writing stream

3. Define the type to be serialized

4. Call the serialization Method

Deserialization:

1. define the type of a loading object

2. Create a read file stream

3. Define the type to be deserialized

4. Call the deserialization Method

The binaryformatter class is serialized and deserialized and written to a file in a thumbnail binary format. The speed is relatively fast, and the written files are saved in binary format with a certain confidentiality effect. All other members marked as nonserialized can be serialized.

In XML serialization, only public fields and read/write attributes can be saved, and private fields cannot be serialized.

Advantages of binary serialization:

1. All class members (including read-only members) can be serialized;

2. excellent performance.

Advantages of XML serialization:

1. Good interoperability;

2. Strict binary dependency is not required;

3. High readability

Soapformatter has been eliminated by Microsoft and Cannot serialize generics. I will not talk about it here.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.