This is the 11th day of my participation in the August More Text Challenge. For details, see:August is more challenging

Retrospective review

There are currently three modules in Python that support serialization, which we studied earlier

  • Sequence and deserialize data using binary protocols – Pickle module
  • Convert data to JSON format and deserialize operations – JSON module
  • Sequence and deserialize data using dictionary – like keys – the shelve module

🗨️ Let’s review the process of serialization and dissequence

Network transmission serialization and deserialization:

Local disk serialization and deserialization:

🕹️ In this installment, we’ll learn that Python provides three built-in modules for serializing data

It is raining heavily again today ☔️, put on headphones 🎧, play music 🎼, start today’s learning journey

1. Overview of the MessagePack module

⏳MessagePack is a binary-based efficient object serialization class library for cross-language communication

The MessagePack module has the following features:

  • Similar to Json, structure objects are exchanged between many languages to optimize for useless characters like colons, parentheses, and so on
  • Supports Python, Ruby, Java, C/C++ and many other languages.
  • Compatible with JSON and pickle

👉 The structure of the messagePack module compares with that of the Json module

(1) The length of json is 27 bytes,

(2) The MessagePack module is 9 bytes less than json, a total of 18 bytes

(3) 9 bytes include: braces, quotes, colons, etc., to indicate that extra meaningless data is added.

(4) MessagePack module omits special symbols and defines various types with specific codes, as shown in the figure below:

👉 MessagePack Module serialization rules

Convert Pyhton to Messagepack

Python data types messagepack
array fixarray or array 16/32
str fixstr or str 8/16/32
int int 8/16/32/64 or uint 8/16/32/64
float float 32/64
True true
False false
Map fixmap or map 16/32

Messagepack is converted to Pyhton

messagepack Python data types
positive fixint, negative fixint, int 8/16/32/64 and uint 8/16/32/64 Integer
fixarray and array 16/32 array
fixstr and str 8/16/32 str
float 32/64 float
true True
false False
fixmap or map 16/32 Map

👉 Install the MSgPack module in the CMD command window

 pip install msgpack
Copy the code

2. Common methods of MessagePack module

methods role
msgpack.packb(obj) Serialized object
msgpack.dumps(obj) Serialized object
msgpack.unpackb(obj) Deserialization operation
msgpack.loads(obj) Deserialization operation
msgpack.pack(obj,file) The serialized object is saved to the file object
msgpack.dump(obj,file) The serialized object is saved to the file object
msgpack.unpack(obj,file) Deserialization objects are saved to file objects
msgpack.load(obj,file) Deserialization objects are saved to file objects

👑 Since messagepack is compatible with JSON and pickle, it also has dump, load, and other methods

3. Experiment

The Messagepack module of Python’s third-party library is optimized for unwanted character serialization compared to the JSON module.

Let’s use the messagepack module and json\ to do this:

import pickle,msgpack,json dic = {" name ":" juejin ", "age" : 12, "hpbby" : "writing", "language" : "Python", "isgirl" : True, "add" : None} print (" dic bytes: Js = json.dumps(dic) print(" json.dumps(dic) print(" -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- ") print (" JSON serialization: ", js) print (type (js), len (js), sep = "\ n") # pickling serialization pk = pickle. Dumps (dic) Print (" pickle processing -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- ") print (" pickle serialization: ",pk) print(type(pk),len(pk),sep = "\n") Print (" msgack processing -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- ") print (" msgpack serialization: ", MSG) print(type(MSG),len(MSG),sep = "\n") #msgpack ",msgpack.unpackb(msg))Copy the code

conclusion

In this installment, we looked at the messagePack module, a third-party module for serializing and deserializing data in Python

The MessagePack module provides packb(),unpackb() and other methods to sequence and deserialize Python data objects.

If you need to serialize the data space, you can use the Messagepack to serialize the data

The above is the content of this issue, welcome big guys to like comment ღ(´ · ᴗ · ‘) heart, see next time ~💖💗💓