Read a JSON file and return its contents as a dictionary. Args: file_path: Path to the JSON file Returns: Dictionary containing the JSON data Raises: FileNotFoundError: If the file doesn't exist json.JSONDecodeError: If the f
(file_path: Union[str, Path])
| 27 | |
| 28 | @staticmethod |
| 29 | def read_json(file_path: Union[str, Path]) -> Dict[str, Any]: |
| 30 | """Read a JSON file and return its contents as a dictionary. |
| 31 | |
| 32 | Args: |
| 33 | file_path: Path to the JSON file |
| 34 | |
| 35 | Returns: |
| 36 | Dictionary containing the JSON data |
| 37 | |
| 38 | Raises: |
| 39 | FileNotFoundError: If the file doesn't exist |
| 40 | json.JSONDecodeError: If the file contains invalid JSON |
| 41 | """ |
| 42 | file_path = Path(file_path) |
| 43 | try: |
| 44 | with file_path.open('r', encoding='utf-8') as f: |
| 45 | return json.load(f) |
| 46 | except FileNotFoundError: |
| 47 | logger.error(f'File not found: {file_path}') |
| 48 | raise |
| 49 | except json.JSONDecodeError as e: |
| 50 | logger.error(f'Invalid JSON in file {file_path}: {str(e)}') |
| 51 | raise |
| 52 | |
| 53 | @staticmethod |
| 54 | def read_jsonl(file_path: Union[str, Path]) -> List[Dict[str, Any]]: |