update
This commit is contained in:
@@ -1,8 +1,8 @@
|
||||
"""Toleranter JSON-Parser für KI-Output — als Text oder aus Dateien.
|
||||
"""Tolerant JSON parser for AI output — from text or from files.
|
||||
|
||||
Verkraftet Code-Fences, Drumherum-Text und unescapte Anführungszeichen in
|
||||
Strings (z. B. MiniMax: "Titel „p" geändert"): das letzte `"` vor der
|
||||
Fehlerstelle wird escapet und erneut geparst.
|
||||
Copes with code fences, surrounding prose and unescaped quotes inside
|
||||
strings (e.g. MiniMax: "Title „p" changed"): the last `"` before the
|
||||
error position is escaped and parsing is retried.
|
||||
"""
|
||||
|
||||
import json
|
||||
@@ -14,7 +14,7 @@ log = logging.getLogger("creator.jsonio")
|
||||
|
||||
|
||||
def parse_json_text(text: str):
|
||||
"""Parst JSON aus KI-Output; None bei nicht reparierbarem Input."""
|
||||
"""Parse JSON from AI output; None for input that can't be repaired."""
|
||||
text = re.sub(r"^```(?:json)?\s*|\s*```$", "", (text or "").strip())
|
||||
start, end = text.find("{"), text.rfind("}")
|
||||
if start == -1 or end <= start:
|
||||
@@ -36,14 +36,14 @@ def parse_json_text(text: str):
|
||||
|
||||
|
||||
def read_json_file(path: Path):
|
||||
"""Liest eine JSON-Datei mit derselben Toleranz; None bei fehlend/ungültig."""
|
||||
"""Read a JSON file with the same tolerance; None if missing/invalid."""
|
||||
if not path.exists():
|
||||
return None
|
||||
try:
|
||||
data = parse_json_text(path.read_text(encoding="utf-8"))
|
||||
except Exception as e:
|
||||
log.debug("JSON-Datei nicht lesbar: %s (%s)", path, e)
|
||||
log.debug("JSON file not readable: %s (%s)", path, e)
|
||||
return None
|
||||
if data is None:
|
||||
log.debug("JSON-Datei ungültig: %s", path)
|
||||
log.debug("JSON file invalid: %s", path)
|
||||
return data
|
||||
|
||||
Reference in New Issue
Block a user