|
4 | 4 | import base64 |
5 | 5 | import dataclasses |
6 | 6 | import hashlib |
7 | | -import json |
8 | 7 | import os |
9 | 8 | import sys |
10 | 9 | import traceback |
|
107 | 106 | from .http import WS_KEY, HttpVersion, WebSocketReader, WebSocketWriter |
108 | 107 | from .http_websocket import WSHandshakeError, ws_ext_gen, ws_ext_parse |
109 | 108 | from .tracing import Trace, TraceConfig |
110 | | -from .typedefs import JSONEncoder, LooseCookies, LooseHeaders, Query, StrOrURL |
| 109 | +from .typedefs import ( |
| 110 | + DEFAULT_JSON_BYTES_ENCODER, |
| 111 | + DEFAULT_JSON_ENCODER, |
| 112 | + JSONBytesEncoder, |
| 113 | + JSONEncoder, |
| 114 | + LooseCookies, |
| 115 | + LooseHeaders, |
| 116 | + Query, |
| 117 | + StrOrURL, |
| 118 | +) |
111 | 119 |
|
112 | 120 | __all__ = ( |
113 | 121 | # client_exceptions |
@@ -277,7 +285,8 @@ def __init__( |
277 | 285 | proxy_auth: Optional[BasicAuth] = None, |
278 | 286 | skip_auto_headers: Optional[Iterable[str]] = None, |
279 | 287 | auth: Optional[BasicAuth] = None, |
280 | | - json_serialize: JSONEncoder = json.dumps, |
| 288 | + json_serialize: JSONEncoder = DEFAULT_JSON_ENCODER, |
| 289 | + json_serialize_bytes: JSONBytesEncoder = DEFAULT_JSON_BYTES_ENCODER, |
281 | 290 | request_class: Type[ClientRequest] = ClientRequest, |
282 | 291 | response_class: Type[ClientResponse] = ClientResponse, |
283 | 292 | ws_response_class: Type[ClientWebSocketResponse] = ClientWebSocketResponse, |
@@ -357,6 +366,7 @@ def __init__( |
357 | 366 | self._default_auth = auth |
358 | 367 | self._version = version |
359 | 368 | self._json_serialize = json_serialize |
| 369 | + self._json_serialize_bytes = json_serialize_bytes |
360 | 370 | self._raise_for_status = raise_for_status |
361 | 371 | self._auto_decompress = auto_decompress |
362 | 372 | self._trust_env = trust_env |
@@ -484,7 +494,11 @@ async def _request( |
484 | 494 | "data and json parameters can not be used at the same time" |
485 | 495 | ) |
486 | 496 | elif json is not None: |
487 | | - data = payload.JsonPayload(json, dumps=self._json_serialize) |
| 497 | + data = payload.JsonPayload( |
| 498 | + json, |
| 499 | + dumps=self._json_serialize, |
| 500 | + dumps_bytes=self._json_serialize_bytes, |
| 501 | + ) |
488 | 502 |
|
489 | 503 | redirects = 0 |
490 | 504 | history: List[ClientResponse] = [] |
@@ -1316,6 +1330,11 @@ def json_serialize(self) -> JSONEncoder: |
1316 | 1330 | """Json serializer callable""" |
1317 | 1331 | return self._json_serialize |
1318 | 1332 |
|
| 1333 | + @property |
| 1334 | + def json_serialize_bytes(self) -> JSONBytesEncoder: |
| 1335 | + """Json bytes serializer callable""" |
| 1336 | + return self._json_serialize_bytes |
| 1337 | + |
1319 | 1338 | @property |
1320 | 1339 | def connector_owner(self) -> bool: |
1321 | 1340 | """Should connector be closed on session closing""" |
|
0 commit comments