A detailed description of serialization and deserialization

Source: Internet
Author: User
Tags italic font

I. The concept of serialization and deserialization

The process of converting an object to a sequence of bytes is called serialization of an object.
The process of reverting a sequence of bytes to an object is called deserialization of the object.
There are two main uses for serialization of objects:
1) The object's byte sequence is permanently saved to the hard disk, usually stored in a file;
2) A sequence of bytes that transmits an object over the network.

In many applications, some objects need to be serialized to leave the memory space and stay on the physical hard disk for long-term storage. For example, the most common is the Web server session object, when there are 100,000 users concurrent access, there may be 100,000 session objects, memory may be unbearable, so the Web container will be some seesion first serialized to the hard disk, and so on, Restore the objects that were saved to the hard disk in memory.

When two processes are communicating remotely, each other can send various types of data. Regardless of the type of data, it is transmitted over the network in the form of a binary sequence. The sender needs to convert the Java object to a sequence of bytes to be transmitted over the network, and the receiver needs to revert the byte sequence back to the Java object.


  

The process of JSON serialization is to write to a file and have another programming language call:

  Import JSON

info = {"alex":"SB","test":"Hard"}

with open ("file","W") as F:
F.write (Json.dumps (info))

It is important to note that the above code uses JSON to write info dictionary information to a file that only stores information in a string format, or binary file information that cannot hold numbers, and information that is placed in a file is of type string.

The process of JSON deserialization:

   Import JSON

''' deserialization starts with dump The information to be extracted in order to achieve the interaction of different programming languages ""

with open ( Span style= "color: #008080; Font-weight:bold "" "File", as f:
data = Json.loads (F.read ())
print (data)
print ( type (data))
print ( Data[   

The above code reads the information stored in JSON format as follows:

{' Test ': ' Hard ', ' Alex ': ' SB '}
<class ' Dict ' >
Sb
The above code implements the function of reading the string information to the dictionary, in fact, the serialization and deserialization is to convert the original format into a string, and then read out the process, so that the interaction can be achieved.

We can also use other methods for serialization and deserialization, and we know that there is a function eval () that enables the conversion of string information to the original style, as follows:

info = [11,22,33,65,33]

With open ("Test.text","W")As F:
F.write (str (info)) # using wirte () information in string format can only be written to a file and cannot be written to other types of information

with open (" Test.text ", " R ") as f_obj:
data = F_obj.read ()

data = eval (data)
print (type (data))
Print (data)   

The program runs as follows:

<class ' list ' >
[11, 22, 33, 65, 33]
In the above process, we have also implemented the process of serialization and deserialization using the Python's own eval () function, but since serialization and deserialization are implemented in the same program, there is no eval () in other programs, but JSON supports all programming languages, Therefore, it is common to use JSON to implement information interaction between different programming languages.

Dump and load also implement the above dumps and loads functions, but not the same way of implementation, the syntax is slightly different, as follows:

Dump Serialization:

  Import JSON

info = {"alex":"SB","test":"Hard"}

with open ("file","W") as F:
Json.dump (INFO,F)

Load () Deserialization:

   Import JSON

''' deserialization starts with dump The information to be extracted in order to achieve the interaction of different programming languages ""

with open ( Span style= "color: #008080; Font-weight:bold "" "File", as f:
data = Json.load (f)
print (data)
print (type (data))
print (data[< Span style= "color: #008080; Font-weight:bold ">" Alex "])   

The above program implements serialization and deserialization functions, dump (information, file path), load (file path), from which file to read information.

The exchange of data between different programs is implemented.

The exchange of data between different programs, or the conversion of the information of a string into its original form;

  The eval () function is also powerful enough to convert the information in the form of a string into the original information, as follows:

>>> dic = "{' Alex ': ' SB ', ' Try ': ' Workhard '}"
>>> data = eval (DIC)
>>> data
{' Try ': ' Workhard ', ' Alex ': ' SB '}

The program only dumps once, load once, cannot dump multiple times. Dumps several file implementations;

Related Article

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.