48 lines
1.5 KiB
Python
48 lines
1.5 KiB
Python
from typing import Any, Dict, List
|
|
import json
|
|
|
|
from modules.datamodels.datamodelExtraction import ContentPart
|
|
from ..subUtils import makeId
|
|
from ..subRegistry import Extractor
|
|
|
|
|
|
class JsonExtractor(Extractor):
|
|
"""
|
|
Extractor for JSON files.
|
|
|
|
Supported formats:
|
|
- MIME types: application/json
|
|
- File extensions: .json
|
|
- Special handling: Validates JSON format, falls back to text if invalid
|
|
"""
|
|
|
|
def detect(self, fileName: str, mimeType: str, headBytes: bytes) -> bool:
|
|
return mimeType == "application/json" or (fileName or "").lower().endswith(".json")
|
|
|
|
def getSupportedExtensions(self) -> list[str]:
|
|
"""Return list of supported file extensions."""
|
|
return [".json"]
|
|
|
|
def getSupportedMimeTypes(self) -> list[str]:
|
|
"""Return list of supported MIME types."""
|
|
return ["application/json"]
|
|
|
|
def extract(self, fileBytes: bytes, context: Dict[str, Any]) -> List[ContentPart]:
|
|
mimeType = context.get("mimeType") or "application/json"
|
|
text = fileBytes.decode("utf-8", errors="replace")
|
|
# verify JSON is well-formed; fall back to text if not
|
|
try:
|
|
json.loads(text)
|
|
except Exception:
|
|
pass
|
|
return [ContentPart(
|
|
id=makeId(),
|
|
parentId=None,
|
|
label="main",
|
|
typeGroup="structure",
|
|
mimeType=mimeType,
|
|
data=text,
|
|
metadata={"size": len(fileBytes)}
|
|
)]
|
|
|
|
|