json-repair 0.40.0__tar.gz → 0.41.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (21) hide show
  1. {json_repair-0.40.0/src/json_repair.egg-info → json_repair-0.41.0}/PKG-INFO +3 -2
  2. {json_repair-0.40.0 → json_repair-0.41.0}/pyproject.toml +1 -1
  3. {json_repair-0.40.0 → json_repair-0.41.0}/src/json_repair/json_parser.py +4 -0
  4. json_repair-0.41.0/src/json_repair/object_comparer.py +63 -0
  5. {json_repair-0.40.0 → json_repair-0.41.0/src/json_repair.egg-info}/PKG-INFO +3 -2
  6. {json_repair-0.40.0 → json_repair-0.41.0}/src/json_repair.egg-info/SOURCES.txt +1 -0
  7. {json_repair-0.40.0 → json_repair-0.41.0}/tests/test_json_repair.py +1 -0
  8. {json_repair-0.40.0 → json_repair-0.41.0}/LICENSE +0 -0
  9. {json_repair-0.40.0 → json_repair-0.41.0}/README.md +0 -0
  10. {json_repair-0.40.0 → json_repair-0.41.0}/setup.cfg +0 -0
  11. {json_repair-0.40.0 → json_repair-0.41.0}/src/json_repair/__init__.py +0 -0
  12. {json_repair-0.40.0 → json_repair-0.41.0}/src/json_repair/__main__.py +0 -0
  13. {json_repair-0.40.0 → json_repair-0.41.0}/src/json_repair/json_context.py +0 -0
  14. {json_repair-0.40.0 → json_repair-0.41.0}/src/json_repair/json_repair.py +0 -0
  15. {json_repair-0.40.0 → json_repair-0.41.0}/src/json_repair/py.typed +0 -0
  16. {json_repair-0.40.0 → json_repair-0.41.0}/src/json_repair/string_file_wrapper.py +0 -0
  17. {json_repair-0.40.0 → json_repair-0.41.0}/src/json_repair.egg-info/dependency_links.txt +0 -0
  18. {json_repair-0.40.0 → json_repair-0.41.0}/src/json_repair.egg-info/entry_points.txt +0 -0
  19. {json_repair-0.40.0 → json_repair-0.41.0}/src/json_repair.egg-info/top_level.txt +0 -0
  20. {json_repair-0.40.0 → json_repair-0.41.0}/tests/test_coverage.py +0 -0
  21. {json_repair-0.40.0 → json_repair-0.41.0}/tests/test_performance.py +0 -0
@@ -1,6 +1,6 @@
1
- Metadata-Version: 2.2
1
+ Metadata-Version: 2.4
2
2
  Name: json_repair
3
- Version: 0.40.0
3
+ Version: 0.41.0
4
4
  Summary: A package to repair broken json strings
5
5
  Author-email: Stefano Baccianella <4247706+mangiucugna@users.noreply.github.com>
6
6
  License: MIT License
@@ -35,6 +35,7 @@ Classifier: Operating System :: OS Independent
35
35
  Requires-Python: >=3.9
36
36
  Description-Content-Type: text/markdown
37
37
  License-File: LICENSE
38
+ Dynamic: license-file
38
39
 
39
40
  [![PyPI](https://img.shields.io/pypi/v/json-repair)](https://pypi.org/project/json-repair/)
40
41
  ![Python version](https://img.shields.io/badge/python-3.9+-important)
@@ -3,7 +3,7 @@ requires = ["setuptools>=61.0"]
3
3
  build-backend = "setuptools.build_meta"
4
4
  [project]
5
5
  name = "json_repair"
6
- version = "0.40.0"
6
+ version = "0.41.0"
7
7
  license = {file = "LICENSE"}
8
8
  authors = [
9
9
  { name="Stefano Baccianella", email="4247706+mangiucugna@users.noreply.github.com" },
@@ -2,6 +2,7 @@ from typing import Any, Dict, List, Literal, Optional, TextIO, Tuple, Union
2
2
 
3
3
  from .json_context import ContextValues, JsonContext
4
4
  from .string_file_wrapper import StringFileWrapper
5
+ from .object_comparer import ObjectComparer
5
6
 
6
7
  JSONReturnType = Union[Dict[str, Any], List[Any], str, float, int, bool, None]
7
8
 
@@ -54,6 +55,9 @@ class JSONParser:
54
55
  while self.index < len(self.json_str):
55
56
  j = self.parse_json()
56
57
  if j != "":
58
+ if ObjectComparer.is_same_object(json[-1], j):
59
+ # replace the last entry with the new one since the new one seems an update
60
+ json.pop()
57
61
  json.append(j)
58
62
  if self.index == last_index:
59
63
  self.index += 1
@@ -0,0 +1,63 @@
1
+ from typing import Any
2
+
3
+
4
+ class ObjectComparer:
5
+ def __init__(self) -> None:
6
+ return
7
+
8
+ @staticmethod
9
+ def is_same_object(obj1: Any, obj2: Any, path: str = "") -> bool:
10
+ """
11
+ Recursively compares two objects and ensures that:
12
+ - Their types match
13
+ - Their keys/structure match
14
+ """
15
+ if type(obj1) is not type(obj2):
16
+ # Fail immediately if the types don't match
17
+ print(
18
+ f"Type mismatch at {path}: {type(obj1).__name__} vs {type(obj2).__name__}"
19
+ )
20
+ return False
21
+
22
+ if isinstance(obj1, dict) and isinstance(obj2, dict):
23
+ # Compare dictionary keys
24
+ keys1, keys2 = set(obj1.keys()), set(obj2.keys())
25
+ common_keys = keys1 & keys2
26
+ extra_keys1 = keys1 - keys2
27
+ extra_keys2 = keys2 - keys1
28
+
29
+ if extra_keys1:
30
+ print(f"Extra keys in first object at {path}: {extra_keys1}")
31
+ return False
32
+ if extra_keys2:
33
+ print(f"Extra keys in second object at {path}: {extra_keys2}")
34
+ return False
35
+
36
+ # Recursively compare the common keys
37
+ for key in common_keys:
38
+ if not ObjectComparer.is_same_object(
39
+ obj1[key], obj2[key], path=f"{path}/{key}"
40
+ ):
41
+ return False
42
+
43
+ elif isinstance(obj1, list) and isinstance(obj2, list):
44
+ # Compare lists
45
+ min_length = min(len(obj1), len(obj2))
46
+ if len(obj1) != len(obj2):
47
+ print(f"Length mismatch at {path}: {len(obj1)} vs {len(obj2)}")
48
+ return False
49
+
50
+ for i in range(min_length):
51
+ if not ObjectComparer.is_same_object(
52
+ obj1[i], obj2[i], path=f"{path}[{i}]"
53
+ ):
54
+ return False
55
+
56
+ if len(obj1) > len(obj2):
57
+ print(f"Extra items in first list at {path}: {obj1[min_length:]}")
58
+ return False
59
+ elif len(obj2) > len(obj1):
60
+ print(f"Extra items in second list at {path}: {obj2[min_length:]}")
61
+ return False
62
+
63
+ return True
@@ -1,6 +1,6 @@
1
- Metadata-Version: 2.2
1
+ Metadata-Version: 2.4
2
2
  Name: json_repair
3
- Version: 0.40.0
3
+ Version: 0.41.0
4
4
  Summary: A package to repair broken json strings
5
5
  Author-email: Stefano Baccianella <4247706+mangiucugna@users.noreply.github.com>
6
6
  License: MIT License
@@ -35,6 +35,7 @@ Classifier: Operating System :: OS Independent
35
35
  Requires-Python: >=3.9
36
36
  Description-Content-Type: text/markdown
37
37
  License-File: LICENSE
38
+ Dynamic: license-file
38
39
 
39
40
  [![PyPI](https://img.shields.io/pypi/v/json-repair)](https://pypi.org/project/json-repair/)
40
41
  ![Python version](https://img.shields.io/badge/python-3.9+-important)
@@ -6,6 +6,7 @@ src/json_repair/__main__.py
6
6
  src/json_repair/json_context.py
7
7
  src/json_repair/json_parser.py
8
8
  src/json_repair/json_repair.py
9
+ src/json_repair/object_comparer.py
9
10
  src/json_repair/py.typed
10
11
  src/json_repair/string_file_wrapper.py
11
12
  src/json_repair.egg-info/PKG-INFO
@@ -199,6 +199,7 @@ def test_multiple_jsons():
199
199
  assert repair_json("{}[]{}") == "[{}, [], {}]"
200
200
  assert repair_json('{"key":"value"}[1,2,3,True]') == '[{"key": "value"}, [1, 2, 3, true]]'
201
201
  assert repair_json('lorem ```json {"key":"value"} ``` ipsum ```json [1,2,3,True] ``` 42') == '[{"key": "value"}, [1, 2, 3, true]]'
202
+ assert repair_json('[{"key":"value"}][{"key":"value_after"}]') == '[{"key": "value_after"}]'
202
203
 
203
204
  def test_repair_json_with_objects():
204
205
  # Test with valid JSON strings
File without changes
File without changes
File without changes