json-repair 0.12.2__tar.gz → 0.13.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.
- {json_repair-0.12.2/src/json_repair.egg-info → json_repair-0.13.0}/PKG-INFO +28 -1
- {json_repair-0.12.2 → json_repair-0.13.0}/README.md +27 -0
- {json_repair-0.12.2 → json_repair-0.13.0}/pyproject.toml +1 -1
- {json_repair-0.12.2 → json_repair-0.13.0}/src/json_repair/__init__.py +2 -0
- {json_repair-0.12.2 → json_repair-0.13.0}/src/json_repair/json_repair.py +16 -2
- {json_repair-0.12.2 → json_repair-0.13.0/src/json_repair.egg-info}/PKG-INFO +28 -1
- {json_repair-0.12.2 → json_repair-0.13.0}/tests/test_json_repair.py +20 -2
- {json_repair-0.12.2 → json_repair-0.13.0}/tests/test_performance.py +1 -1
- {json_repair-0.12.2 → json_repair-0.13.0}/LICENSE +0 -0
- {json_repair-0.12.2 → json_repair-0.13.0}/setup.cfg +0 -0
- {json_repair-0.12.2 → json_repair-0.13.0}/src/json_repair.egg-info/SOURCES.txt +0 -0
- {json_repair-0.12.2 → json_repair-0.13.0}/src/json_repair.egg-info/dependency_links.txt +0 -0
- {json_repair-0.12.2 → json_repair-0.13.0}/src/json_repair.egg-info/top_level.txt +0 -0
@@ -1,6 +1,6 @@
|
|
1
1
|
Metadata-Version: 2.1
|
2
2
|
Name: json_repair
|
3
|
-
Version: 0.
|
3
|
+
Version: 0.13.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
|
@@ -65,6 +65,33 @@ or just
|
|
65
65
|
|
66
66
|
decoded_object = json_repair.repair_json(json_string, return_objects=True)
|
67
67
|
|
68
|
+
### Read json from a file or file descriptor
|
69
|
+
|
70
|
+
JSON repair provides also a drop-in replacement for `json.load()`:
|
71
|
+
|
72
|
+
import json_repair
|
73
|
+
|
74
|
+
try:
|
75
|
+
file_descriptor = open(fname, 'rb')
|
76
|
+
except OSError:
|
77
|
+
...
|
78
|
+
|
79
|
+
with file_descriptor:
|
80
|
+
decoded_object = json_repair.load(file_descriptor)
|
81
|
+
|
82
|
+
and another method to read from a file:
|
83
|
+
|
84
|
+
import json_repair
|
85
|
+
|
86
|
+
try:
|
87
|
+
decoded_object = json_repair.from_file(json_file)
|
88
|
+
except OSError:
|
89
|
+
...
|
90
|
+
except IOError:
|
91
|
+
...
|
92
|
+
|
93
|
+
Keep in mind that the library will not catch any IO-related exception and those will need to be managed by you
|
94
|
+
|
68
95
|
### Performance considerations
|
69
96
|
If you find this library too slow because is using `json.loads()` you can skip that by passing `skip_json_loads=True` to `repair_json`. Like:
|
70
97
|
|
@@ -28,6 +28,33 @@ or just
|
|
28
28
|
|
29
29
|
decoded_object = json_repair.repair_json(json_string, return_objects=True)
|
30
30
|
|
31
|
+
### Read json from a file or file descriptor
|
32
|
+
|
33
|
+
JSON repair provides also a drop-in replacement for `json.load()`:
|
34
|
+
|
35
|
+
import json_repair
|
36
|
+
|
37
|
+
try:
|
38
|
+
file_descriptor = open(fname, 'rb')
|
39
|
+
except OSError:
|
40
|
+
...
|
41
|
+
|
42
|
+
with file_descriptor:
|
43
|
+
decoded_object = json_repair.load(file_descriptor)
|
44
|
+
|
45
|
+
and another method to read from a file:
|
46
|
+
|
47
|
+
import json_repair
|
48
|
+
|
49
|
+
try:
|
50
|
+
decoded_object = json_repair.from_file(json_file)
|
51
|
+
except OSError:
|
52
|
+
...
|
53
|
+
except IOError:
|
54
|
+
...
|
55
|
+
|
56
|
+
Keep in mind that the library will not catch any IO-related exception and those will need to be managed by you
|
57
|
+
|
31
58
|
### Performance considerations
|
32
59
|
If you find this library too slow because is using `json.loads()` you can skip that by passing `skip_json_loads=True` to `repair_json`. Like:
|
33
60
|
|
@@ -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.
|
6
|
+
version = "0.13.0"
|
7
7
|
license = {file = "LICENSE"}
|
8
8
|
authors = [
|
9
9
|
{ name="Stefano Baccianella", email="4247706+mangiucugna@users.noreply.github.com" },
|
@@ -23,7 +23,7 @@ All supported use cases are in the unit tests
|
|
23
23
|
"""
|
24
24
|
|
25
25
|
import json
|
26
|
-
from typing import Any, Dict, List, Union
|
26
|
+
from typing import Any, Dict, List, Union, TextIO
|
27
27
|
|
28
28
|
|
29
29
|
class JSONParser:
|
@@ -127,7 +127,7 @@ class JSONParser:
|
|
127
127
|
self.context = ""
|
128
128
|
obj[key] = value
|
129
129
|
|
130
|
-
if (self.get_char_at() or "")
|
130
|
+
if (self.get_char_at() or "") in [",", "'", '"']:
|
131
131
|
self.index += 1
|
132
132
|
|
133
133
|
# Remove trailing spaces
|
@@ -351,3 +351,17 @@ def loads(
|
|
351
351
|
It is a wrapper around the `repair_json()` function with `return_objects=True`.
|
352
352
|
"""
|
353
353
|
return repair_json(json_str, True)
|
354
|
+
|
355
|
+
|
356
|
+
def load(fp: TextIO) -> Union[Dict[str, Any], List[Any], str, float, int, bool, None]:
|
357
|
+
return loads(fp.read())
|
358
|
+
|
359
|
+
|
360
|
+
def from_file(
|
361
|
+
filename: str,
|
362
|
+
) -> Union[Dict[str, Any], List[Any], str, float, int, bool, None]:
|
363
|
+
fd = open(filename)
|
364
|
+
jsonobj = load(fd)
|
365
|
+
fd.close()
|
366
|
+
|
367
|
+
return jsonobj
|
@@ -1,6 +1,6 @@
|
|
1
1
|
Metadata-Version: 2.1
|
2
2
|
Name: json_repair
|
3
|
-
Version: 0.
|
3
|
+
Version: 0.13.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
|
@@ -65,6 +65,33 @@ or just
|
|
65
65
|
|
66
66
|
decoded_object = json_repair.repair_json(json_string, return_objects=True)
|
67
67
|
|
68
|
+
### Read json from a file or file descriptor
|
69
|
+
|
70
|
+
JSON repair provides also a drop-in replacement for `json.load()`:
|
71
|
+
|
72
|
+
import json_repair
|
73
|
+
|
74
|
+
try:
|
75
|
+
file_descriptor = open(fname, 'rb')
|
76
|
+
except OSError:
|
77
|
+
...
|
78
|
+
|
79
|
+
with file_descriptor:
|
80
|
+
decoded_object = json_repair.load(file_descriptor)
|
81
|
+
|
82
|
+
and another method to read from a file:
|
83
|
+
|
84
|
+
import json_repair
|
85
|
+
|
86
|
+
try:
|
87
|
+
decoded_object = json_repair.from_file(json_file)
|
88
|
+
except OSError:
|
89
|
+
...
|
90
|
+
except IOError:
|
91
|
+
...
|
92
|
+
|
93
|
+
Keep in mind that the library will not catch any IO-related exception and those will need to be managed by you
|
94
|
+
|
68
95
|
### Performance considerations
|
69
96
|
If you find this library too slow because is using `json.loads()` you can skip that by passing `skip_json_loads=True` to `repair_json`. Like:
|
70
97
|
|
@@ -1,4 +1,4 @@
|
|
1
|
-
from src.json_repair.json_repair import repair_json
|
1
|
+
from src.json_repair.json_repair import from_file, repair_json
|
2
2
|
|
3
3
|
|
4
4
|
def test_repair_json():
|
@@ -109,6 +109,7 @@ def test_repair_json():
|
|
109
109
|
assert repair_json('{""answer"":[{""traits"":''Female aged 60+'',""answer1"":""5""}]}') == '{"answer": [{"traits": "Female aged 60+", "answer1": "5"}]}'
|
110
110
|
assert repair_json('{""answer":[{""traits":""Female aged 60+",""answer1":""5"}]}') == '{"answer": [{"traits": "Female aged 60+", "answer1": "5"}]}'
|
111
111
|
assert repair_json('{"key":"",}') == '{"key": ",}"}'
|
112
|
+
assert repair_json('{ "words": abcdef", "numbers": 12345", "words2": ghijkl" }') == '{"words": "abcdef", "numbers": 12345, "words2": "ghijkl"}'
|
112
113
|
|
113
114
|
|
114
115
|
|
@@ -239,4 +240,21 @@ def test_repair_json_skip_json_loads():
|
|
239
240
|
assert repair_json('{"key": true, "key2": false, "key3": null}', skip_json_loads=True) == '{"key": true, "key2": false, "key3": null}'
|
240
241
|
assert repair_json('{"key": true, "key2": false, "key3": null}', return_objects=True, skip_json_loads=True) == {"key": True, "key2": False, "key3": None}
|
241
242
|
assert repair_json('{"key": true, "key2": false, "key3": }', skip_json_loads=True) == '{"key": true, "key2": false, "key3": ""}'
|
242
|
-
assert repair_json('{"key": true, "key2": false, "key3": }', return_objects=True, skip_json_loads=True) == {"key": True, "key2": False, "key3": ""}
|
243
|
+
assert repair_json('{"key": true, "key2": false, "key3": }', return_objects=True, skip_json_loads=True) == {"key": True, "key2": False, "key3": ""}
|
244
|
+
|
245
|
+
def test_repair_json_from_file():
|
246
|
+
import os
|
247
|
+
import tempfile
|
248
|
+
|
249
|
+
# Create a temporary file
|
250
|
+
temp_fd, temp_path = tempfile.mkstemp(suffix=".json")
|
251
|
+
try:
|
252
|
+
# Write content to the temporary file
|
253
|
+
with os.fdopen(temp_fd, 'w') as tmp:
|
254
|
+
tmp.write("{")
|
255
|
+
|
256
|
+
assert(from_file(temp_path)) == {}
|
257
|
+
|
258
|
+
finally:
|
259
|
+
# Clean up - delete the temporary file
|
260
|
+
os.remove(temp_path)
|
@@ -580,7 +580,7 @@ def test_true_false_correct(benchmark):
|
|
580
580
|
mean_time = benchmark.stats.get("median")
|
581
581
|
|
582
582
|
# Define your time threshold in seconds
|
583
|
-
max_time =
|
583
|
+
max_time = 24 * (1 / 10 ** 6) # 24 microsecond
|
584
584
|
|
585
585
|
# Assert that the average time is below the threshold
|
586
586
|
assert mean_time < max_time, f"Benchmark exceeded threshold: {mean_time:.3f}s > {max_time:.3f}s"
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|