Python JSON
JSON (JavaScript Object Notation) is a lightweight data interchange format that is easy for people to read and write. The JSON data format is actually the dictionary format in Python, which can contain arrays enclosed in square brackets, which is the list in Python.
In Python, there are modules--jason and pickle that specialize in the Jason format.
The Jason module offers four methods: dumps, dump, loads, load
The Pickle module also offers four functions: dumps, dump, loads, load
First, dumps and dump
Dumps and dump serialization methods
Dumps only completed serialization of STR
Dump must pass a file descriptor to save the serialized str in the file
File operation:
Second, loads and load
Loads and load deserialization methods
Loads only the deserialization is complete.
Load only accepts file descriptors, finishes reading files and deserializes
Third, Jason and pickle modules
Both the Jason module and the Pickle module have dumps, dump, loads, and load four methods, and use the same
The difference is that the JSON module is serialized out as a common format, and other programming languages are known as ordinary strings
and the Pickle module serialized out only Python can be recognized, other programming languages do not know, the performance of garbled
However, Pickle can serialize functions, but other files want to use the function, where the definition of the file needs to be defined (definitions and parameters must be the same, content can be different)
Relationship of four Python objects to JSON objects
Five summary
1. JSON serialization method
Dumps: No file operation dump: Serialization + Write file
2, Jason deserialization method:
Loads: No file operation load: Read file + deserialization
3, JSON module serialization of data more general
The Pickle module serializes data only Python is available, but is powerful and can serialize functions.
4. The JSON module can serialize and deserialize data types as shown in the table above
5. Format write file using indent=4
Cpckle
The Python standard library provides pickle and cpickle modules. Cpickle is encoded in C and is higher in efficiency than pickle , but the type defined in the Cpickle module cannot be inherited (most of the time, we do not need to inherit from these types, we recommend using Cpickle). The serialization/deserialization rules for cpickle and Pickle are the same, using pickle to serialize an object that can be deserialized using Cpickle . At the same time, these two modules become more "smart" when dealing with self-referencing types, and it does not have unrestricted recursive serialization of self-referencing objects, which are serialized only once for multiple references to the same object.
Python3 Jason, Pickle and Cpickle