-
-
Notifications
You must be signed in to change notification settings - Fork 842
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
overridable json lib #1344
overridable json lib #1344
Conversation
Not sure I like this approach. This could cause problems if a third party lib (for example a github client built on top httpx) decides to use Why not allow a new param in |
This is a quick-n-dirty solution as mentioned here #717 (comment). Currently, I'm working on better implementation. |
Updated implementation: encoder/decoder are now passed as |
So, I really don't particularly want us pushing support for this into the client or making a formal public API out of this. I'm just about okay with us allowing for a workaround to override the Third party libs all tend to have different behaviours from stdlib in obscure corner cases, and the "let's switch to X because it's faster" is almost always made as a pre-mature optimisation that's not evidence led. The network is generally going to be the bottleneck - so trading off improving JSON deserialization speeds vs. differences in behaviour vs. stdlib isn't usually a great option, or something that we should be promoting. As I say, I'm just about okay with providing a get-out that allows switching the
|
@tomchristie should I revert last commits?
Ok, what do you think of
Think about microservices: a lot of small high-performance applications, running within one network or even one server. That's my case and it is probably popular. |
I totally agree that there can be obscure corner cases, and users should understand the implications before using non standard libraries. Httpx definitely should not just pick a library for itself. Using stdlib, unless explicitly set not to, makes sense. However, I don't really see allowing it to be overridden as promoting their use. Just letting it happen, if someone makes the decision to do so. And, I think there is many legitimate reasons for not wanting to use stdlib, even though a lot of the uses are pre-mature optimization. What about how Pydantic does it: https://pydantic-docs.helpmanual.io/usage/exporting_models/#custom-json-deserialisation It defaults the config to I could see something like this maybe:
Then the you can just call |
So, I'm not convinced we'd necessarily want to accept API additions to support this, but if we do we want them to be absolutely as narrow as possible. With that in mind, I think the two options we could consider accepting pull requests for would be... Option 1. Globally overridable library substitution. httpx.json = orjson # Or perhaps httpx.jsonlib = ... Option 2. Client level library substitution. httpx.Client(jsonlib=...) I'm not super keen on option 2, because it'd also end up requiring to add public API for |
So, a more narrow alternative for consideration here... #1352 |
The way from #1352 is much less flexible, it won't allow to overload a single encoder/decoder function. |
@dmig-alarstudios Yes, it would… import orjson
import ujson
class CustomJSON:
def loads(self, text):
return orjson.loads(text)
def dumps(self, value):
return ujson.dumps(value)
httpx.json = CustomJSON() (Although I'm not sure what the use case for the JSON encoder and decoder to be different would be, but as you can see it's possible using the API from #1352.) |
Imlement #717