mirror of
https://github.com/PiBrewing/craftbeerpi4.git
synced 2024-11-10 01:17:42 +01:00
26 lines
No EOL
580 B
Python
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) |