修复缺少safe_decode_json

This commit is contained in:
nnwang
2025-12-12 19:18:40 +08:00
parent c6d9d4f093
commit 33ff81a15d

View File

@@ -48,6 +48,18 @@ class Room:
'created_at': self.created_at.isoformat()
}
def safe_decode_json(raw_body: bytes) -> Dict[Any, Any]:
"""
尝试用 UTF-8 解码,失败则尝试 GB18030兼容 GBK/GB2312
"""
for encoding in ['utf-8', 'gb18030', 'latin1']:
try:
text = raw_body.decode(encoding)
return json.loads(text)
except (UnicodeDecodeError, json.JSONDecodeError):
continue
raise ValueError("无法解码请求体为有效 JSON")
def get_frontend_to_client_queue(room_id: str) -> List[Dict[str, Any]]:
if room_id not in frontend_to_client_queues:
frontend_to_client_queues[room_id] = []