json-repair 0.7.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.7.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,35 +49,28 @@ 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
72
-
73
- ## Performance considerations
65
+
66
+ decoded_object = json_repair.repair_json(json_string, return_objects=True)
67
+
68
+ ### Performance considerations
74
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:
75
70
 
76
71
  from json_repair import repair_json
77
- try:
78
- good_json_string = repair_json(bad_json_string, skip_json_loads=True)
79
- except Exception:
80
- # Not even this library could fix this JSON
72
+
73
+ good_json_string = repair_json(bad_json_string, skip_json_loads=True)
81
74
 
82
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.
83
76
 
@@ -85,6 +78,16 @@ Some rules of thumb to use:
85
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
86
79
  - `skip_json_loads` is faster only if you 100% know that the string is not a valid JSON
87
80
 
81
+ ## Adding to requirements
82
+ **Please pin this library only on the major version!**
83
+
84
+ We use TDD and strict semantic versioning, there will be frequent updates and no breaking changes in minor and patch versions.
85
+ To ensure that you only pin the major version of this library in your `requirements.txt`, specify the package name followed by the major version and a wildcard for minor and patch versions. For example:
86
+
87
+ json_repair==0.*
88
+
89
+ In this example, any version that starts with `0.` will be acceptable, allowing for updates on minor and patch versions.
90
+
88
91
  # How it works
89
92
  This module will parse the JSON file following the BNF definition:
90
93
 
@@ -12,35 +12,28 @@ 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
35
-
36
- ## Performance considerations
28
+
29
+ decoded_object = json_repair.repair_json(json_string, return_objects=True)
30
+
31
+ ### Performance considerations
37
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:
38
33
 
39
34
  from json_repair import repair_json
40
- try:
41
- good_json_string = repair_json(bad_json_string, skip_json_loads=True)
42
- except Exception:
43
- # Not even this library could fix this JSON
35
+
36
+ good_json_string = repair_json(bad_json_string, skip_json_loads=True)
44
37
 
45
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.
46
39
 
@@ -48,6 +41,16 @@ Some rules of thumb to use:
48
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
49
42
  - `skip_json_loads` is faster only if you 100% know that the string is not a valid JSON
50
43
 
44
+ ## Adding to requirements
45
+ **Please pin this library only on the major version!**
46
+
47
+ We use TDD and strict semantic versioning, there will be frequent updates and no breaking changes in minor and patch versions.
48
+ To ensure that you only pin the major version of this library in your `requirements.txt`, specify the package name followed by the major version and a wildcard for minor and patch versions. For example:
49
+
50
+ json_repair==0.*
51
+
52
+ In this example, any version that starts with `0.` will be acceptable, allowing for updates on minor and patch versions.
53
+
51
54
  # How it works
52
55
  This module will parse the JSON file following the BNF definition:
53
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.7.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" },
@@ -71,14 +71,10 @@ class JSONParser:
71
71
  # This might be a <string> that is missing the starting '"'
72
72
  elif char.isalpha():
73
73
  return self.parse_string()
74
- # Ignore whitespaces outside of strings
75
- elif char.isspace():
74
+ # If everything else fails, we just ignore and move on
75
+ else:
76
76
  self.index += 1
77
- self.skip_whitespaces_at()
78
77
  return self.parse_json()
79
- # If everything else fails, then we give up and return an exception
80
- else:
81
- raise ValueError("Invalid JSON format")
82
78
 
83
79
  def parse_object(self) -> Dict[str, Any]:
84
80
  # <object> ::= '{' [ <member> *(', ' <member>) ] '}' ; A sequence of 'members'
@@ -251,6 +247,9 @@ class JSONParser:
251
247
  if number_str:
252
248
  if "." in number_str or "e" in number_str or "E" in number_str:
253
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()
254
253
  else:
255
254
  return int(number_str)
256
255
  else:
@@ -306,7 +305,7 @@ def repair_json(
306
305
  It will return the fixed string by default.
307
306
  When `return_objects=True` is passed, it will return the decoded data structure instead.
308
307
  """
309
- json_str = json_str.strip().lstrip("```json").rstrip("```")
308
+ json_str = json_str.strip().lstrip("```json")
310
309
  parser = JSONParser(json_str)
311
310
  if skip_json_loads:
312
311
  parsed_json = parser.parse()
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: json_repair
3
- Version: 0.7.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,35 +49,28 @@ 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
72
-
73
- ## Performance considerations
65
+
66
+ decoded_object = json_repair.repair_json(json_string, return_objects=True)
67
+
68
+ ### Performance considerations
74
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:
75
70
 
76
71
  from json_repair import repair_json
77
- try:
78
- good_json_string = repair_json(bad_json_string, skip_json_loads=True)
79
- except Exception:
80
- # Not even this library could fix this JSON
72
+
73
+ good_json_string = repair_json(bad_json_string, skip_json_loads=True)
81
74
 
82
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.
83
76
 
@@ -85,6 +78,16 @@ Some rules of thumb to use:
85
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
86
79
  - `skip_json_loads` is faster only if you 100% know that the string is not a valid JSON
87
80
 
81
+ ## Adding to requirements
82
+ **Please pin this library only on the major version!**
83
+
84
+ We use TDD and strict semantic versioning, there will be frequent updates and no breaking changes in minor and patch versions.
85
+ To ensure that you only pin the major version of this library in your `requirements.txt`, specify the package name followed by the major version and a wildcard for minor and patch versions. For example:
86
+
87
+ json_repair==0.*
88
+
89
+ In this example, any version that starts with `0.` will be acceptable, allowing for updates on minor and patch versions.
90
+
88
91
  # How it works
89
92
  This module will parse the JSON file following the BNF definition:
90
93
 
@@ -46,8 +46,10 @@ def test_repair_json():
46
46
  # Test with edge cases
47
47
  assert repair_json(" ") == '""'
48
48
  assert repair_json("[") == "[]"
49
+ assert repair_json("]") == '""'
49
50
  assert repair_json("[[1\n\n]") == "[[1]]"
50
51
  assert repair_json("{") == "{}"
52
+ assert repair_json("}") == '""'
51
53
  assert repair_json('{"') == '{"": ""}'
52
54
  assert repair_json('["') == '[]'
53
55
  assert repair_json("'\"'") == '"\\\""'
@@ -88,6 +90,8 @@ def test_repair_json():
88
90
  assert {
89
91
  repair_json('{"": true, "key2": "value2"}') == '{"empty_placeholder": true, "key2": "value_2"}'
90
92
  }
93
+ # Test a nasty corner case
94
+ assert repair_json(' - { "test_key": ["test_value", "test_value2"] }') == '{"test_key": ["test_value", "test_value2"]}'
91
95
 
92
96
  #Test markdown stupidities from ChatGPT
93
97
  assert repair_json('{ "content": "[LINK]("https://google.com")" }') == '{"content": "[LINK](\\"https://google.com\\")"}'
File without changes
File without changes