读取 JSON 文件 Args: filepath: 文件路径 Returns: JSON 数据,如果读取失败返回 None
(filepath: str)
| 338 | |
| 339 | |
| 340 | def read_json_file(filepath: str) -> Optional[Dict[str, Any]]: |
| 341 | """ |
| 342 | 读取 JSON 文件 |
| 343 | |
| 344 | Args: |
| 345 | filepath: 文件路径 |
| 346 | |
| 347 | Returns: |
| 348 | JSON 数据,如果读取失败返回 None |
| 349 | """ |
| 350 | try: |
| 351 | with open(filepath, 'r', encoding='utf-8') as f: |
| 352 | return json.load(f) |
| 353 | except (FileNotFoundError, json.JSONDecodeError, IOError) as e: |
| 354 | logging.getLogger(__name__).warning(f"读取 JSON 文件失败: {filepath} - {e}") |
| 355 | return None |
| 356 | |
| 357 | |
| 358 | def write_json_file(filepath: str, data: Dict[str, Any], indent: int = 2) -> bool: |