Skip to content

Commit

Permalink
Fix #47, a stupid bug was triggered because of missing tests during t…
Browse files Browse the repository at this point in the history
…he last refactor. Fixed the bug and updated tests
  • Loading branch information
mangiucugna committed May 26, 2024
1 parent 6bc9a03 commit 81580dc
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 4 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ requires = ["setuptools>=61.0"]
build-backend = "setuptools.build_meta"
[project]
name = "json_repair"
version = "0.20.0"
version = "0.20.1"
license = {file = "LICENSE"}
authors = [
{ name="Stefano Baccianella", email="4247706+mangiucugna@users.noreply.github.com" },
Expand Down
6 changes: 5 additions & 1 deletion src/json_repair/json_repair.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,10 +131,14 @@ def parse_object(self) -> Dict[str, Any]:
# Sometimes the string search might not move the index at all, that might lead us to an infinite loop
self.index += 1

self.skip_whitespaces_at()

# We reached the end here
if (self.get_char_at() or "}") == "}":
continue

self.skip_whitespaces_at()

# An extreme case of missing ":" after a key
if (self.get_char_at() or "") != ":":
self.log(
Expand Down Expand Up @@ -178,7 +182,7 @@ def parse_array(self) -> List[Any]:
value = self.parse_json()

# It is possible that parse_json() returns nothing valid, so we stop
if not value:
if value == "":
break

if value == "..." and self.get_char_at(-1) == ".":
Expand Down
5 changes: 3 additions & 2 deletions tests/test_json_repair.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def test_valid_json():
assert repair_json('{"key": "value\\nvalue"}') == '{"key": "value\\nvalue"}'

def test_brackets_edge_cases():
assert repair_json("[{]") == "[]"
assert repair_json("[{]") == "[{}]"
assert repair_json(" { } ") == "{}"
assert repair_json("[") == "[]"
assert repair_json("]") == '""'
Expand Down Expand Up @@ -83,6 +83,7 @@ def test_array_edge_cases():
assert repair_json("[1, 2, 3, ...]") == "[1, 2, 3]"
assert repair_json("[1, 2, ... , 3]") == "[1, 2, 3]"
assert repair_json("[1, 2, '...', 3]") == '[1, 2, "...", 3]'
assert repair_json("[true, false, null, ...]") == '[true, false, null]'
assert (
repair_json('{"employees":["John", "Anna",')
== '{"employees": ["John", "Anna"]}'
Expand Down Expand Up @@ -119,7 +120,7 @@ def test_object_edge_cases():
assert repair_json('{""answer"":[{""traits"":''Female aged 60+'',""answer1"":""5""}]}') == '{"answer": [{"traits": "Female aged 60+", "answer1": "5"}]}'
assert repair_json('{ "words": abcdef", "numbers": 12345", "words2": ghijkl" }') == '{"words": "abcdef", "numbers": 12345, "words2": "ghijkl"}'
assert repair_json('''{"number": 1,"reason": "According...""ans": "YES"}''') == '{"number": 1, "reason": "According...", "ans": "YES"}'
assert repair_json('''{ "a": "{ b": {} }" }''') == '{"a": "{ b"}'
assert repair_json('''{ "a" : "{ b": {} }" }''') == '{"a": "{ b"}'
assert repair_json("""{"b": "xxxxx" true}""") == '{"b": "xxxxx"}'
assert repair_json('{"key": "Lorem "ipsum" s,"}') == '{"key": "Lorem \\"ipsum\\" s,"}'

Expand Down

0 comments on commit 81580dc

Please sign in to comment.