craftbeerpi4-pione/cbpi/utils/encoder.py

23 lines
600 B
Python
Raw Normal View History

2018-11-18 15:40:10 +01:00
from json import JSONEncoder
class ComplexEncoder(JSONEncoder):
def default(self, obj):
2019-01-05 20:43:48 +01:00
from cbpi.database.orm_framework import DBModel
2018-12-29 00:27:19 +01:00
2018-11-18 15:40:10 +01:00
try:
if isinstance(obj, DBModel):
return obj.__dict__
2018-12-29 00:27:19 +01:00
#elif callable(getattr(obj, "reprJSON")):
# return obj.reprJSON()
2018-12-13 21:45:33 +01:00
#elif isinstance(obj, ActorModel):
# return None
2019-01-02 21:20:44 +01:00
#elif hasattr(obj, "callback"):
# return obj()
2018-11-18 15:40:10 +01:00
else:
2019-01-04 09:29:09 +01:00
raise TypeError()
2019-01-02 21:20:44 +01:00
except TypeError:
2018-11-18 15:40:10 +01:00
pass
return None