Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: BI-5673 add bytes-to-json serializer #551

Merged
merged 1 commit into from
Jul 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 3 additions & 5 deletions lib/dl_core/dl_core_tests/unit/test_json_serializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
some_timedelta=datetime.timedelta(seconds=1320.0231),
some_decimal=decimal.Decimal("12345" * 9 + "." + "54321" * 9),
some_uuid=uuid.UUID("12345678123456781234567812345678"),
# some_bytes=b"Another one bites", TODO: currently not serializable
some_bytes=b"Another one bites",
)


Expand All @@ -47,7 +47,7 @@
"value": "123451234512345123451234512345123451234512345.543215432154321543215432154321543215432154321",
},
some_uuid={"__dl_type__": "uuid", "value": "12345678-1234-5678-1234-567812345678"},
# some_bytes=b"Another one bites",
some_bytes={"__dl_type__": "bytes", "value": "QW5vdGhlciBvbmUgYml0ZXM="},
)


Expand All @@ -61,9 +61,7 @@ def test_json_serialization():


def test_json_tricky_serialization():
data = SAMPLE_DATA
dumped = json.dumps(data, cls=RedisDatalensDataJSONEncoder)
tricky_data = dict(normal=data, abnormal=json.loads(dumped))
tricky_data = dict(normal=SAMPLE_DATA, abnormal=EXPECTED_DUMP)
tricky_data_dumped = json.dumps(tricky_data, cls=RedisDatalensDataJSONEncoder)
tricky_roundtrip = json.loads(tricky_data_dumped, cls=RedisDatalensDataJSONDecoder)
assert tricky_roundtrip["normal"] == tricky_data["normal"], tricky_roundtrip
Expand Down
15 changes: 15 additions & 0 deletions lib/dl_model_tools/dl_model_tools/serialization.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from __future__ import annotations

import abc
import base64
import datetime
import decimal
import json
Expand Down Expand Up @@ -152,13 +153,27 @@ def from_jsonable(value: TJSONLike) -> uuid.UUID:
return uuid.UUID(value)


class BytesSerializer(TypeSerializer[bytes]):
typename = "bytes"

@staticmethod
def to_jsonable(value: bytes) -> TJSONLike:
return base64.b64encode(value).decode("ascii")

@staticmethod
def from_jsonable(value: TJSONLike) -> bytes:
assert isinstance(value, str)
return base64.b64decode(value, validate=True)


COMMON_SERIALIZERS: list[Type[TypeSerializer]] = [
DateSerializer,
DatetimeSerializer,
TimeSerializer,
TimedeltaSerializer,
DecimalSerializer,
UUIDSerializer,
BytesSerializer,
]
assert len(set(cls.typename for cls in COMMON_SERIALIZERS)) == len(COMMON_SERIALIZERS), "uniqueness check"

Expand Down
Loading