Skip to content

Commit

Permalink
fix: Raise FormatAutodetectionError when encountering non-pysubs2 J…
Browse files Browse the repository at this point in the history
…SON input file

Fixes issue #85.
  • Loading branch information
tkarabela committed May 4, 2024
1 parent 7589463 commit 362161b
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
2 changes: 1 addition & 1 deletion pysubs2/jsonformat.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class JSONFormat(FormatBase):
@classmethod
def guess_format(cls, text):
"""See :meth:`pysubs2.formats.FormatBase.guess_format()`"""
if text.startswith("{\""):
if text.startswith("{\"") and "\"info:\"" in text:
return "json"

@classmethod
Expand Down
15 changes: 14 additions & 1 deletion tests/test_json.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
from pysubs2 import SSAFile, SSAEvent, SSAStyle, Color
import pytest
import os.path as op
from pysubs2 import SSAFile, SSAEvent, SSAStyle, Color, FormatAutodetectionError
import tempfile

def test_write_read():
subs = SSAFile()
Expand All @@ -15,3 +18,13 @@ def test_write_read():
subs2 = SSAFile.from_string(json_text, "json")

assert subs2.equals(subs)


def test_read_unsupported_json_issue_85():
with tempfile.TemporaryDirectory() as temp_dir:
path = op.join(temp_dir, "test.atpj")
with open(path, "w") as fp:
print("""{"some data": [1,2,3]}""", file=fp)

with pytest.raises(FormatAutodetectionError):
SSAFile.load(path)

0 comments on commit 362161b

Please sign in to comment.