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

Improve error message when trying to encode unsupported object type #340

Conversation

kerrick-lyft
Copy link

@kerrick-lyft kerrick-lyft commented Oct 2, 2019

When you try to encode an unsupported object type, you can get a hard-to-understand error:

>>> import numpy
>>> import ujson
>>> ujson.dumps(numpy.bool_(True))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
OverflowError: Maximum recursion level reached

Add a hint to this error message about why the encoding may have failed: (maybe you tried encoding a type of object that isn't supported?)

Fixes #339

@kerrick-lyft

This comment has been minimized.

@hugovk

This comment has been minimized.

@hugovk hugovk added the changelog: Changed For changes in existing functionality label Mar 3, 2020
@hugovk
Copy link
Member

hugovk commented Mar 13, 2020

Hello, thank you very much for the issue and PR.

Since UltraJSON 2.0 was released with 53f85b1, this is no longer valid:

Removed generic serialization of objects/iterables

The behavior of ujson has always been to try to serialize all objects in
any way possible. This has been quite a deviation from other json
libraries, including Pythons standard json module, and the source of a
lot of confusion and bugs. Removing this quirk moves ultrajson closer to
the expected behavior.

Instead of trying to coerce serialization ultrajson will now throw a
TypeError: "repr(obj) is not JSON serializable" exception.

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.
(.startup.py)
(imported collections, datetime, itertools, math, os, pprint, re, sys, time)
>>> import numpy
>>> import ujson
>>> ujson.dumps(numpy.bool_(True))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: � is not JSON serializable
>>>

The "�" bit is another issue: #258.

So let's close this. Thanks again for contributing!

@hugovk hugovk closed this Mar 13, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
changelog: Changed For changes in existing functionality
Projects
None yet
Development

Successfully merging this pull request may close these issues.

OverflowError error message description should be improved
2 participants