craftbeerpi4-pione/cbpi/utils/encoder.py

25 lines
639 B
Python
Raw Permalink Normal View History

2019-08-05 23:00:18 +02:00
import datetime
2018-11-18 15:40:10 +01:00
from json import JSONEncoder
2021-02-27 20:09:19 +01:00
from pandas import Timestamp
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__()
2021-02-27 20:09:19 +01:00
elif isinstance(obj, Timestamp):
2023-01-22 16:37:10 +01:00
#print("TIMe")
2021-02-27 20:09:19 +01:00
return obj.__str__()
2018-11-18 15:40:10 +01:00
else:
2023-01-22 16:37:10 +01:00
#print(type(obj))
2019-01-04 09:29:09 +01:00
raise TypeError()
2019-01-17 22:11:55 +01:00
except Exception as e:
2021-02-16 20:37:51 +01:00
2018-11-18 15:40:10 +01:00
pass
return None