INNER CODE UNIT · Python
_json_safe
caiovicentino/polymarket-mcp-server · src/polymarket_mcp/web/app.py:553
def _json_safe(value):
"""Return a JSON-serializable copy of ``value``.
The /ws frames are serialized by Starlette's ``send_json`` (plain
``json.dumps``), which cannot encode datetimes (or any other
non-JSON-native object). The module-level ``stats`` dict carries
``uptime_start: datetime`` -- sending it raw crashes the endpoint on the
FIRST frame, killing every dashboard WebSocket with a reconnect loop
(V-WSDT, proven live 2026-09-18). ``default=str`` converts any such value
deterministically (datetime -> "YYYY-MM-DD HH:MM:SS.ffffff").
"""
return json.loads(json.dumps(value, default=str))
# ============================================================================
# WebSocket for Real-time Updates
# ============================================================================