feat: 将 JSON 处理库从 json 更改为 orjson,以提高性能和兼容性

This commit is contained in:
Windpicker-owo
2025-11-06 12:47:56 +08:00
parent cbe94f3fa8
commit de24580dec
18 changed files with 83 additions and 78 deletions

View File

@@ -7,7 +7,7 @@
"""
import atexit
import json
import orjson
import os
import threading
from typing import Any, ClassVar
@@ -100,10 +100,10 @@ class PluginStorage:
if os.path.exists(self.file_path):
with open(self.file_path, encoding="utf-8") as f:
content = f.read()
self._data = json.loads(content) if content else {}
self._data = orjson.loads(content) if content else {}
else:
self._data = {}
except (json.JSONDecodeError, Exception) as e:
except (orjson.JSONDecodeError, Exception) as e:
logger.warning(f"'{self.file_path}' 加载数据失败: {e},将初始化为空数据。")
self._data = {}
@@ -125,7 +125,7 @@ class PluginStorage:
try:
with open(self.file_path, "w", encoding="utf-8") as f:
json.dump(self._data, f, indent=4, ensure_ascii=False)
f.write(orjson.dumps(self._data, option=orjson.OPT_INDENT_2 | orjson.OPT_NON_STR_KEYS).decode('utf-8'))
self._dirty = False # 保存后重置标志
logger.debug(f"插件 '{self.name}' 的数据已成功保存到磁盘。")
except Exception as e:

View File

@@ -5,7 +5,7 @@ MCP Client Manager
"""
import asyncio
import json
import orjson
import shutil
from pathlib import Path
from typing import Any
@@ -89,7 +89,7 @@ class MCPClientManager:
try:
with open(self.config_path, encoding="utf-8") as f:
config_data = json.load(f)
config_data = orjson.loads(f.read())
servers = {}
mcp_servers = config_data.get("mcpServers", {})
@@ -106,7 +106,7 @@ class MCPClientManager:
logger.info(f"成功加载 {len(servers)} 个 MCP 服务器配置")
return servers
except json.JSONDecodeError as e:
except orjson.JSONDecodeError as e:
logger.error(f"解析 MCP 配置文件失败: {e}")
return {}
except Exception as e:

View File

@@ -236,10 +236,10 @@ class ToolExecutor:
if isinstance(content, str):
result_preview = content
elif isinstance(content, list | dict):
import json
import orjson
try:
result_preview = json.dumps(content, ensure_ascii=False)
result_preview = orjson.dumps(content, option=orjson.OPT_NON_STR_KEYS).decode('utf-8')
except Exception:
result_preview = str(content)
else: