"""JSON exporter for HTTP entries.""" import json from http_log_explorer.models import HTTPEntry class JSONExporter: """Export HTTP entries to JSON format.""" def export(self, entries: list[HTTPEntry], indent: int = 2) -> str: """Export entries to JSON string. Args: entries: List of HTTPEntry objects indent: JSON indent level Returns: JSON string representation """ data = [entry.to_dict() for entry in entries] return json.dumps(data, indent=indent, default=str) def export_compact(self, entries: list[HTTPEntry]) -> str: """Export entries to compact JSON (no indent). Args: entries: List of HTTPEntry objects Returns: Compact JSON string """ data = [entry.to_dict() for entry in entries] return json.dumps(data, separators=(",", ":"), default=str) def save(self, entries: list[HTTPEntry], path: str, indent: int = 2) -> None: """Save entries to JSON file. Args: entries: List of HTTPEntry objects path: Output file path indent: JSON indent level """ with open(path, "w") as f: f.write(self.export(entries, indent)) def export_summary(self, entries: list[HTTPEntry]) -> str: """Export summary of entries (URL, method, status only). Args: entries: List of HTTPEntry objects Returns: JSON string with summary info """ summary = [] for entry in entries: summary.append({ "id": entry.id, "method": entry.request.method, "url": entry.request.url, "status": entry.response.status, "content_type": entry.content_type, "duration_ms": entry.duration_ms, }) return json.dumps(summary, indent=2)