Initial upload: ConfigConvert CLI with full test suite and CI/CD
This commit is contained in:
32
config_convert/converters/json_converter.py
Normal file
32
config_convert/converters/json_converter.py
Normal file
@@ -0,0 +1,32 @@
|
||||
"""JSON format converter using stdlib json."""
|
||||
|
||||
import json
|
||||
from pathlib import Path
|
||||
from typing import Any, Dict, Optional
|
||||
|
||||
from config_convert.converters.base import Converter
|
||||
|
||||
|
||||
class JSONConverter(Converter):
|
||||
"""Converter for JSON format."""
|
||||
|
||||
@property
|
||||
def name(self) -> str:
|
||||
return "json"
|
||||
|
||||
def loads(self, data: str) -> Dict[str, Any]:
|
||||
return json.loads(data)
|
||||
|
||||
def dumps(self, data: Dict[str, Any], indent: Optional[int] = None) -> str:
|
||||
if indent is None:
|
||||
indent = 2
|
||||
return json.dumps(data, indent=indent, ensure_ascii=False)
|
||||
|
||||
def load(self, file_path: str) -> Dict[str, Any]:
|
||||
with open(file_path, "r", encoding="utf-8") as f:
|
||||
return json.load(f)
|
||||
|
||||
def dump(self, data: Dict[str, Any], file_path: str, indent: Optional[int] = None) -> None:
|
||||
Path(file_path).parent.mkdir(parents=True, exist_ok=True)
|
||||
with open(file_path, "w", encoding="utf-8") as f:
|
||||
json.dump(data, f, indent=indent, ensure_ascii=False)
|
||||
Reference in New Issue
Block a user