Skip to content

Commit

Permalink
fix #18 use strict=False
Browse files Browse the repository at this point in the history
  • Loading branch information
lucemia committed Oct 23, 2023
1 parent 431b8ee commit 49c02cb
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 4 deletions.
4 changes: 2 additions & 2 deletions src/fuzzy_json/decoder.py
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ def repair_json(json_str: str) -> str:

def loads(json_str: str, auto_repair: bool = False) -> dict[str, Any]:
try:
return json.loads(json_str)
return json.loads(json_str, strict=False)
except json.decoder.JSONDecodeError:
if not auto_repair:
raise
Expand All @@ -282,4 +282,4 @@ def loads(json_str: str, auto_repair: bool = False) -> dict[str, Any]:
except Exception as e:
raise json.decoder.JSONDecodeError(f"Failed to repair JSON: {e}", json_str, 0)

return json.loads(repaired_json)
return json.loads(repaired_json, strict=False)
8 changes: 8 additions & 0 deletions src/fuzzy_json/tests/__snapshots__/test_decoder.ambr
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,14 @@
"subject's": 'Introducing the Mandelic Acid and Allantoin Acne-Care Calming Ampoule',
})
# ---
# name: test_repaired_json_invalid_case_special
dict({
'a': '''


''',
})
# ---
# name: test_repaired_json_simple_case
dict({
})
Expand Down
11 changes: 9 additions & 2 deletions src/fuzzy_json/tests/test_decoder.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
import pytest
from syrupy.assertion import SnapshotAssertion

from ..decoder import loads, repair_json
from ..decoder import loads


def load_repaired_json(json_str: str) -> dict[str, Any]:
return json.loads(repair_json(json_str))
return loads(json_str, auto_repair=True)


def test_repaired_json_simple_case(snapshot: SnapshotAssertion) -> None:
Expand Down Expand Up @@ -48,6 +48,13 @@ def test_repaired_json_invaild_case(snapshot: SnapshotAssertion, test_filename:
assert snapshot == result


def test_repaired_json_invalid_case_special(snapshot: SnapshotAssertion) -> None:
content = '{"a": "\n"}'
result = load_repaired_json(content)
assert snapshot == result
assert json.loads(content, strict=False) == result


def test_repair_json_fail() -> None:
with pytest.raises(json.decoder.JSONDecodeError) as _:
# test that it will raise JSONDecodeError if it can't fix the JSON
Expand Down

0 comments on commit 49c02cb

Please sign in to comment.