我有一个缓存字典,它为每个键存储一个 3 元素列表:[value, date_created, hits]
. 缓存通信是通过 JSON 完成的。缓存中有一个items
命令将返回所有项目。这是设置缓存方法:
@status
def handle_set(self, key, value):
self.data[key] = [value, datetime.datetime.now(), 0]
return
当我有一个非空缓存并调用items
它时,就会出现问题。Python datetime 对象不可序列化:
Traceback (most recent call last):
File "/usr/lib/python2.7/dist-packages/twisted/python/log.py", line 84, in callWithLogger
return callWithContext({"system": lp}, func, *args, **kw)
File "/usr/lib/python2.7/dist-packages/twisted/python/log.py", line 69, in callWithContext
return context.call({ILogContext: newCtx}, func, *args, **kw)
File "/usr/lib/python2.7/dist-packages/twisted/python/context.py", line 118, in callWithContext
return self.currentContext().callWithContext(ctx, func, *args, **kw)
File "/usr/lib/python2.7/dist-packages/twisted/python/context.py", line 81, in callWithContext
return func(*args,**kw)
--- <exception caught here> ---
File "/usr/lib/python2.7/dist-packages/twisted/internet/selectreactor.py", line 146, in _doReadOrWrite
why = getattr(selectable, method)()
File "/usr/lib/python2.7/dist-packages/twisted/internet/tcp.py", line 460, in doRead
rval = self.protocol.dataReceived(data)
File "./server.py", line 17, in dataReceived
result = getattr(self.factory, command)(**request)
File "./server.py", line 35, in execute
return json.dumps(result)
File "/usr/lib/python2.7/json/__init__.py", line 231, in dumps
return _default_encoder.encode(obj)
File "/usr/lib/python2.7/json/encoder.py", line 201, in encode
chunks = self.iterencode(o, _one_shot=True)
File "/usr/lib/python2.7/json/encoder.py", line 264, in iterencode
return _iterencode(o, 0)
File "/usr/lib/python2.7/json/encoder.py", line 178, in default
raise TypeError(repr(o) + " is not JSON serializable")
exceptions.TypeError: datetime.datetime(2013, 10, 26, 11, 38, 42, 348094) is not JSON serializable
我发现了一个类似的SO question。但我不喜欢接受的答案是我必须提供一个自定义序列化程序。
在我的缓存中,我有不同的命令,但我想对所有缓存命令使用一种 JSON 格式的方法。如果我要遵循这个答案,恐怕我必须做一个 if-cascade。
是否有任何方法可以覆盖datetime.datetime
以提供 JSON 序列化程序将使用的一种简单方法?或者有什么更好的解决方案?
datetime.datetime JSON 序列化对我来说可能很简单str(d)
(字符串表示)。