-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathjson_formatter.py
More file actions
35 lines (28 loc) · 869 Bytes
/
json_formatter.py
File metadata and controls
35 lines (28 loc) · 869 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
"""JSON output formatter."""
import json
from typing import Any
from dataclasses import asdict, is_dataclass
from .base import BaseFormatter
class JSONFormatter(BaseFormatter):
"""
Format results as JSON.
Provides clean, structured JSON output suitable for:
- API consumption
- Data processing
- Automation
"""
def format(self, result: Any) -> str:
"""Format result as JSON string."""
if hasattr(result, "to_dict"):
data = result.to_dict()
elif hasattr(result, "__dict__"):
if is_dataclass(result):
data = asdict(result)
else:
data = result.__dict__
else:
data = result
return json.dumps(data, indent=2, default=str)
def get_extension(self) -> str:
"""Get file extension."""
return ".json"