Skip to content

Commit df73a3b

Browse files
authored
fix #134 hotfix decode error (#135)
* fix #134 hotfix decode error * fix pre-commit * update * loose version
1 parent 4aea647 commit df73a3b

File tree

6 files changed

+38
-21
lines changed

6 files changed

+38
-21
lines changed

.pre-commit-config.yaml

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -45,24 +45,15 @@ repos:
4545
- id: codespell
4646
types_or: [python, markdown]
4747

48-
- repo: local
48+
- repo: https://github.com/pre-commit/mirrors-mypy
49+
rev: v1.8.0
4950
hooks:
5051
- id: mypy
5152
name: mypy
5253
entry: mypy
53-
language: system
5454
types: [python]
55-
exclude: migrations/|commands/|scripts/
56-
args:
57-
[
58-
--pretty,
59-
--show-error-codes,
60-
--implicit-optional,
61-
--follow-imports=silent,
62-
--warn-redundant-casts,
63-
--warn-unused-ignores,
64-
--disallow-any-generics,
65-
--check-untyped-defs,
66-
--no-implicit-reexport,
67-
--disallow-untyped-defs,
68-
]
55+
exclude: migrations/|commands/|sandbox/|samples|sdk
56+
additional_dependencies: [pytest, syrupy, json5]
57+
args: [
58+
"--config-file=pyproject.toml"
59+
]

poetry.lock

Lines changed: 13 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ classifiers = [
2424

2525
[tool.poetry.dependencies]
2626
python = "^3.10"
27+
json5 = "*"
2728

2829
[tool.poetry.group.dev]
2930
optional = true

src/fuzzy_json/decoder.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
from functools import wraps
33
from typing import Any, Callable
44

5+
import json5
6+
57

68
def state(fn: Callable[[str, list[str]], str | None]) -> Callable[[str, list[str]], str]:
79
@wraps(fn)
@@ -288,10 +290,17 @@ def repair_json(json_str: str) -> str:
288290
return state_start(json_str)
289291

290292

291-
def loads(json_str: str, auto_repair: bool = True) -> dict[str, Any]:
293+
def base_loads(json_str: str) -> dict[str, Any]:
292294
try:
295+
return json5.loads(json_str)
296+
except Exception:
293297
return json.loads(json_str, strict=False)
294-
except json.decoder.JSONDecodeError:
298+
299+
300+
def loads(json_str: str, auto_repair: bool = True) -> dict[str, Any]:
301+
try:
302+
return base_loads(json_str)
303+
except Exception:
295304
if not auto_repair:
296305
raise
297306

@@ -300,4 +309,4 @@ def loads(json_str: str, auto_repair: bool = True) -> dict[str, Any]:
300309
except Exception as e:
301310
raise json.decoder.JSONDecodeError(f"Failed to repair JSON: {e}", json_str, 0)
302311

303-
return json.loads(repaired_json, strict=False)
312+
return base_loads(repaired_json)
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"highlighted_sentence": "to choose \"a candidate who can <em>defeat Donald Trump</em> in November.\"",
3+
"keyword": "defeat Donald Trump"
4+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"keyword": "defeat Donald Trump", "highlighted_sentence": 'to choose "a candidate who can <em>defeat Donald Trump</em> in November."'}

0 commit comments

Comments
 (0)