hatchimera / src /buddy_fusion /json_utils.py
arkai2025's picture
feat: Build Buddy — voxel pet fusion (AI genetics engine)
357e825
Raw
History Blame Contribute Delete
716 Bytes
from __future__ import annotations
import json
from typing import Any
def extract_json_object(text: str) -> str | None:
decoder = json.JSONDecoder()
for index, char in enumerate(text):
if char != "{":
continue
try:
_, end = decoder.raw_decode(text[index:])
except json.JSONDecodeError:
continue
return text[index : index + end]
return None
def parse_json_object(text: str) -> dict[str, Any] | None:
raw = extract_json_object(text.strip())
if raw is None:
return None
try:
value = json.loads(raw)
except json.JSONDecodeError:
return None
return value if isinstance(value, dict) else None