diff --git a/pikatasks/serialization.py b/pikatasks/serialization.py index e796f572fdef77688ec26f02f4a626190852e572..f8113e748d34938ef01bbd8ca31038d51a6e5a97 100644 --- a/pikatasks/serialization.py +++ b/pikatasks/serialization.py @@ -3,6 +3,7 @@ import json import datetime import collections import itertools +import base64 from . import settings from .utils import logger @@ -67,11 +68,17 @@ def json_serialize_tweaks(obj): JSON_PYTHON_DATA_TYPE: "set", JSON_PYTHON_DATA_VALUE: list(obj), } + elif isinstance(obj, bytes): + logger.warning("Going to serialize: {obj}".format(obj=obj)) + return { + JSON_PYTHON_DATA_TYPE: "bytes/base64", + JSON_PYTHON_DATA_VALUE: base64.b64encode(obj).decode("utf-8") # need to .decode because b64encode returns bytes + } elif isinstance(obj, collections.Iterable): - # iterators and other iterables will become lists + # other iterables and iterators will become lists elements = list(itertools.islice(obj, JSON_ITER_MAX_YIELD)) # protect from trolls with itertools.repeat() if len(elements is JSON_ITER_MAX_YIELD): - logger.warning("Will not automatically yield more than {n} elements from {obj}.".format(n=JSON_ITER_MAX_YIELD, obj=obj)) + logger.warning("Will not automatically yield more than {n} elements from {t}.".format(n=JSON_ITER_MAX_YIELD, t=type(obj))) return elements elif isinstance(obj, datetime.datetime): return { @@ -101,6 +108,8 @@ def json_deserialize_tweaks(obj): value = obj[JSON_PYTHON_DATA_VALUE] if type_name == "set": return set(value) + elif type_name == "bytes/base64": + return base64.b64decode(value.encode("utf-8")) elif type_name == "datetime": return str_to_datetime(value) elif type_name == "date":