Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.


The introduction

The process of converting an object’s state information into a form that can be stored or transmitted is called serialization

A similar conversion from serialized data to corresponding objects is called deserialization

This article introduces Python’s two modules for serializing and de-ordering objects

  • pickle
  • json


pickle

In [18] :import pickle

# serialization
In [19]: num = 66

In [20]: s = 'python'

In [21]: pi = 3.14

In [22]: li = [1.2.3]

In [27]: b_num = pickle.dumps(num)

In [28]: b_s = pickle.dumps(s)

In [29]: b_pi = pickle.dumps(pi)

In [30]: b_li = pickle.dumps(li)

In [31]: b_num
Out[31] :b'\x80\x03KB.'

In [32]: b_s
Out[32] :b'\x80\x03X\x06\x00\x00\x00pythonq\x00.'

In [33]: b_pi
Out[33] :b'\x80\x03G@\t\x1e\xb8Q\xeb\x85\x1f.'

In [34]: b_li
Out[34] :b'\x80\x03]q\x00(K\x01K\x02K\x03e.'
    
In [35] :type(b_li)
Out[35] :bytes
    
# deserialize
In [47]: pickle.loads(b_num)
Out[47] :66

In [48]: pickle.loads(b_s)
Out[48] :'python'

In [49]: pickle.loads(b_pi)
Out[49] :3.14

In [50]: li = pickle.loads(b_li)

In [51]: li
Out[51] : [1.2.3]

In [52] :type(li)
Out[52] :list    
Copy the code


Custom objects can also be serialized

class User:
    
    def __init__(self, name, sex) :
        self.name = name
        self.sex = sex
        
In [38]: user = User('hui'.'male')

In [39]: b_user = pickle.dumps(user)

In [40]: b_user
Out[40] :b'\x80\x03c__main__\nUser\nq\x00)\x81q\x01}q\x02(X\x04\x00\x00\x00nameq\x03X\x03\x00\x00\x00huiq\x04X\x03\x00\x00\x00sex q\x05X\x03\x00\x00\x00\xe7\x94\xb7q\x06ub.'

In [41] :type(b_user)
Out[41] :bytes

In [42]: user = pickle.loads(b_user)

In [43] :type(user)
Out[43]: __main__.User

In [44]: user.name
Out[44] :'hui'

In [45]: user.sex
Out[45] :'male'        
Copy the code


Note:pickleSerialized data is bytes


Pickle can also serialize an object to a file and then reverse order it back from the file.

import pickle

class User:
    
    def __init__(self, name, sex) :
        self.name = name
        self.sex = sex
        
user = User('ithui'.'male')
f = open('user.txt', mode='wb')
pickle.dump(user, f)
f.close()

Copy the code


Reverse order an object from a file

In [3]: f = open('user.txt'.'rb')... : user = pickle.load(f) ... : f.close() ... : In [4]: user
Out[4]: <__main__.User at 0x16c58ebef08>

In [5]: user.name
Out[5] :'ithui'

In [6]: user.sex
Out[6] :'male'
Copy the code


The pickle module serializes objects, but it only works in Python, making it difficult to exchange data. For example, if you send data to the front-end, JS can’t convert the data to what it wants.


json

If we want to pass objects between different programming languages, we must serialize the object to a standard format, such as JSON, because JSON represents a string that can be read by all languages and easily stored to disk or transferred over a network for data exchange.


Json strings represent objects that are js objects. Json and Python’s built-in data types correspond as follows:

JSON type Python types
{} dict
[] list
“string” Unicode ‘ ‘STR’ or u ‘
3.14 Int or float
true / false True / False
null None


In [7] :import json

In [8]: info_dict = { ... :'name': 'hui'. :'age': 22. :'is_admin': True. :'hobbies': ['Play chess'.'Write code'],
   ...:     'other': None. : } In [9]: info_json = json.dumps(info_dict)

In [10]: info_json
Out[10] :'{ "name": "hui", "age": 22, "is_admin": true, "hobbies": ["\\u4e0b\\u8c61\\u68cb", "\\u5199\\u4ee3\\u7801"], "other": null }'
   
# corresponding deserialization
In [16]: info_d = json.loads(info_json)

In [17]: info_d
Out[17] : {'name': 'hui'.'age': 22.'is_admin': True.'hobbies': ['Play chess'.'Write code'].'other': None}

In [18] :type(info_d)
Out[18] :dict        
Copy the code


See if a custom class object can be json serialized

In [21] :import json

In [22] :class User:. :... :def __init__(self, name, sex) :. : self.name = name ... : self.sex = sex ... : In [23]: user = User('ithui'.'male')

In [24]: json.dumps(user)
    
TypeError: Object of type User is not JSON serializable    
Copy the code

Error: User object cannot be json serialized. Is there a way to convert custom objects to JSON? There is.

The idea is to turn the User object into a json serializable object, such as dict, and then pass the serializable object to the JSON module.


In [28] :def user2dict(obj) :. :return {'name': obj.name, 'sex': obj.sex} ... :... : In [29]: user = User('ithui'.'male')

In [30]: user_dict = user2dict(user)

In [31]: user_dict
Out[31] : {'name': 'ithui'.'sex': 'male'}

In [32]: user_json = json.dumps(user_dict)

In [33]: user_json
Out[33] :'{"name": "ithui", "sex": "\\u7537"}'
Copy the code


We can also specify a converter for serialization. The optional default argument is to convert any object into a seriable JSON object. We just need to write a conversion function for User and pass it in:

In [28] :def user2dict(obj) :. :return {'name': obj.name, 'sex': obj.sex} ... :... : In [34]: user_json = json.dumps(user, default=user2dict)

In [35]: user_json
Out[35] :'{"name": "ithui", "sex": "\\u7537"}'
Copy the code


This allows you to convert custom class objects to JSON, but customizing different converters for different classes is repetitive and cumbersome, so the idea is to serialize each class using its __dict__ attribute, which is a dict object used to store instance variables. There are a few exceptions, such as classes that define __slots__

In [36]: user.__dict__
Out[36] : {'name': 'ithui'.'sex': 'male'}
    
In [41]: json.dumps(user.__dict__)
Out[41] :'{"name": "ithui", "sex": "\\u7537"}'
Copy the code


Note: If an attribute in an object is nested another cannot be directlyjsonSerialized object, used__dict__Again, the property does not serialize properly.


The tail language

✍ Code writes the world and makes life more interesting. ❤ ️

✍ thousands of rivers and mountains always love, ✍ go again. ❤ ️

✍ code word is not easy, but also hope you heroes support. ❤ ️