Python 處理 JSON 數據時,dumps 函數是經常用到的,當 JSON 數據中有特殊類型時,往往是比較頭疼的,因為經常會報這樣一個錯誤。
自定義編碼類
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author: wxnacy(wxnacy@gmail.com)
import json
from datetime import datetime
USER_DATA = dict(
id = 1, name = 'wxnacy', ts = datetime.now()
)
print(json.dumps(USER_DATA))
Traceback (most recent call last):
File "/Users/wxnacy/PycharmProjects/study/python/office_module/json_demo/dumps.py", line 74, in
dumps_encoder()
File "/Users/wxnacy/PycharmProjects/study/python/office_module/json_demo/dumps.py", line 68, in dumps_encoder
print(json.dumps(USER_DATA))
File "/Users/wxnacy/.pyenv/versions/3.6.0/Python.framework/Versions/3.6/lib/python3.6/json/__init__.py", line 231, in dumps
return _default_encoder.encode(obj)
File "/Users/wxnacy/.pyenv/versions/3.6.0/Python.framework/Versions/3.6/lib/python3.6/json/encoder.py", line 199, in encode
chunks = self.iterencode(o, _one_shot=True)
File "/Users/wxnacy/.pyenv/versions/3.6.0/Python.framework/Versions/3.6/lib/python3.6/json/encoder.py", line 257, in iterencode
return _iterencode(o, 0)
File "/Users/wxnacy/.pyenv/versions/3.6.0/Python.framework/Versions/3.6/lib/python3.6/json/encoder.py", line 180, in default
o.__class__.__name__)
TypeError: Object of type 'datetime' is not JSON serializable
原因在于?dumps 函數不知道如何處理?datetime 對象,默認情況下?json 模塊使用?json.JSONEncoder 類來進行編碼,此時我們需要自定義一下編碼類。
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author: wxnacy(wxnacy@gmail.com)
class CustomEncoder(json.JSONEncoder):
def default(self, x):
if isinstance(x, datetime):
return int(x.timestamp())
return super().default(self, x)
定義編碼類?CustomEncoder 并重寫實例的?default 函數,對特殊類型進行處理,其余類型繼續使用父類的解析。
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author: wxnacy(wxnacy@gmail.com)
import json
from datetime import datetime
class CustomEncoder(json.JSONEncoder):
def default(self, x):
if isinstance(x, datetime):
return int(x.timestamp())
return super().default(self, x)
USER_DATA = dict(
id = 1, name = 'wxnacy', ts = datetime.now()
)
print(json.dumps(USER_DATA, cls=CustomEncoder))
# {"id": 1, "name": "wxnacy", "ts": 1562938926}
最后整合起來,將類使用?cls 參數傳入?dumps 函數即可。
使用?CustomEncoder 實例的?encode 函數可以對對象進行轉碼
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author: wxnacy(wxnacy@gmail.com)
print(CustomEncoder().encode(datetime.now()))
# 1562939035
在父類源碼中,所有的編碼邏輯都在?encode 函數中,?default 只負責拋出?TypeError 異常,這就是文章開始報錯的出處。
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author: wxnacy(wxnacy@gmail.com)
def default(self, o):
"""Implement this method in a subclass such that it returns
a serializable object for ``o``, or calls the base implementation
(to raise a ``TypeError``).
For example, to support arbitrary iterators, you could
implement default like this::
def default(self, o):
try:
iterable = iter(o)
except TypeError:
pass
else:
return list(iterable)
# Let the base class default method raise the TypeError
return JSONEncoder.default(self, o)
"""
raise TypeError(f'Object of type {o.__class__.__name__} '
f'is not JSON serializable')
def encode(self, o):
"""Return a JSON string representation of a Python data structure.
>>> from json.encoder import JSONEncoder
>>> JSONEncoder().encode({"foo": ["bar", "baz"]})
'{"foo": ["bar", "baz"]}'
"""
# This is for extremely simple cases and benchmarks.
if isinstance(o, str):
if self.ensure_ascii:
return encode_basestring_ascii(o)
else:
return encode_basestring(o)
# This doesn't pass the iterator directly to ''.join() because the
# exceptions aren't as detailed. The list call should be roughly
# equivalent to the PySequence_Fast that ''.join() would do.
chunks = self.iterencode(o, _one_shot=True)
if not isinstance(chunks, (list, tuple)):
chunks = list(chunks)
return ''.join(chunks)
單分派裝飾器處理對象
CustomEncoder 如果處理的對象種類很多的話,需要寫多個?if elif else 來區分,這樣并不是不行,但是不夠優雅,不夠 pythonic
根據對象的類型不同,而做出不同的處理。剛好有個裝飾器可以做到這點,它就是單分派函數 functools.singledispatch
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author: wxnacy(wxnacy@gmail.com)
from datetime import datetime
from datetime import date
from functools import singledispatch
class CustomEncoder(json.JSONEncoder):
def default(self, x):
try:
return encode(x)
except TypeError:
return super().default(self, x)
@singledispatch # 1
def encode(x):
raise TypeError('Unencode type')
@encode.register(datetime) # 2
def _(x):
return int(x.timestamp())
@encode.register(date)
def _(x):
return x.isoformat()
print(json.dumps(dict(dt = datetime.now(), d = date.today()), cls=CustomEncoder))
# {"dt": 1562940781, "d": "2019-07-12"}
1 使用?@singledispatch 裝飾?encode 函數,是他處理默認類型。同時給他添加一個裝飾器構造函數變量。
2 `@encode.register ()?是一個裝飾器構造函數,接收需要處理的對象類型作為參數。用它裝飾的函數不需要名字, _` 代替即可。
最后提一點,?json 也可以在命令行中使用
$ echo '{"json": "obj"}' | python -m json.tool
{
"json": "obj"
}
參考鏈接
json
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061
微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元

