Skip to content

Commit

Permalink
Perform custom deserializations
Browse files Browse the repository at this point in the history
Items with these values will start appearing in a later version of kombu (5.3.0 onwards).

To migrate our kombu installation safely, we first add support for deserializing these objects, deploy that, and then we can migrate kombu to version 5.3.0 safely.
  • Loading branch information
jbkkd committed Oct 3, 2023
1 parent 444aa42 commit 3b235b4
Showing 1 changed file with 15 additions and 2 deletions.
17 changes: 15 additions & 2 deletions kombu/utils/json.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
"""JSON Serialization Utilities."""

import base64
import datetime
import decimal
import json as stdjson
Expand Down Expand Up @@ -69,6 +69,19 @@ def dumps(s, _dumps=json.dumps, cls=None, default_kwargs=None, **kwargs):
**dict(default_kwargs, **kwargs))


def object_hook(dct):
"""Hook function to perform custom deserialization."""
if "__bytes__" in dct:
return dct["bytes"].encode("utf-8")
if "__base64__" in dct:
return base64.b64decode(dct["bytes"].encode("utf-8"))
if "__uuid__" in dct:
return uuid.UUID(dct["uuid"], version=dct["version"])
if "__datetime__" in dct:
return datetime.datetime.fromisoformat(dct["datetime"])
return dct


def loads(s, _loads=json.loads, decode_bytes=True):
"""Deserialize json from string."""
# None of the json implementations supports decoding from
Expand All @@ -88,4 +101,4 @@ def loads(s, _loads=json.loads, decode_bytes=True):
return _loads(s)
except _DecodeError:
# catch "Unpaired high surrogate" error
return stdjson.loads(s)
return stdjson.loads(s, object_hook=object_hook)

0 comments on commit 3b235b4

Please sign in to comment.