Skip to content

Commit

Permalink
fix #134 hotfix decode error (#135)
Browse files Browse the repository at this point in the history
* fix #134 hotfix decode error

* fix pre-commit

* update

* loose version
  • Loading branch information
lucemia authored Jul 23, 2024
1 parent 4aea647 commit df73a3b
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 21 deletions.
23 changes: 7 additions & 16 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -45,24 +45,15 @@ repos:
- id: codespell
types_or: [python, markdown]

- repo: local
- repo: https://github.com/pre-commit/mirrors-mypy
rev: v1.8.0
hooks:
- id: mypy
name: mypy
entry: mypy
language: system
types: [python]
exclude: migrations/|commands/|scripts/
args:
[
--pretty,
--show-error-codes,
--implicit-optional,
--follow-imports=silent,
--warn-redundant-casts,
--warn-unused-ignores,
--disallow-any-generics,
--check-untyped-defs,
--no-implicit-reexport,
--disallow-untyped-defs,
]
exclude: migrations/|commands/|sandbox/|samples|sdk
additional_dependencies: [pytest, syrupy, json5]
args: [
"--config-file=pyproject.toml"
]
15 changes: 13 additions & 2 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ classifiers = [

[tool.poetry.dependencies]
python = "^3.10"
json5 = "*"

[tool.poetry.group.dev]
optional = true
Expand Down
15 changes: 12 additions & 3 deletions src/fuzzy_json/decoder.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
from functools import wraps
from typing import Any, Callable

import json5


def state(fn: Callable[[str, list[str]], str | None]) -> Callable[[str, list[str]], str]:
@wraps(fn)
Expand Down Expand Up @@ -288,10 +290,17 @@ def repair_json(json_str: str) -> str:
return state_start(json_str)


def loads(json_str: str, auto_repair: bool = True) -> dict[str, Any]:
def base_loads(json_str: str) -> dict[str, Any]:
try:
return json5.loads(json_str)
except Exception:
return json.loads(json_str, strict=False)
except json.decoder.JSONDecodeError:


def loads(json_str: str, auto_repair: bool = True) -> dict[str, Any]:
try:
return base_loads(json_str)
except Exception:
if not auto_repair:
raise

Expand All @@ -300,4 +309,4 @@ def loads(json_str: str, auto_repair: bool = True) -> 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, strict=False)
return base_loads(repaired_json)
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"highlighted_sentence": "to choose \"a candidate who can <em>defeat Donald Trump</em> in November.\"",
"keyword": "defeat Donald Trump"
}
1 change: 1 addition & 0 deletions src/fuzzy_json/tests/test_data/invalid/case9.jsonx
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"keyword": "defeat Donald Trump", "highlighted_sentence": 'to choose "a candidate who can <em>defeat Donald Trump</em> in November."'}

0 comments on commit df73a3b

Please sign in to comment.