craftbeerpi4-pione/core/helper/jsondump.py
2018-11-01 19:50:04 +01:00

26 lines
No EOL
580 B
Python

import json
from json import JSONEncoder
from core.database.model import DBModel, ActorModel
class ComplexEncoder(JSONEncoder):
def default(self, obj):
try:
if isinstance(obj, DBModel):
return obj.__dict__
elif isinstance(obj, ActorModel):
return None
elif hasattr(obj, "callback"):
return obj()
else:
return None
except TypeError as e:
pass
return None
def json_dumps(obj):
return json.dumps(obj, cls=ComplexEncoder)