From ac469dfcee5c73c14c97db33998e9de8129ca6fd Mon Sep 17 00:00:00 2001 From: dosisod <39638017+dosisod@users.noreply.github.com> Date: Thu, 27 Apr 2023 19:45:03 -0700 Subject: [PATCH] Minify JSON websocket data via `send_json` (#2128) Co-authored-by: Marcelo Trylesinski --- starlette/testclient.py | 2 +- starlette/websockets.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/starlette/testclient.py b/starlette/testclient.py index bdae83bf0..a66b901d6 100644 --- a/starlette/testclient.py +++ b/starlette/testclient.py @@ -145,7 +145,7 @@ def send_bytes(self, data: bytes) -> None: def send_json(self, data: typing.Any, mode: str = "text") -> None: assert mode in ["text", "binary"] - text = json.dumps(data) + text = json.dumps(data, separators=(",", ":")) if mode == "text": self.send({"type": "websocket.receive", "text": text}) else: diff --git a/starlette/websockets.py b/starlette/websockets.py index afcbde7fc..e89579235 100644 --- a/starlette/websockets.py +++ b/starlette/websockets.py @@ -168,7 +168,7 @@ async def send_bytes(self, data: bytes) -> None: async def send_json(self, data: typing.Any, mode: str = "text") -> None: if mode not in {"text", "binary"}: raise RuntimeError('The "mode" argument should be "text" or "binary".') - text = json.dumps(data) + text = json.dumps(data, separators=(",", ":")) if mode == "text": await self.send({"type": "websocket.send", "text": text}) else: