2019-08-05 23:00:18 +02:00
|
|
|
import datetime
|
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()
|
2019-08-05 23:00:18 +02:00
|
|
|
elif isinstance(obj, datetime.datetime):
|
|
|
|
return obj.__str__()
|
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
|