Skip to content
Snippets Groups Projects
Commit 87bceac1 authored by Art's avatar Art :lizard:
Browse files

Add bytes support to serialization

parent 312eaf47
No related branches found
No related tags found
No related merge requests found
......@@ -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":
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment