diff --git a/config_converter/converters/json_converter.py b/config_converter/converters/json_converter.py index b77e0e4..0a48dfb 100644 --- a/config_converter/converters/json_converter.py +++ b/config_converter/converters/json_converter.py @@ -2,7 +2,7 @@ import json from pathlib import Path -from typing import Any, Dict +from typing import Any, Dict, Union from config_converter.converters.base import BaseConverter, ConversionError @@ -13,11 +13,12 @@ class JsonConverter(BaseConverter): FORMAT_NAME = "json" FILE_EXTENSIONS = ["json"] - def read(self, source: str | Path) -> Dict[str, Any]: + def read(self, source: Union[str, Path]) -> Dict[str, Any]: """Read and parse a JSON configuration file.""" try: with open(source, "r", encoding="utf-8") as f: - return json.load(f) + data = json.load(f) + return data if data is not None else {} except json.JSONDecodeError as e: raise ConversionError(f"Invalid JSON in {source}: {e}") from e except FileNotFoundError as e: @@ -25,7 +26,7 @@ class JsonConverter(BaseConverter): except PermissionError as e: raise ConversionError(f"Permission denied: {source}") from e - def write(self, data: Dict[str, Any], target: str | Path) -> None: + def write(self, data: Dict[str, Any], target: Union[str, Path]) -> None: """Write configuration data to a JSON file.""" try: with open(target, "w", encoding="utf-8") as f: @@ -36,7 +37,8 @@ class JsonConverter(BaseConverter): def parse(self, content: str) -> Dict[str, Any]: """Parse JSON content from a string.""" try: - return json.loads(content) + data = json.loads(content) + return data if data is not None else {} except json.JSONDecodeError as e: raise ConversionError(f"Invalid JSON content: {e}") from e