Python 處理 JSON 數(shù)據(jù)時(shí),dumps 函數(shù)是經(jīng)常用到的,當(dāng) JSON 數(shù)據(jù)中有特殊類(lèi)型時(shí),往往是比較頭疼的,因?yàn)榻?jīng)常會(huì)報(bào)這樣一個(gè)錯(cuò)誤。
自定義編碼類(lèi)
#!/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, indumps_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 函數(shù)不知道如何處理?datetime 對(duì)象,默認(rèn)情況下?json 模塊使用?json.JSONEncoder 類(lèi)來(lái)進(jìn)行編碼,此時(shí)我們需要自定義一下編碼類(lèi)。
#!/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)
定義編碼類(lèi)?CustomEncoder 并重寫(xiě)實(shí)例的?default 函數(shù),對(duì)特殊類(lèi)型進(jìn)行處理,其余類(lèi)型繼續(xù)使用父類(lèi)的解析。
#!/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}
最后整合起來(lái),將類(lèi)使用?cls 參數(shù)傳入?dumps 函數(shù)即可。
使用?CustomEncoder 實(shí)例的?encode 函數(shù)可以對(duì)對(duì)象進(jìn)行轉(zhuǎn)碼
#!/usr/bin/env python # -*- coding:utf-8 -*- # Author: wxnacy(wxnacy@gmail.com) print(CustomEncoder().encode(datetime.now())) # 1562939035
在父類(lèi)源碼中,所有的編碼邏輯都在?encode 函數(shù)中,?default 只負(fù)責(zé)拋出?TypeError 異常,這就是文章開(kāi)始報(bào)錯(cuò)的出處。
#!/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)
單分派裝飾器處理對(duì)象
CustomEncoder 如果處理的對(duì)象種類(lèi)很多的話,需要寫(xiě)多個(gè)?if elif else 來(lái)區(qū)分,這樣并不是不行,但是不夠優(yōu)雅,不夠 pythonic
根據(jù)對(duì)象的類(lèi)型不同,而做出不同的處理。剛好有個(gè)裝飾器可以做到這點(diǎn),它就是單分派函數(shù) 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 函數(shù),是他處理默認(rèn)類(lèi)型。同時(shí)給他添加一個(gè)裝飾器構(gòu)造函數(shù)變量。
2 `@encode.register ()?是一個(gè)裝飾器構(gòu)造函數(shù),接收需要處理的對(duì)象類(lèi)型作為參數(shù)。用它裝飾的函數(shù)不需要名字, _` 代替即可。
最后提一點(diǎn),?json 也可以在命令行中使用
$ echo '{"json": "obj"}' | python -m json.tool { "json": "obj" }
參考鏈接
json
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號(hào)聯(lián)系: 360901061
您的支持是博主寫(xiě)作最大的動(dòng)力,如果您喜歡我的文章,感覺(jué)我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長(zhǎng)非常感激您!手機(jī)微信長(zhǎng)按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對(duì)您有幫助就好】元
