feat: 添加性能优化和架构文档,更新依赖和核心模块
refactor(browser_manager): 实现页面池机制以提升性能 refactor(image_manager): 添加模板缓存并集成页面池 refactor(bili_parser): 迁移到异步HTTP请求并实现会话复用 docs: 新增性能优化、架构设计和最佳实践文档 chore: 更新requirements.txt添加新依赖
This commit is contained in:
34
core/utils/json_utils.py
Normal file
34
core/utils/json_utils.py
Normal file
@@ -0,0 +1,34 @@
|
||||
"""
|
||||
JSON 工具模块
|
||||
|
||||
统一使用高性能的 orjson 库进行 JSON 序列化和反序列化。
|
||||
如果 orjson 不可用,则回退到标准库 json。
|
||||
"""
|
||||
from typing import Any, Union
|
||||
import json
|
||||
|
||||
# 在模块加载时检查 orjson 是否可用
|
||||
try:
|
||||
import orjson
|
||||
_orjson_available = True
|
||||
except ImportError:
|
||||
_orjson_available = False
|
||||
|
||||
def dumps(obj: Any) -> str:
|
||||
"""
|
||||
将对象序列化为 JSON 字符串。
|
||||
"""
|
||||
if _orjson_available:
|
||||
# orjson.dumps 返回 bytes,需要 decode
|
||||
return orjson.dumps(obj).decode("utf-8")
|
||||
else:
|
||||
return json.dumps(obj, ensure_ascii=False)
|
||||
|
||||
def loads(json_str: Union[str, bytes]) -> Any:
|
||||
"""
|
||||
将 JSON 字符串反序列化为对象。
|
||||
"""
|
||||
if _orjson_available:
|
||||
return orjson.loads(json_str)
|
||||
else:
|
||||
return json.loads(json_str)
|
||||
Reference in New Issue
Block a user