全面更换orjson

This commit is contained in:
雅诺狐
2025-08-26 14:20:26 +08:00
parent bfabf896b5
commit 4a44ba9866
45 changed files with 991 additions and 1279 deletions

View File

@@ -1,4 +1,4 @@
import json
import orjson
import os
import signal
from concurrent.futures import ThreadPoolExecutor, as_completed
@@ -74,8 +74,8 @@ def process_single_text(pg_hash, raw_data):
# 存在对应的提取结果
logger.info(f"找到缓存的提取结果:{pg_hash}")
with open(temp_file_path, "r", encoding="utf-8") as f:
return json.load(f), None
except json.JSONDecodeError:
return orjson.loads(f.read()), None
except orjson.JSONDecodeError:
# 如果JSON文件损坏删除它并重新处理
logger.warning(f"缓存文件损坏,重新处理:{pg_hash}")
os.remove(temp_file_path)
@@ -97,7 +97,7 @@ def process_single_text(pg_hash, raw_data):
with file_lock:
try:
with open(temp_file_path, "w", encoding="utf-8") as f:
json.dump(doc_item, f, ensure_ascii=False, indent=4)
f.write(orjson.dumps(doc_item, option=orjson.OPT_INDENT_2).decode('utf-8'))
except Exception as e:
logger.error(f"保存缓存文件失败:{pg_hash}, 错误:{e}")
# 如果保存失败,确保不会留下损坏的文件
@@ -199,12 +199,12 @@ def main(): # sourcery skip: comprehension-to-generator, extract-method
filename = now.strftime("%m-%d-%H-%S-openie.json")
output_path = os.path.join(OPENIE_OUTPUT_DIR, filename)
with open(output_path, "w", encoding="utf-8") as f:
json.dump(
f.write(
orjson.dumps(
openie_obj.to_dict() if hasattr(openie_obj, "to_dict") else openie_obj.__dict__,
f,
ensure_ascii=False,
indent=4,
)
option=orjson.OPT_INDENT_2
).decode('utf-8')
)
logger.info(f"信息提取结果已保存到: {output_path}")
else:
logger.warning("没有可保存的信息提取结果")

View File

@@ -1,6 +1,6 @@
import tkinter as tk
from tkinter import ttk, messagebox, filedialog, colorchooser
import json
import orjson
from pathlib import Path
import threading
import toml
@@ -199,7 +199,7 @@ class LogFormatter:
parts.append(event)
elif isinstance(event, dict):
try:
parts.append(json.dumps(event, ensure_ascii=False, indent=None))
parts.append(orjson.dumps(event).decode('utf-8'))
except (TypeError, ValueError):
parts.append(str(event))
else:
@@ -212,7 +212,7 @@ class LogFormatter:
if key not in ("timestamp", "level", "logger_name", "event"):
if isinstance(value, (dict, list)):
try:
value_str = json.dumps(value, ensure_ascii=False, indent=None)
value_str = orjson.dumps(value).decode('utf-8')
except (TypeError, ValueError):
value_str = str(value)
else:
@@ -428,10 +428,10 @@ class AsyncLogLoader:
# 处理这批数据
for line in lines:
try:
log_entry = json.loads(line.strip())
log_entry = orjson.loads(line.strip())
log_index.add_entry(line_count, log_entry)
line_count += 1
except json.JSONDecodeError:
except orjson.JSONDecodeError:
continue
# 更新进度
@@ -844,7 +844,7 @@ class LogViewer:
if mapping_file.exists():
try:
with open(mapping_file, "r", encoding="utf-8") as f:
custom_mapping = json.load(f)
custom_mapping = orjson.loads(f.read())
self.module_name_mapping.update(custom_mapping)
except Exception as e:
print(f"加载模块映射失败: {e}")
@@ -855,7 +855,10 @@ class LogViewer:
mapping_file.parent.mkdir(exist_ok=True)
try:
with open(mapping_file, "w", encoding="utf-8") as f:
json.dump(self.module_name_mapping, f, ensure_ascii=False, indent=2)
f.write(orjson.dumps(
self.module_name_mapping,
option=orjson.OPT_INDENT_2
).decode('utf-8'))
except Exception as e:
print(f"保存模块映射失败: {e}")
@@ -1178,7 +1181,7 @@ class LogViewer:
for line in f:
if line.strip():
try:
log_entry = json.loads(line)
log_entry = orjson.loads(line)
self.log_index.add_entry(line_count, log_entry)
new_entries.append(log_entry)
@@ -1187,7 +1190,7 @@ class LogViewer:
new_modules.add(logger_name)
line_count += 1
except json.JSONDecodeError:
except orjson.JSONDecodeError:
continue
# 如果发现了新模块,在主线程中更新模块集合

View File

@@ -7,7 +7,7 @@
import os
import sys
import argparse
import json
import orjson
from pathlib import Path
from src.common.logger import get_logger
from src.plugin_system.utils.manifest_utils import (
@@ -51,7 +51,10 @@ def create_minimal_manifest(plugin_dir: str, plugin_name: str, description: str
try:
with open(manifest_path, "w", encoding="utf-8") as f:
json.dump(minimal_manifest, f, ensure_ascii=False, indent=2)
f.write(orjson.dumps(
minimal_manifest,
option=orjson.OPT_INDENT_2
).decode('utf-8'))
print(f"✅ 已创建最小化manifest文件: {manifest_path}")
return True
except Exception as e:
@@ -99,7 +102,10 @@ def create_complete_manifest(plugin_dir: str, plugin_name: str) -> bool:
try:
with open(manifest_path, "w", encoding="utf-8") as f:
json.dump(complete_manifest, f, ensure_ascii=False, indent=2)
f.write(orjson.dumps(
complete_manifest,
option=orjson.OPT_INDENT_2
).decode('utf-8'))
print(f"✅ 已创建完整manifest模板: {manifest_path}")
print("💡 请根据实际情况修改manifest文件中的内容")
return True
@@ -125,7 +131,7 @@ def validate_manifest_file(plugin_dir: str) -> bool:
try:
with open(manifest_path, "r", encoding="utf-8") as f:
manifest_data = json.load(f)
manifest_data = orjson.loads(f.read())
validator = ManifestValidator()
is_valid = validator.validate_manifest(manifest_data)
@@ -141,7 +147,7 @@ def validate_manifest_file(plugin_dir: str) -> bool:
return is_valid
except json.JSONDecodeError as e:
except orjson.JSONDecodeError as e:
print(f"❌ Manifest文件格式错误: {e}")
return False
except Exception as e:

View File

@@ -1,5 +1,5 @@
import os
import json
import orjson
import sys # 新增系统模块导入
# import time
@@ -369,7 +369,7 @@ class MongoToSQLiteMigrator:
if field_type in ["CharField", "TextField"]:
if isinstance(value, (list, dict)):
return json.dumps(value, ensure_ascii=False)
return orjson.dumps(value, ensure_ascii=False)
return str(value) if value is not None else ""
elif field_type == "IntegerField":
@@ -895,7 +895,7 @@ class MongoToSQLiteMigrator:
try:
with open(filepath, "w", encoding="utf-8") as f:
json.dump(error_report, f, ensure_ascii=False, indent=2)
orjson.dumps(error_report, f, ensure_ascii=False, indent=2)
logger.info(f"错误报告已导出到: {filepath}")
except Exception as e:
logger.error(f"导出错误报告失败: {e}")