json-repair 0.8.0__tar.gz → 0.8.1__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.8.0
3
+ Version: 0.8.1
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
@@ -49,26 +49,34 @@ I searched for a lightweight python package that was able to reliably fix this p
49
49
 
50
50
  # How to use
51
51
  from json_repair import repair_json
52
- try:
53
- good_json_string = repair_json(bad_json_string)
54
- except Exception:
55
- # Not even this library could fix this JSON
52
+
53
+ good_json_string = repair_json(bad_json_string)
54
+ # If the string was super broken this will return an empty string
56
55
 
57
56
  You can use this library to completely replace `json.loads()`:
58
57
 
59
58
  import json_repair
60
- try:
61
- decoded_object = json_repair.loads(json_string)
62
- except Exception:
63
- # Not even this library could fix this JSON
59
+
60
+ decoded_object = json_repair.loads(json_string)
64
61
 
65
62
  or just
66
63
 
67
64
  import json_repair
68
- try:
69
- decoded_object = json_repair.repair_json(json_string, return_objects=True)
70
- except Exception:
71
- # Not even this library could fix this JSON
65
+
66
+ decoded_object = json_repair.repair_json(json_string, return_objects=True)
67
+
68
+ ### Performance considerations
69
+ 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
+
71
+ from json_repair import repair_json
72
+
73
+ good_json_string = repair_json(bad_json_string, skip_json_loads=True)
74
+
75
+ I made a choice of not using any fast json library to avoid having any external dependency, so that anybody can use it regardless of their stack.
76
+
77
+ Some rules of thumb to use:
78
+ - Setting `return_objects=True` will always be faster because the parser returns an object already and it doesn't have serialize that object to JSON
79
+ - `skip_json_loads` is faster only if you 100% know that the string is not a valid JSON
72
80
 
73
81
  ## Adding to requirements
74
82
  **Please pin this library only on the major version!**
@@ -80,21 +88,6 @@ To ensure that you only pin the major version of this library in your `requireme
80
88
 
81
89
  In this example, any version that starts with `0.` will be acceptable, allowing for updates on minor and patch versions.
82
90
 
83
- ## Performance considerations
84
- 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:
85
-
86
- from json_repair import repair_json
87
- try:
88
- good_json_string = repair_json(bad_json_string, skip_json_loads=True)
89
- except Exception:
90
- # Not even this library could fix this JSON
91
-
92
- I made a choice of not using any fast json library to avoid having any external dependency, so that anybody can use it regardless of their stack.
93
-
94
- Some rules of thumb to use:
95
- - Setting `return_objects=True` will always be faster because the parser returns an object already and it doesn't have serialize that object to JSON
96
- - `skip_json_loads` is faster only if you 100% know that the string is not a valid JSON
97
-
98
91
  # How it works
99
92
  This module will parse the JSON file following the BNF definition:
100
93
 
@@ -12,26 +12,34 @@ I searched for a lightweight python package that was able to reliably fix this p
12
12
 
13
13
  # How to use
14
14
  from json_repair import repair_json
15
- try:
16
- good_json_string = repair_json(bad_json_string)
17
- except Exception:
18
- # Not even this library could fix this JSON
15
+
16
+ good_json_string = repair_json(bad_json_string)
17
+ # If the string was super broken this will return an empty string
19
18
 
20
19
  You can use this library to completely replace `json.loads()`:
21
20
 
22
21
  import json_repair
23
- try:
24
- decoded_object = json_repair.loads(json_string)
25
- except Exception:
26
- # Not even this library could fix this JSON
22
+
23
+ decoded_object = json_repair.loads(json_string)
27
24
 
28
25
  or just
29
26
 
30
27
  import json_repair
31
- try:
32
- decoded_object = json_repair.repair_json(json_string, return_objects=True)
33
- except Exception:
34
- # Not even this library could fix this JSON
28
+
29
+ decoded_object = json_repair.repair_json(json_string, return_objects=True)
30
+
31
+ ### Performance considerations
32
+ 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
+
34
+ from json_repair import repair_json
35
+
36
+ good_json_string = repair_json(bad_json_string, skip_json_loads=True)
37
+
38
+ I made a choice of not using any fast json library to avoid having any external dependency, so that anybody can use it regardless of their stack.
39
+
40
+ Some rules of thumb to use:
41
+ - Setting `return_objects=True` will always be faster because the parser returns an object already and it doesn't have serialize that object to JSON
42
+ - `skip_json_loads` is faster only if you 100% know that the string is not a valid JSON
35
43
 
36
44
  ## Adding to requirements
37
45
  **Please pin this library only on the major version!**
@@ -43,21 +51,6 @@ To ensure that you only pin the major version of this library in your `requireme
43
51
 
44
52
  In this example, any version that starts with `0.` will be acceptable, allowing for updates on minor and patch versions.
45
53
 
46
- ## Performance considerations
47
- 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:
48
-
49
- from json_repair import repair_json
50
- try:
51
- good_json_string = repair_json(bad_json_string, skip_json_loads=True)
52
- except Exception:
53
- # Not even this library could fix this JSON
54
-
55
- I made a choice of not using any fast json library to avoid having any external dependency, so that anybody can use it regardless of their stack.
56
-
57
- Some rules of thumb to use:
58
- - Setting `return_objects=True` will always be faster because the parser returns an object already and it doesn't have serialize that object to JSON
59
- - `skip_json_loads` is faster only if you 100% know that the string is not a valid JSON
60
-
61
54
  # How it works
62
55
  This module will parse the JSON file following the BNF definition:
63
56
 
@@ -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.8.0"
6
+ version = "0.8.1"
7
7
  license = {file = "LICENSE"}
8
8
  authors = [
9
9
  { name="Stefano Baccianella", email="4247706+mangiucugna@users.noreply.github.com" },
@@ -247,6 +247,9 @@ class JSONParser:
247
247
  if number_str:
248
248
  if "." in number_str or "e" in number_str or "E" in number_str:
249
249
  return float(number_str)
250
+ elif number_str == "-":
251
+ # If there is a stray "-" this will throw an exception, throw away this character
252
+ return self.parse_json()
250
253
  else:
251
254
  return int(number_str)
252
255
  else:
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: json_repair
3
- Version: 0.8.0
3
+ Version: 0.8.1
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
@@ -49,26 +49,34 @@ I searched for a lightweight python package that was able to reliably fix this p
49
49
 
50
50
  # How to use
51
51
  from json_repair import repair_json
52
- try:
53
- good_json_string = repair_json(bad_json_string)
54
- except Exception:
55
- # Not even this library could fix this JSON
52
+
53
+ good_json_string = repair_json(bad_json_string)
54
+ # If the string was super broken this will return an empty string
56
55
 
57
56
  You can use this library to completely replace `json.loads()`:
58
57
 
59
58
  import json_repair
60
- try:
61
- decoded_object = json_repair.loads(json_string)
62
- except Exception:
63
- # Not even this library could fix this JSON
59
+
60
+ decoded_object = json_repair.loads(json_string)
64
61
 
65
62
  or just
66
63
 
67
64
  import json_repair
68
- try:
69
- decoded_object = json_repair.repair_json(json_string, return_objects=True)
70
- except Exception:
71
- # Not even this library could fix this JSON
65
+
66
+ decoded_object = json_repair.repair_json(json_string, return_objects=True)
67
+
68
+ ### Performance considerations
69
+ 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
+
71
+ from json_repair import repair_json
72
+
73
+ good_json_string = repair_json(bad_json_string, skip_json_loads=True)
74
+
75
+ I made a choice of not using any fast json library to avoid having any external dependency, so that anybody can use it regardless of their stack.
76
+
77
+ Some rules of thumb to use:
78
+ - Setting `return_objects=True` will always be faster because the parser returns an object already and it doesn't have serialize that object to JSON
79
+ - `skip_json_loads` is faster only if you 100% know that the string is not a valid JSON
72
80
 
73
81
  ## Adding to requirements
74
82
  **Please pin this library only on the major version!**
@@ -80,21 +88,6 @@ To ensure that you only pin the major version of this library in your `requireme
80
88
 
81
89
  In this example, any version that starts with `0.` will be acceptable, allowing for updates on minor and patch versions.
82
90
 
83
- ## Performance considerations
84
- 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:
85
-
86
- from json_repair import repair_json
87
- try:
88
- good_json_string = repair_json(bad_json_string, skip_json_loads=True)
89
- except Exception:
90
- # Not even this library could fix this JSON
91
-
92
- I made a choice of not using any fast json library to avoid having any external dependency, so that anybody can use it regardless of their stack.
93
-
94
- Some rules of thumb to use:
95
- - Setting `return_objects=True` will always be faster because the parser returns an object already and it doesn't have serialize that object to JSON
96
- - `skip_json_loads` is faster only if you 100% know that the string is not a valid JSON
97
-
98
91
  # How it works
99
92
  This module will parse the JSON file following the BNF definition:
100
93
 
@@ -90,6 +90,8 @@ def test_repair_json():
90
90
  assert {
91
91
  repair_json('{"": true, "key2": "value2"}') == '{"empty_placeholder": true, "key2": "value_2"}'
92
92
  }
93
+ # Test a nasty corner case
94
+ assert repair_json(' - { "test_key": ["test_value", "test_value2"] }') == '{"test_key": ["test_value", "test_value2"]}'
93
95
 
94
96
  #Test markdown stupidities from ChatGPT
95
97
  assert repair_json('{ "content": "[LINK]("https://google.com")" }') == '{"content": "[LINK](\\"https://google.com\\")"}'
File without changes
File without changes