|
104 | 104 | from .http import WS_KEY, HttpVersion, WebSocketReader, WebSocketWriter |
105 | 105 | from .http_websocket import WSHandshakeError, ws_ext_gen, ws_ext_parse |
106 | 106 | from .tracing import Trace, TraceConfig |
107 | | -from .typedefs import JSONEncoder, LooseCookies, LooseHeaders, Query, StrOrURL |
| 107 | +from .typedefs import ( |
| 108 | + JSONBytesEncoder, |
| 109 | + JSONEncoder, |
| 110 | + LooseCookies, |
| 111 | + LooseHeaders, |
| 112 | + Query, |
| 113 | + StrOrURL, |
| 114 | +) |
108 | 115 |
|
109 | 116 | __all__ = ( |
110 | 117 | # client_exceptions |
@@ -271,6 +278,7 @@ class ClientSession: |
271 | 278 | "_default_auth", |
272 | 279 | "_version", |
273 | 280 | "_json_serialize", |
| 281 | + "_json_serialize_bytes", |
274 | 282 | "_requote_redirect_url", |
275 | 283 | "_timeout", |
276 | 284 | "_raise_for_status", |
@@ -311,6 +319,7 @@ def __init__( |
311 | 319 | skip_auto_headers: Iterable[str] | None = None, |
312 | 320 | auth: BasicAuth | None = None, |
313 | 321 | json_serialize: JSONEncoder = json.dumps, |
| 322 | + json_serialize_bytes: JSONBytesEncoder | None = None, |
314 | 323 | request_class: type[ClientRequest] = ClientRequest, |
315 | 324 | response_class: type[ClientResponse] = ClientResponse, |
316 | 325 | ws_response_class: type[ClientWebSocketResponse] = ClientWebSocketResponse, |
@@ -421,6 +430,7 @@ def __init__( |
421 | 430 | self._default_auth = auth |
422 | 431 | self._version = version |
423 | 432 | self._json_serialize = json_serialize |
| 433 | + self._json_serialize_bytes = json_serialize_bytes |
424 | 434 | self._raise_for_status = raise_for_status |
425 | 435 | self._auto_decompress = auto_decompress |
426 | 436 | self._trust_env = trust_env |
@@ -561,7 +571,10 @@ async def _request( |
561 | 571 | "data and json parameters can not be used at the same time" |
562 | 572 | ) |
563 | 573 | elif json is not None: |
564 | | - data = payload.JsonPayload(json, dumps=self._json_serialize) |
| 574 | + if self._json_serialize_bytes is not None: |
| 575 | + data = payload.JsonBytesPayload(json, dumps=self._json_serialize_bytes) |
| 576 | + else: |
| 577 | + data = payload.JsonPayload(json, dumps=self._json_serialize) |
565 | 578 |
|
566 | 579 | if not isinstance(chunked, bool) and chunked is not None: |
567 | 580 | warnings.warn("Chunk size is deprecated #1615", DeprecationWarning) |
|
0 commit comments