Detailed introduction to serialization and deserialization

零下一度
Release: 2017-07-19 16:45:20
Original
1748 people have browsed it

1. The concept of serialization and deserialization

The process of converting an object into a sequence of bytes is called serialization of the object.
The process of restoring a byte sequence into an object is called deserialization of the object.
The serialization of objects has two main uses:
1) Permanently save the byte sequence of the object to the hard disk, usually in a file;
2) Transmit the object's byte sequence over the network section sequence.

In many applications, certain objects need to be serialized so that they can leave the memory space and move to the physical hard disk for long-term storage. For example, the most common one is the Session object in the Web server. When 100,000 users access it concurrently, there may be 100,000 Session objects, which may be too much for the memory, so the Web container will serialize some sessions to the hard disk first, and so on. When you want to use it, restore the object saved in the hard disk to the memory.

When two processes communicate remotely, they can send various types of data to each other. No matter what type of data it is, it is transmitted over the network in the form of a binary sequence. The sender needs to convert this Java object into a byte sequence before it can be transmitted over the network; the receiver needs to restore the byte sequence into a Java object.


The process of json serialization is to write it into a file , let another programming language make the call:

import json

 info = {"alex":"sb","test":"hard"}

 withopen("file","w")as f:
  f.write(json.dumps(info))

# The above code uses json to write the info dictionary information into a file. The file can only store information in string format or binary file information. It cannot store numbers and other information. The information placed in the file is String type, you must pay attention to this.

The process of json deserialization:

import json

 '''The beginning of deserialization is to extract the information fromdumpto realize the interaction of different programming languages'''

 withopen("file","r")as f:  data = json.loads(f.read())

print(data)
print(type(data))
print(data["alex"])

The above code will use json The information stored in the format is read out as follows:

 {'test': 'hard', 'alex': 'sb'}

 sb
The above code realizes reading the string information Asking about the function of the dictionary, in fact, serialization and deserialization are the processes of converting the original format into a string first, and then reading it out so that interaction can be achieved.

We can also use other methods for serialization and deserialization. We know that there is a function eval() that can convert string information into the original style, as follows:

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

 with open ("test.text","w")as f:
  f.write(str(info))   #USEwirte ()Only information in string format can be written to the file, and other types of information cannot be written

 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:

 [11, 22, 33, 65, 33]
In the above process, we use the eval() function that comes with python. The process of serialization and deserialization is implemented, but since serialization and deserialization are implemented in the same program, it is uncertain whether there is eval() in other programs, but json supports all programming languages. Therefore, json is now generally used to realize information interaction between different programming languages.

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

Dump serialization:

import json
info = {
"alex":"sb","test":"hard"}

 withopen("file","w")as f:  json .dump(info,f)

#  load () Deserialization:

import json

 '''The beginning of deserialization is to extract the information indumpto achieve different programming Language interaction'''

## withopen("file","r")as f:data = json.load(f)

print(data)
print(type(data))
print(data["alex" ])
# 

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

Implement data exchange between different programs.

Data exchange between different programs, or converting string information into its original form;

eval The ()function is also very powerful and can convert information in string form into original information, as follows:

 >>> dic = "{'alex':'sb','try':'workhard'}"

 >>> data = eval(dic) >>> data
 {'try ': 'workhard', 'alex': 'sb'}

The program only dumps once and loads once, and cannot dump multiple times. Dumps is implemented in several files;

The above is the detailed content of Detailed introduction to serialization and deserialization. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!