Uh oh!
There was an error while loading. Please reload this page.
overridable json lib - #1344
Conversation
jcugat
commented
Oct 5, 2020
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 |
dmig
commented
Oct 5, 2020
This is a quick-n-dirty solution as mentioned here https://github.com/encode/httpx/issues/717#issuecomment-702802728. Currently, I'm working on better implementation. |
dmig
commented
Oct 6, 2020
Updated implementation: encoder/decoder are now passed as |
lovelydinosaur
commented
Oct 6, 2020
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
|
dmig-alarstudios
commented
Oct 6, 2020
@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. |
eseglem
commented
Oct 8, 2020
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 |
lovelydinosaur
commented
Oct 8, 2020
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 |
lovelydinosaur
commented
Oct 8, 2020
So, a more narrow alternative for consideration here... #1352 |
dmig-alarstudios
commented
Oct 8, 2020
The way from #1352 is much less flexible, it won't allow to overload a single encoder/decoder function. |
florimondmanca
commented
Oct 8, 2020
@dmig-alarstudios Yes, it would… importorjsonimportujsonclassCustomJSON:
defloads(self, text):
returnorjson.loads(text)
defdumps(self, value):
returnujson.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