json-repair 0.12.3__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.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: json_repair
3
- Version: 0.12.3
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.12.3"
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" },
@@ -1,2 +1,4 @@
1
1
  from .json_repair import repair_json as repair_json
2
2
  from .json_repair import loads as loads
3
+ from .json_repair import loads as load
4
+ from .json_repair import loads as from_file
@@ -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:
@@ -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.12.3
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():
@@ -240,4 +240,21 @@ def test_repair_json_skip_json_loads():
240
240
  assert repair_json('{"key": true, "key2": false, "key3": null}', skip_json_loads=True) == '{"key": true, "key2": false, "key3": null}'
241
241
  assert repair_json('{"key": true, "key2": false, "key3": null}', return_objects=True, skip_json_loads=True) == {"key": True, "key2": False, "key3": None}
242
242
  assert repair_json('{"key": true, "key2": false, "key3": }', 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": ""}
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 = 23 * (1 / 10 ** 6) # 23 microsecond
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