內建json模組對於python內建型別序列化的描述
"""extensible json encoder for python data structures.
supports the following objects and types by default:
+-------------------+---------------+
| python | json |
+***************====+***************+
| dict | object |
+-------------------+---------------+
| list, tuple | array |
+-------------------+---------------+
| str | string |
+-------------------+---------------+
| int, float | number |
+-------------------+---------------+
| true | true |
+-------------------+---------------+
| false | false |
+-------------------+---------------+
| none | null |
+-------------------+---------------+
to extend this to recognize other objects, subclass and implement a
``.default()`` method with another method that returns a serializable
object for ``o`` if possible, otherwise it should call the superclass
implementation (to raise ``typeerror``).
"""
內建json模組對於python內建型別反序列化的描述
"""****** json decoder
performs the following translations in decoding by default:
+---------------+-------------------+
| json | python |
+***************+***************====+
| object | dict |
+---------------+-------------------+
| array | list |
+---------------+-------------------+
| string | str |
+---------------+-------------------+
| number (int) | int |
+---------------+-------------------+
| number (real) | float |
+---------------+-------------------+
| true | true |
+---------------+-------------------+
| false | false |
+---------------+-------------------+
| null | none |
+---------------+-------------------+
it also understands ``nan``, ``infinity``, and ``-infinity`` as
their corresponding ``float`` values, which is outside the json spec.
"""
分別使用pickle和json模組來實現自定義型別的序列化和反序列化
'''
'''class
person()
:"""人類"""
# __slots__ = ['age']
# __dict__ = ['age', 'name']
_age:
int=
0def
__init__
(self, age, name=
'eason'):
# json.jsonencoder.__init__(self, skipkeys=true)
self.age = age
self.name = name
# self.name = name
# def __dir__(self):
# return ['age', 'name']
@property
defage
(self)
->
int:
return self._age
@age.setter
defage(self, age:
int)
:print
('set age'
)if age <=0:
raise valueerror(
'age'
) self._age = age
defhello
(self)
:print
('**********====hello-locals***************'
)print
(locals()
)
import pickle
import json
from demo.src.models.person import person
# import demo.src.models.person.person
class
personjsonencoder
(json.jsonencoder)
:def
default
(self, o: person)
:# 返回字典型別
return
# def encode(self, o: person):
# # 直接返回字典,
# return str()
class
personjsondecoder
(json.jsondecoder)
:def
decode
(self, s:
str)
: obj_dict = json.loads(s)
# return person(obj_dict['age'], obj_dict['age'])
return person(
**obj_dict)
p = person(28,
'wjchi'
)# bytes
# p_bytes = pickle.dumps(p)
# print(type(p_bytes), p_bytes)
# p_obj = pickle.loads(p_bytes)
# print(type(p_obj), p_obj.age)
# string
# p_str = json.dumps(p, default=lambda obj: obj.__dict__)
# print(type(p_str), p_str)
# p_dict = json.loads(p_str)
# print(type(p_dict), p_dict['age'])
p_str = json.dumps(p, cls=personjsonencoder)
print
(type
(p_str)
, p_str)
p_dict = json.loads(p_str)
# print(type(p_dict), p_dict['age'])
# p_obj = json.loads(p_str, cls=personjsondecoder)
p_obj = person(
**p_dict)
print
(type
(p_obj)
, p_obj.age)
序列化反序列化
只要用到網路開發啊,就一定會用到序列化反序列化。1,自定義結構體 struct test int len int type char data 10 test data test buffer.缺點 明文,只支援基本型別,不支援變長結構 2,在1的基礎上,自定義乙個緩衝類,存放乙個訊息。把訊息寫入緩...
序列化反序列化
using system using system.collections.generic using system.io using system.linq using system.text using system.threading.tasks namespace 序列化反序列化 syste...
序列化和反序列化 C 序列化與反序列化。
序列化介紹 把物件用一種新的格式來表示。系列化只序列化資料。序列化不建議使用自動屬性 為什麼要序列化 將乙個複雜的物件轉換流,方便儲存與資訊交換。class program class person public int age 二進位制序列化 就是將物件變成流的過程,把物件變成byte class...