Skip to content
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

Confusing error message when serializing set data type #398

Closed
pcdinh opened this issue Mar 25, 2020 · 1 comment
Closed

Confusing error message when serializing set data type #398

pcdinh opened this issue Mar 25, 2020 · 1 comment
Labels
duplicate This issue or pull request already exists

Comments

@pcdinh
Copy link

pcdinh commented Mar 25, 2020

What did you do?

In Python 3.8, I tried to serialize a Python dict whose value can be a set. ujson failed to dumps() it into a JSON string. It is expected. But the error message is quite confusing

Python 3.8.2 (default, Feb 26 2020, 02:56:10)
[GCC 7.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from ujson import dumps
>>> dumps(set())
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: �0d is not JSON serializable

What did you expect to happen?

ujson should have better error message that helps debugging. Here is the error message in built-in json module

>>> from json import dumps
>>> dumps(set())
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python3.8/json/__init__.py", line 231, in dumps
    return _default_encoder.encode(obj)
  File "/usr/lib/python3.8/json/encoder.py", line 199, in encode
    chunks = self.iterencode(o, _one_shot=True)
  File "/usr/lib/python3.8/json/encoder.py", line 257, in iterencode
    return _iterencode(o, 0)
  File "/usr/lib/python3.8/json/encoder.py", line 179, in default
    raise TypeError(f'Object of type {o.__class__.__name__} '
TypeError: Object of type set is not JSON serializable

json module has much better error message

What versions are you using?

  • OS: Ubuntu 18.04
  • Python: 3.8.2
  • UltraJSON: 2.0.1
from ujson import dumps
dumps(set())
@hugovk
Copy link
Member

hugovk commented Mar 25, 2020

Thanks, this was fixed in #382 and released in UltraJSON 2.0.2:

Python 3.8.1 (v3.8.1:1b293b6006, Dec 18 2019, 14:08:53)
[Clang 6.0 (clang-600.0.57)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from ujson import dumps
>>> import ujson
>>> ujson.__version__
'2.0.3'
>>> ujson.dumps(set())
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: set() is not JSON serializable

@hugovk hugovk closed this as completed Mar 25, 2020
@hugovk hugovk added the duplicate This issue or pull request already exists label Mar 25, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
duplicate This issue or pull request already exists
Projects
None yet
Development

No branches or pull requests

2 participants