Skip to content
Snippets Groups Projects
Commit 23841bbb authored by Alessandro Cerioni's avatar Alessandro Cerioni
Browse files

Adding utils/serializers.py

parent 9bdbba69
Loading
import datetime
# cf. https://stackoverflow.com/questions/30313243/messagepack-and-datetime
def decode_datetime(obj):
if '__datetime__' in obj.keys():
obj = datetime.datetime.strptime(obj["as_str"], "%Y%m%dT%H:%M:%S.%f")
elif '__date__' in obj.keys():
obj = datetime.datetime.strptime(obj["as_str"], "%Y%m%d")
else:
return obj
# cf. https://stackoverflow.com/questions/30313243/messagepack-and-datetime
def encode_datetime(obj):
if isinstance(obj, datetime.datetime):
return {'__datetime__': True, 'as_str': obj.strftime("%Y%m%dT%H:%M:%S.%f")}
if isinstance(obj, datetime.date):
return {'__date__': True, 'as_str': obj.strftime("%Y%m%d")}
# if isinstance(obj, Decimal):
# return float(obj)
return obj
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment