feat: configurable response JSON decoder
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 1.5k
- Forks
- 78
- Avg merge
- 8h 59m
- Merged PRs (30d)
- 24
Description
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
Noneexplicitly 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
Responseobject 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_deserializerthe right naming? - Is
Request.extensionsan acceptable propagation mechanism for this configuration? - Should the decoder contract accept only
bytes, orstr | bytes?
I’d appreciate feedback before continuing the implementation work in the PR.
See also issue #259 for related discussion.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by reading build_request(), Request.extensions, response.json(), and the response send handling described in the issue. Review related issue #259 and the open questions about API levels, naming, propagation, and the decoder contract. Done means the project agrees on the design and the proposed client- and request-level semantics are specified well enough to implement.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- api, backend
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Active
- Clarity
- Needs clarification
- Newbie friendliness
- 30/100