craftbeerpi4-pione/cbpi/utils/encoder.py

17 lines
365 B
Python
Raw Normal View History

2018-11-18 15:40:10 +01:00
from json import JSONEncoder
2019-01-08 23:31:39 +01:00
2018-11-18 15:40:10 +01:00
class ComplexEncoder(JSONEncoder):
def default(self, obj):
try:
2019-01-08 23:31:39 +01:00
2019-07-27 21:08:19 +02:00
if hasattr(obj, "to_json") and callable(getattr(obj, "to_json")):
return obj.to_json()
2018-11-18 15:40:10 +01:00
else:
2019-01-04 09:29:09 +01:00
raise TypeError()
2019-01-17 22:11:55 +01:00
except Exception as e:
2019-01-28 22:21:31 +01:00
print(e)
2018-11-18 15:40:10 +01:00
pass
return None