Python serialization and deserialization (JSON vs. Pickle)

Source: Internet
Author: User
Tags string format

In Python, serialization can be interpreted as converting the encoded format of an object in Python to a string in JSON (pickle) format, which can be interpreted as converting a string in JSON (pickle) format into an encoded format for an object in Python

To give a simple example, we write the Python program in the Vmvare environment, and then we need to go away temporarily, but we do not want to close this vmvare, this time we can choose to hang the machine, so that we come back to continue the operation, we can directly revert to the state before the walk, So the code we've written and the state of the vmvare is saved in the file, but we know that the file can only handle this type of data in the string, and during the operation, when we store the data in the file, We need to convert the Python object's encoding format to string format, which is serialization, and again, when we restore the code in Python, we need to convert the encoding format of the string to the encoding format we need, called deserialization. The JSON and pickle modules in Python can be used to implement this function.

Json

JSON provides four functions: Dumps,dump,loads,load (the first two are for serialization, and two for deserialization)

First, let's take a look at whether you can implement a file operation without JSON serialization

We create a file and store it in a dictionary to see if it's okay.

The result?

Obviously the error, tell us that the content written into the file must be a string format, not a dictionary.

We're trying to write with the JSON module.

The result creates a file, and the corresponding content is also written in the

We used the JSON dumps function to serialize, and then we use dump implementation, the code simplifies the

The result is the same.

Let's take a look at the deserialization

If we don't have to deserialize, see if we can find the corresponding amount of the name in the dictionary.

The result is not possible.

Let's import the JSON module.

We're going to use another deserialization feature of JSON. Load write this piece of code

The result is the same.

It seems that JSON is very powerful, but he can only be used to implement the list, dictionary, string, such as simple data type processing, for complex functions such as a function can not be processed, but he has his advantage, is to achieve with Java and other languages of the interaction, due to the limitations of JSON, Let's try pickle, he can serialize and deserialize all objects, but he can't interact with other languages.

Let's take a look at the situation where JSON encounters complex objects, and for this we define a Niusha function that adds his memory address to the dictionary

The results seem to be passable.

An error.

Let's try it again with pickle.

How did it turn out that way?

Note ha, here we use pickle.dumps default to become binary. So the error. We need to change it.

Change the way the file is written to "WB"

Let's take a look at the deserialization of pickle.

What's wrong with the results?

This is because the Niusha function we defined in the serialization is freed after it is used, so we cannot find this function in the deserialization process, here we are to prove that the pickle can serialize the function and so on complex object, in fact should not use this, if is to use this, We can only copy the code in the serialization.

So the results come out.

Note that here we are all just trying out pickle dump, with the load method, his dumps function and the loads function are the same as the JSON usage, which no longer describes

Python serialization and deserialization (JSON vs. Pickle)

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.