python 序列化

import json
from io import BytesIO
from rest_framework.parsers import JSONParser
from rest_framework.renderers import JSONRenderer


dic_data = {‘id‘: 2, ‘name‘: ‘Metis Finance‘}
print(dic_data)
print(type(dic_data))

# dict - > str
json_data = json.dumps(dic_data)
print(json_data)
print(type(json_data))

# str - > byte
b_data = bytes(json_data,encoding=‘utf8‘)
print(b_data)
print(type(b_data))

# byte - > dict
dict_data2 = JSONParser().parse(BytesIO(b_data))
print(dict_data2)
print(type(dict_data2))

# dic - > byte
b_data2 = JSONRenderer().render(dic_data)
print(b_data2)
print(type(b_data2))

{‘id‘: 2, ‘name‘: ‘Metis Finance‘}
<class ‘dict‘>
{"id": 2, "name": "Metis Finance"}
<class ‘str‘>
b‘{"id": 2, "name": "Metis Finance"}‘
<class ‘bytes‘>
{‘id‘: 2, ‘name‘: ‘Metis Finance‘}
<class ‘dict‘>
b‘{"id":2,"name":"Metis Finance"}‘
<class ‘bytes‘>

Process finished with exit code 0