Improve JSON detection: strip both ends, check matching brackets, use full content
This commit is contained in:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user