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

Support for `zoneinfo` as specified in Python 3.9 #131

Closed rudolf-bauer closed 3 years ago

rudolf-bauer commented 3 years ago

In Python 3.9 zoneinfo was introduced`.

Currently, this simple example code does not work:

from zoneinfo import ZoneInfo
import datetime
import jsons

d = datetime.datetime.now(ZoneInfo("America/Los_Angeles"))
jsons.dumps(d)

It results in the following error:

Traceback (most recent call last):
  File "/Users/***/Library/Python/3.9/lib/python/site-packages/jsons/_dump_impl.py", line 61, in _do_dump
    result = serializer(obj, cls=cls, **kwargs)
  File "/Users/***/Library/Python/3.9/lib/python/site-packages/jsons/serializers/default_datetime.py", line 21, in default_datetime_serializer
    return to_str(obj, strip_microseconds, kwargs['fork_inst'],
  File "/Users/***/Library/Python/3.9/lib/python/site-packages/jsons/_datetime_impl.py", line 20, in to_str
    offset = get_offset_str(dt, fork_inst)
  File "/Users/***/Library/Python/3.9/lib/python/site-packages/jsons/_datetime_impl.py", line 37, in get_offset_str
    result = _datetime_offset_str(obj, fork_inst)
  File "/Users/***/Library/Python/3.9/lib/python/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 "/Users/***/Library/Python/3.9/lib/python/site-packages/jsons/_dump_impl.py", line 88, in dumps
    dumped = dump(obj, *args, **kwargs)
  File "/Users/***/Library/Python/3.9/lib/python/site-packages/jsons/_dump_impl.py", line 56, in dump
    return _do_dump(obj, serializer, cls, initial, kwargs_)
  File "/Users/***/Library/Python/3.9/lib/python/site-packages/jsons/_dump_impl.py", line 67, in _do_dump
    raise SerializationError(str(err)) from err
jsons.exceptions.SerializationError: 'zoneinfo.ZoneInfo' object has no attribute 'adjusted_offset'

I am using jsons==1.4.0. Are you planning to support zoneinfo soon?