Detect format and return (format, parsed_data).
parsed_data is non-None only when the format is JSON, giving the
caller the already-parsed object so it can be injected into a
:class:SmeltContext without a redundant json.loads call.
Source code in packages/axm-smelt/src/axm_smelt/core/detector.py
| Python |
|---|
| def detect_format_parsed(text: str) -> tuple[Format, object | None]:
"""Detect format and return ``(format, parsed_data)``.
*parsed_data* is non-None only when the format is JSON, giving the
caller the already-parsed object so it can be injected into a
:class:`SmeltContext` without a redundant ``json.loads`` call.
"""
stripped = text.strip()
if not stripped:
return Format.TEXT, None
# JSON probe — capture the parsed object
if stripped[0] in ("{", "["):
try:
data = json.loads(stripped)
return Format.JSON, data
except (json.JSONDecodeError, ValueError):
pass
# Remaining probes (XML, YAML, Markdown) — no parsed data
for probe in (_try_xml, _try_yaml, _try_markdown):
result = probe(stripped)
if result is not None:
return result, None
return Format.TEXT, None
|