Improve JSON detection: strip both ends, check matching brackets, use full content

This commit is contained in:
2026-04-01 23:51:41 -07:00
parent c3a71c2ff9
commit 217feab7de

View File

@@ -90,10 +90,13 @@ def detect_type(
return EXTENSION_MAP[suffix]
if content is not None:
stripped = content.lstrip()
if stripped and stripped[0] in ("{", "["):
stripped = content.strip()
if stripped and (
(stripped[0] == "{" and stripped[-1] == "}")
or (stripped[0] == "[" and stripped[-1] == "]")
):
try:
json.loads(stripped[:1000])
json.loads(stripped)
return "json"
except (json.JSONDecodeError, ValueError):
pass