-
-
Notifications
You must be signed in to change notification settings - Fork 9.3k
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
Using a json.JSONDecoder
fails when simplejson is present
#4842
Comments
encoding
and a json.JSONDecoder
fails when simplejson is presentjson.JSONDecoder
fails when simplejson is present
Hi @ricellis, thanks for revisiting this. I don't believe we'll be seeing this removed in the near future until 3.0.0 is ready. If you'd like to make a small addendum to the documentation about this, we'd be happy to review it. |
Thanks @nateprewitt I can update the docs, but would you be willing to accept a patch that protects people from the error if they are expecting to be using For example adding a check to |
Your proposed change breaks the deterministic behaviour of that function which is not ideal in the slightest. I don't think we can accept that. |
It's annoying that the
It was just a first stab at something that might work and I was trying to contain the change to just the specific area of the problem. I agree it is not the cleanest thing to do, but neither is swapping the JSON module with an API incompatible one. That's where this issue is different from the others that have been reported previously around the preferential import - this has identified a place where That in itself might not be a problem when the Perhaps someone can think of a better option than my proposal or maybe an option to disable the Or if you're not interested in fixing 2.x at all then just say so and I'll submit a docs patch and spend my time implementing a workaround in our library instead. |
@nateprewitt @sigmavirus24 any thoughts on this ^? |
When the simplejson module is present in the Phython environment, requests will use it to decode json but a call to response.json(cls=...) will raise an exception as the arguments accepted by JSONDecoder are inconsistent between the standard library json and simplejson (more details here: psf/requests#4842). So ensure to use standard json module for decoding response texts.
Simplejson is slower than stdlib json implementation in python 3, why don't you want to allow to override the default behavior at least? |
I ran into this problem when we added a third-party library that had a transitive dependency on |
Having the same issue as the user above. |
Any update :)? |
I came here because berserk uses request, and this issue comes up. Can you plz give a sttatus update? |
See also #3052 |
Our team has run into a similar issue, where we have simplejson installed but we want to use a custom json encoder class in the prepare_body method which now fails |
Hi @krystofernewman, it doesn't sound like your issue is related to this one directly. We're specifically discussing simplejson's impact on
|
@nateprewitt gotcha thanks for the response, I can open a separate issue for the impact of simplejson on the encoding of the request's body preparation if that works |
This uses the requests module and converts requests responses to json using requests' own `.json()` method on responses. For incomprehensible reasons, requests has spent about a decade using either simplejson or the standard library's json module more or less at will, and returning either one or the other exception types. They don't know why they use simplejson, we don't know why they use simplejson. In requests 3 (which will be released in the Year Of The Linux Desktop or when pigs fly, whichever one comes later) simplejson is dropped entirely. There are innumerable issues discussing the problem on the requests bugtracker, with the general consensus being that it's better to randomly return either one of two different libraries and two different library return types in errors -- because it was historically done that way and people might be depending on it. ?????? Bugs: psf/requests#710 psf/requests#2516 psf/requests#3052 psf/requests#4169 psf/requests#4842 psf/requests#5794 psf/requests#6084 The awkward workaround is to guarantee that requests' silent behavior of using simplejson *if it is installed* is forcibly triggered by forcibly depending on simplejson, and then catching the simplejson exception. The better solution here is pretty simple: do not rely on the requests module's automatic json conversion, this is as simple as using the already-imported json module and calling json.loads() on the retrieved content. Fixes: 1df343e Reimplements: bb154a8
This uses the requests module and converts requests responses to json using requests' own `.json()` method on responses. For incomprehensible reasons, requests has spent about a decade using either simplejson or the standard library's json module more or less at will, and returning either one or the other exception types. They don't know why they use simplejson, we don't know why they use simplejson. In requests 3 (which will be released in the Year Of The Linux Desktop or when pigs fly, whichever one comes later) simplejson is dropped entirely. There are innumerable issues discussing the problem on the requests bugtracker, with the general consensus being that it's better to randomly return either one of two different libraries and two different library return types in errors -- because it was historically done that way and people might be depending on it. ?????? Bugs: psf/requests#710 psf/requests#2516 psf/requests#3052 psf/requests#4169 psf/requests#4842 psf/requests#5794 psf/requests#6084 The awkward workaround is to guarantee that requests' silent behavior of using simplejson *if it is installed* is forcibly triggered by forcibly depending on simplejson, and then catching the simplejson exception. The better solution here is pretty simple: do not rely on the requests module's automatic json conversion, this is as simple as using the already-imported json module and calling json.loads() on the retrieved content. Fixes: 1df343e Reimplements: bb154a8
Another incompatible change in simplejson: simplejson/simplejson#324 (eg.: by adding a |
It appears that when
simplejson
is in the environmentrequests
preferentially imports it. However, the arguments accepted byJSONDecoder
are inconsistent between the standard libraryjson
andsimplejson
leading to errors when using the standardjson.JSONDecoder
whensimplejson
is in the environment.Your documentation for
response.json()
says:The documentation for
json.loads
says:and
I expected to be able to use a custom
json.JSONDecoder
without issue and indeed this works on Python 2.7, but in Python 3.7 it fails.I can see that the issue of preferentially importing
simplejson
has been raised a few times e.g.#2516
#3052
and that it is slated for removal in Requests 3, which I guess will resolve this issue.
If it is not possible to resolve this issue some other way in Requests 2.x then it would be nice if the documentation around
response.json()
was updated to make it clear that the arguments tojson.loads
could be eithersimplejson.loads
or the standard libjson.loads
depending on the environment since the preferential import is effectively undocumented and non-obvious.Expected Result
A successful JSON load when using
r.json(cls=json.JSONDecoder)
.Actual Result
Reproduction Steps
System Information
Note this issue does not occur in Python 2.7.x.
The text was updated successfully, but these errors were encountered: