Httpx: Custom JSON library

Created on 3 Jan 2020  Â·  6Comments  Â·  Source: encode/httpx

Hi!

Is there a way to use an alternative JSON library to decode the request? Like orjson for example?

Thanks!

question

Most helpful comment

You'd need to do that explicitly. I think it'd look like this to encode the request...

httpx.post(headers={'Content-Type': 'application/json'}, data=orjson.dumps(...))

...and like this, to decode the response:

orjson.loads(response.text)

All 6 comments

You'd need to do that explicitly. I think it'd look like this to encode the request...

httpx.post(headers={'Content-Type': 'application/json'}, data=orjson.dumps(...))

...and like this, to decode the response:

orjson.loads(response.text)

Alternatively, for a more automated solution, you could probably get away with a sys.modules hack? 😅

Here's an example — it uses a wrapper module to add verification-only print statements, but you can skip it and just use sys.modules["json"] = orjson.

# spy_orjson.py
import orjson


def loads(text):
    print("It works! Loading...")
    return orjson.loads(text)


def dumps(text):
    print("It works! Dumping...")
    return orjson.dumps(text)
# main.py
import sys
import spy_orjson

sys.modules["json"] = spy_orjson

import httpx

request = httpx.Request("GET", "https://example.org")
content = b'{"message": "Hello, world"}'
response = httpx.Response(
    200, content=content, headers={"Content-Type": "application/json"}, request=request
)

print(response.json())

Output:

$ python main.py
It works! Loading...

That's great! Thanks!!

@florimondmanca such an ugly hack...

Why not implement this feature? Looking at orjson and simdjson libraries, this may be used to improve performance a lot.

I'll try to implement this.

I'd be okay with us providing an easy way to patch this in, if it follows something similar to how requests allows for this... https://github.com/psf/requests/issues/1595

I'm looking at the code currently, don't see an easy way...

Probably I'll create a httpx.jsonlib with loads and dumps, which may be overridden later. Not the cleanest solution, but will allow to use e.g.:

  • orjson for loads and dumps (which returns bytes)
  • simdjson for loads and orjson for dumps
Was this page helpful?
0 / 5 - 0 ratings