Skip to content

feat: configurable response JSON decoder #965

@0x12th

Description

@0x12th

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

  1. Does the proposed API shape make sense?
  2. Should the decoder be configurable at the client level, request level, or both?
  3. Is json_deserializer the right naming?
  4. Is Request.extensions an acceptable propagation mechanism for this configuration?
  5. 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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions