craftbeerpi4-pione/cbpi/utils/encoder.py
2021-02-16 20:37:51 +01:00

19 lines
463 B
Python

import datetime
from json import JSONEncoder
class ComplexEncoder(JSONEncoder):
def default(self, obj):
try:
if hasattr(obj, "to_json") and callable(getattr(obj, "to_json")):
return obj.to_json()
elif isinstance(obj, datetime.datetime):
return obj.__str__()
else:
raise TypeError()
except Exception as e:
pass
return None