Skip to content

Add details for JSON serialization errors #122163

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

Closed
serhiy-storchaka opened this issue Jul 23, 2024 · 1 comment
Closed

Add details for JSON serialization errors #122163

serhiy-storchaka opened this issue Jul 23, 2024 · 1 comment
Labels
type-feature A feature request or enhancement

Comments

@serhiy-storchaka
Copy link
Member

serhiy-storchaka commented Jul 23, 2024

Feature or enhancement

When an JSON unserializable object occurs deeply in the large structure, it is difficult to find the culprit, because the error message by default only contains the type of the unserializable object. This is pretty common error, for example you can forget to convert the datetime object to timestamp or string.

The proposed PR adds notes to the raised exception which allow to identify the source of the error. For example:

>>> import json
>>> json.dumps([{'a': 1, 'b': 2}, {'a': 3, 'b': ...}])
Traceback (most recent call last):
  File "<python-input-16>", line 1, in <module>
    json.dumps([{'a': 1, 'b': 2}, {'a': 3, 'b': ...}])
    ~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/serhiy/py/cpython/Lib/json/__init__.py", line 231, in dumps
    return _default_encoder.encode(obj)
           ~~~~~~~~~~~~~~~~~~~~~~~^^^^^
  File "/home/serhiy/py/cpython/Lib/json/encoder.py", line 200, in encode
    chunks = self.iterencode(o, _one_shot=True)
  File "/home/serhiy/py/cpython/Lib/json/encoder.py", line 261, in iterencode
    return _iterencode(o, 0)
  File "/home/serhiy/py/cpython/Lib/json/encoder.py", line 180, in default
    raise TypeError(f'Object of type {o.__class__.__name__} '
                    f'is not JSON serializable')
TypeError: Object of type ellipsis is not JSON serializable
when serializing dict item 'b'
when serializing list item 1

Linked PRs

@serhiy-storchaka serhiy-storchaka added the type-feature A feature request or enhancement label Jul 23, 2024
@serhiy-storchaka
Copy link
Member Author

cc @iritkatriel

serhiy-storchaka added a commit to serhiy-storchaka/cpython that referenced this issue Jul 23, 2024
This that allows to identify the source of the error.
serhiy-storchaka added a commit to serhiy-storchaka/cpython that referenced this issue Jul 23, 2024
This allows to identify the source of the error.
serhiy-storchaka added a commit that referenced this issue Jul 23, 2024
This allows to identify the source of the error.
nohlson pushed a commit to nohlson/cpython that referenced this issue Jul 24, 2024
nohlson pushed a commit to nohlson/cpython that referenced this issue Jul 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type-feature A feature request or enhancement
Projects
Status: Done
Development

No branches or pull requests

1 participant