feat: configurable response JSON decoder

Aperta
#965 5 commenti 1 reazione 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
5/5
Tempo stimato
Più di una settimana
Idoneità per principianti
30/100
Tipo di issue
Funzionalità
Chiarezza
Da chiarire
Stato di attività
Attiva
Stack tecnologico
python
Ambito
api, backend

Direzione di ricerca

Inizia leggendo build_request(), Request.extensions, response.json() e la gestione dell’invio della risposta descritta nell’issue. Esamina l’issue correlata #259 e le domande aperte sui livelli dell’API, la denominazione, la propagazione e il contratto del decoder. Il lavoro è completo quando il progetto ha concordato il design e la semantica proposta a livello di client e di request è specificata abbastanza da poter essere implementata.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

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.

Lingua principale
Python
Stelle
1.6k
Fork
103
Merge medio
1g 19h
PR unite (30g)
25

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di pydantic/httpx2

Tutte le issue di pydantic/httpx2

Issue simili

Altre issue su Python

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.