Add formatters module
This commit is contained in:
23
src/contextgen/formatters/json_formatter.py
Normal file
23
src/contextgen/formatters/json_formatter.py
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
"""JSON formatter for structured output."""
|
||||||
|
|
||||||
|
import json
|
||||||
|
from pathlib import Path
|
||||||
|
from typing import Any
|
||||||
|
|
||||||
|
|
||||||
|
class JSONFormatter:
|
||||||
|
"""Formats context data as JSON."""
|
||||||
|
|
||||||
|
def __init__(self, output_path: Path | None = None):
|
||||||
|
self.output_path = output_path
|
||||||
|
|
||||||
|
def format(self, data: dict[str, Any]) -> str:
|
||||||
|
"""Format data as JSON string."""
|
||||||
|
return json.dumps(data, indent=2, ensure_ascii=False)
|
||||||
|
|
||||||
|
def save(self, data: dict[str, Any]) -> None:
|
||||||
|
"""Save formatted data to file."""
|
||||||
|
if self.output_path:
|
||||||
|
self.output_path.parent.mkdir(parents=True, exist_ok=True)
|
||||||
|
content = self.format(data)
|
||||||
|
self.output_path.write_text(content, encoding="utf-8")
|
||||||
Reference in New Issue
Block a user