def _json_default_compat(obj):
classname = type(obj).__name__
handlers = {
'datetime': methodcaller('isoformat'),
'date': methodcaller('isoformat'),
'time': methodcaller('isoformat'),
'timedelta': _timedelta_total_seconds,
'set': list,
'frozenset': list,
'complex': partial(getattrs, attrs=['real', 'imag']),
'Fraction': partial(getattrs, attrs=['numerator', 'denominator']),
'UUID': str
}
if classname in handlers:
return handlers[classname](obj)
raise TypeError(repr(obj) + " is not JSON serializable")
评论列表
文章目录