Configurable response JSON decoder
While working on #945, it became clear that request-side JSON serialization and response-side JSON decoding may not belong to the same API surface.
This discussion focuses only on configurable response JSON decoding.
Motivation
The original issue was primarily about customizing how response JSON is decoded, for example using orjson.loads instead of the standard library decoder.
Today response.json() is effectively tied to the stdlib implementation, which makes it difficult to:
- use alternative JSON libraries globally
- optimize JSON decoding performance
- customize decoding behavior consistently across a client
Proposed scope
Only response-side decoding.
No request-side JSON serialization customization.
Proposed API shape
Client-level decoder:
client = httpx.Client(
json_deserializer=orjson.loads,
)
Per-request override:
response = client.get(
"https://example.org",
json_deserializer=custom_decoder,
)
Explicit disable:
response = client.get(
"https://example.org",
json_deserializer=None,
)
Proposed semantics
- request-level decoder overrides client-level decoder
None explicitly disables the custom decoder
- manually-created
Request(...) objects without decoder configuration fall back to the client-level decoder
- redirect requests preserve decoder configuration
Internal design direction
Current direction is:
- keep all config merging in
build_request()
- propagate decoder configuration through
Request.extensions
- configure the
Response object during send handling
- avoid introducing new abstractions or generalized serialization hooks
Example internal extension key:
"httpx2.json_deserializer"
Type contract
Current proposal:
JsonDeserializer = typing.Callable[[bytes], typing.Any]
The intention is to keep the contract strict and compatible with libraries like orjson.
Open questions
- Does the proposed API shape make sense?
- Should the decoder be configurable at the client level, request level, or both?
- Is
json_deserializer the right naming?
- Is
Request.extensions an acceptable propagation mechanism for this configuration?
- Should the decoder contract accept only
bytes, or str | bytes?
I’d appreciate feedback before continuing the implementation work in the PR.
See also issue #259 for related discussion.
Configurable response JSON decoder
While working on #945, it became clear that request-side JSON serialization and response-side JSON decoding may not belong to the same API surface.
This discussion focuses only on configurable response JSON decoding.
Motivation
The original issue was primarily about customizing how response JSON is decoded, for example using
orjson.loadsinstead of the standard library decoder.Today
response.json()is effectively tied to the stdlib implementation, which makes it difficult to:Proposed scope
Only response-side decoding.
No request-side JSON serialization customization.
Proposed API shape
Client-level decoder:
Per-request override:
Explicit disable:
Proposed semantics
Noneexplicitly disables the custom decoderRequest(...)objects without decoder configuration fall back to the client-level decoderInternal design direction
Current direction is:
build_request()Request.extensionsResponseobject during send handlingExample internal extension key:
"httpx2.json_deserializer"Type contract
Current proposal:
The intention is to keep the contract strict and compatible with libraries like
orjson.Open questions
json_deserializerthe right naming?Request.extensionsan acceptable propagation mechanism for this configuration?bytes, orstr | bytes?I’d appreciate feedback before continuing the implementation work in the PR.
See also issue #259 for related discussion.