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

ujson option #1595

Closed
woozyking opened this issue Sep 12, 2013 · 8 comments
Closed

ujson option #1595

woozyking opened this issue Sep 12, 2013 · 8 comments

Comments

@woozyking
Copy link

Hi,

Is it possible to use ujson as an optional JSON encoder/decoder for requests? Or, if ujson satisfies all the use cases of requests, replace simplejson with ujson?

@Lukasa
Copy link
Member

Lukasa commented Sep 12, 2013

You absolutely can. The easiest way to do it is to monkeypatch:

import requests
import ujson

requests.models.json = ujson

That should cause all further calls to Response.json() to use ujson. As that's the only place in Requests that uses simplejson, that should cover everything.

@woozyking
Copy link
Author

So by looking at Response.json(), I reckon it's safe to use ujson as default for requests?

@Lukasa
Copy link
Member

Lukasa commented Sep 12, 2013

Certainly it will be safe to override that behaviour. For obvious reasons we won't change this in the core library, but I highly encourage you to use ujson if it suits your purpose better. =)

@woozyking
Copy link
Author

Fair :)

Thank you @Lukasa !

@Lukasa
Copy link
Member

Lukasa commented Sep 12, 2013

My pleasure, I hope you enjoy using Requests! =D Let us know if you have any problems. ⭐

@Lukasa Lukasa closed this as completed Sep 12, 2013
@ziadsawalha
Copy link

@Lukasa - would you consider using ujson by default if it is installed?

Something like:

# in compat.py
try:
    import ujson as json
except ImportError:
    try:
        import simplejson as json
    except ImportError:
        import json

@Lukasa
Copy link
Member

Lukasa commented Dec 20, 2013

Nope. =)

I see no reason for Requests to favour ujson over any other third-party JSON decoder. We do nothing very complicated with JSON decoding, so replacing the decoder we use either via monkeypatching or via doing the decoding yourself is totally safe. With that in mind, there's no good reason to move away from the standard library in Requests proper.

@synapticarbors
Copy link

For those finding this issue circa-2019, please not that I believe the current way of doing this would instead be:

import requests
import ujson

requests.models.complexjson = ujson

I've seen plenty of examples of code on Github that monkey-patches requests.models.json, which does not appear to accomplish the stated goal, since that attribute of models does not exist and is not used in the modern requests code base.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Sep 5, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants