diff --git a/kombu/utils/json.py b/kombu/utils/json.py index f6429c3f..26583d61 100644 --- a/kombu/utils/json.py +++ b/kombu/utils/json.py @@ -41,9 +41,7 @@ class JSONEncoder(_encoder_cls): if not isinstance(o, datetime): o = datetime(o.year, o.month, o.day, 0, 0, 0, 0) r = o.isoformat() - if r.endswith("+00:00"): - r = r[:-6] + "Z" - return r + return {"datetime": r, "__datetime__": True} elif isinstance(o, times): return o.isoformat() elif isinstance(o, textual): @@ -71,6 +69,8 @@ def dumps(s, _dumps=json.dumps, cls=None, default_kwargs=None, **kwargs): def object_hook(dct): """Hook function to perform custom deserialization.""" + if "__datetime__" in dct: + return datetime.datetime.fromisoformat(dct["datetime"]) if "__bytes__" in dct: return dct["bytes"].encode("utf-8") if "__base64__" in dct: diff --git a/requirements/test.txt b/requirements/test.txt index 6674f34c..038d5e9d 100644 --- a/requirements/test.txt +++ b/requirements/test.txt @@ -3,3 +3,4 @@ pytest~=7.1.1 pytest-sugar Pyro4 hypothesis +pytest-freezegun diff --git a/t/unit/utils/test_json.py b/t/unit/utils/test_json.py index c263b4f9..54d559e4 100644 --- a/t/unit/utils/test_json.py +++ b/t/unit/utils/test_json.py @@ -25,11 +25,10 @@ class Custom: class test_JSONEncoder: - + @pytest.mark.freeze_time("2015-10-21") def test_datetime(self): now = datetime.utcnow() now_utc = now.replace(tzinfo=pytz.utc) - stripped = datetime(*now.timetuple()[:3]) serialized = loads(dumps({ 'datetime': now, 'tz': now_utc, @@ -37,10 +36,10 @@ class test_JSONEncoder: 'time': now.time()}, )) assert serialized == { - 'datetime': now.isoformat(), - 'tz': '{}Z'.format(now_utc.isoformat().split('+', 1)[0]), + 'datetime': now, + 'tz': now_utc, 'time': now.time().isoformat(), - 'date': stripped.isoformat(), + 'date': datetime(now.year, now.month, now.day, 0, 0, 0, 0), } @given(message=st.binary())