ramonhagenaars / jsons

🐍 A Python lib for (de)serializing Python objects to/from JSON
https://jsons.readthedocs.io
MIT License
288 stars 40 forks source link

zoneinfo support incomplete #141

Closed jrobbins-LiveData closed 3 years ago

jrobbins-LiveData commented 3 years ago

As the following REPL session seems to show, zoneinfo support in version 1.5.0 is incomplete. The standalone dump of ZoneInfo works, but when constructing a datetime with tzinfo , the nested dump doesn't work.

>>> import jsons
>>> from zoneinfo import ZoneInfo
>>> info = ZoneInfo(key='America/Los_Angeles')
>>> di = jsons.dump(info)
>>> di
{'key': 'America/Los_Angeles'}
>>> from datetime import datetime, timedelta
>>> dt = datetime(2020, 10, 31, 12, tzinfo=ZoneInfo("America/Los_Angeles"))
>>> jdt = jsons.dump(dt)
Traceback (most recent call last):
  File "C:\Users\jeff1\Documents\projects\git\cdk\r53\.venv\lib\site-packages\jsons\_dump_impl.py", line 60, in _do_dump
    result = serializer(obj, cls=cls, **kwargs)
  File "C:\Users\jeff1\Documents\projects\git\cdk\r53\.venv\lib\site-packages\jsons\serializers\default_datetime.py", line 21, in default_datetime_serializer
    return to_str(obj, strip_microseconds, kwargs['fork_inst'],
  File "C:\Users\jeff1\Documents\projects\git\cdk\r53\.venv\lib\site-packages\jsons\_datetime_impl.py", line 20, in to_str
    offset = get_offset_str(dt, fork_inst)
  File "C:\Users\jeff1\Documents\projects\git\cdk\r53\.venv\lib\site-packages\jsons\_datetime_impl.py", line 37, in get_offset_str
    result = _datetime_offset_str(obj, fork_inst)
  File "C:\Users\jeff1\Documents\projects\git\cdk\r53\.venv\lib\site-packages\jsons\_datetime_impl.py", line 76, in _datetime_offset_str
    tdelta = tzone.utcoffset(None) or tzone.adjusted_offset
AttributeError: 'zoneinfo.ZoneInfo' object has no attribute 'adjusted_offset'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Users\jeff1\Documents\projects\git\cdk\r53\.venv\lib\site-packages\jsons\_dump_impl.py", line 55, in dump
    return _do_dump(obj, serializer, cls, initial, kwargs_)
  File "C:\Users\jeff1\Documents\projects\git\cdk\r53\.venv\lib\site-packages\jsons\_dump_impl.py", line 66, in _do_dump
    raise SerializationError(str(err)) from err
jsons.exceptions.SerializationError: 'zoneinfo.ZoneInfo' object has no attribute 'adjusted_offset'
ramonhagenaars commented 3 years ago

This has now been fixed in v1.5.1.