MCPcopy
hub / github.com/encode/starlette / send_json

Method send_json

starlette/websockets.py:171–178  ·  starlette/websockets.py::WebSocket.send_json
(self, data: Any, mode: str = "text")

Source from the content-addressed store, hash-verified

169 await self.send({class="st">"type": class="st">"websocket.send", class="st">"bytes": data})
170
171 async def send_json(self, data: Any, mode: str = class="st">"text") -> None:
172 if mode not in {class="st">"text", class="st">"binary"}:
173 raise RuntimeError(&class="cm">#x27;The class="st">"mode" argument should be class="st">"text" or class="st">"binary".')
174 text = json.dumps(data, separators=(class="st">",", class="st">":"), ensure_ascii=False)
175 if mode == class="st">"text":
176 await self.send({class="st">"type": class="st">"websocket.send", class="st">"text": text})
177 else:
178 await self.send({class="st">"type": class="st">"websocket.send", class="st">"bytes": text.encode(class="st">"utf-8")})
179
180 async def close(self, code: int = 1000, reason: str | None = None) -> None:
181 await self.send({class="st">"type": class="st">"websocket.close", class="st">"code": code, class="st">"reason": reason or class="st">""})

Calls 1

sendMethod · 0.95