json-repair 0.1.6__py3-none-any.whl → 0.1.7__py3-none-any.whl
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/json_repair.py +17 -6
- {json_repair-0.1.6.dist-info → json_repair-0.1.7.dist-info}/METADATA +1 -1
- json_repair-0.1.7.dist-info/RECORD +7 -0
- json_repair-0.1.6.dist-info/RECORD +0 -7
- {json_repair-0.1.6.dist-info → json_repair-0.1.7.dist-info}/LICENSE +0 -0
- {json_repair-0.1.6.dist-info → json_repair-0.1.7.dist-info}/WHEEL +0 -0
- {json_repair-0.1.6.dist-info → json_repair-0.1.7.dist-info}/top_level.txt +0 -0
json_repair/json_repair.py
CHANGED
@@ -41,6 +41,10 @@ class JSONParser:
|
|
41
41
|
obj = {}
|
42
42
|
while (char := self.get_char_at()) != "}" and char is not False:
|
43
43
|
self.context = "object_key"
|
44
|
+
# Skip filler whitespaces
|
45
|
+
if(char == " "):
|
46
|
+
self.index += 1
|
47
|
+
continue
|
44
48
|
key = self.parse_string()
|
45
49
|
self.context = ""
|
46
50
|
if self.get_char_at() != ":":
|
@@ -84,8 +88,10 @@ class JSONParser:
|
|
84
88
|
return arr
|
85
89
|
|
86
90
|
def parse_string(self):
|
91
|
+
fixed_quotes = False
|
87
92
|
if self.get_char_at() != '"':
|
88
93
|
self.insert_char_at('"')
|
94
|
+
fixed_quotes = True
|
89
95
|
else:
|
90
96
|
self.index += 1
|
91
97
|
|
@@ -96,8 +102,8 @@ class JSONParser:
|
|
96
102
|
while (
|
97
103
|
(char := self.get_char_at()) != '"'
|
98
104
|
and char is not False
|
99
|
-
and (self.context != "object_key" or char != ":")
|
100
|
-
and (self.context != "object_value" or (char != "," and char != "}"))
|
105
|
+
and (not fixed_quotes or self.context != "object_key" or char != ":")
|
106
|
+
and (not fixed_quotes or self.context != "object_value" or (char != "," and char != "}"))
|
101
107
|
):
|
102
108
|
self.index += 1
|
103
109
|
|
@@ -105,7 +111,7 @@ class JSONParser:
|
|
105
111
|
if self.get_char_at() != '"':
|
106
112
|
self.insert_char_at('"')
|
107
113
|
# A fallout of the previous special case, we need to update the index only if we didn't enter that case
|
108
|
-
if (self.
|
114
|
+
if (self.get_char_at() == '"'):
|
109
115
|
self.index += 1
|
110
116
|
|
111
117
|
return self.json_str[start:end]
|
@@ -151,8 +157,13 @@ class JSONParser:
|
|
151
157
|
|
152
158
|
|
153
159
|
def repair_json(json_str: str, return_objects: bool = False) -> any:
|
154
|
-
|
155
|
-
|
160
|
+
json_str = json_str.replace("\n", " ").replace("\r", " ").strip()
|
161
|
+
try:
|
162
|
+
parsed_json = json.loads(json_str)
|
163
|
+
except Exception:
|
164
|
+
parser = JSONParser(json_str)
|
165
|
+
parsed_json = parser.parse()
|
166
|
+
|
156
167
|
if return_objects:
|
157
168
|
return parsed_json
|
158
|
-
return json.dumps(parsed_json)
|
169
|
+
return json.dumps(parsed_json)
|
@@ -0,0 +1,7 @@
|
|
1
|
+
json_repair/__init__.py,sha256=5Sn_TG7QYURJ6Fo9s7-vzngauLjfLQ1FVDgmZ35oS1c,52
|
2
|
+
json_repair/json_repair.py,sha256=LifyApdRiK-A4hQoJvsMx0azZHxNJWFJzaDBFBn-YKI,5385
|
3
|
+
json_repair-0.1.7.dist-info/LICENSE,sha256=wrjQo8MhNrNCicXtMe3MHmS-fx8AmQk1ue8AQwiiFV8,1076
|
4
|
+
json_repair-0.1.7.dist-info/METADATA,sha256=o_qtanLe4TbA6z58iduOw_bHzOWDo9J5lpdZGbFvhPg,3482
|
5
|
+
json_repair-0.1.7.dist-info/WHEEL,sha256=yQN5g4mg4AybRjkgi-9yy4iQEFibGQmlz78Pik5Or-A,92
|
6
|
+
json_repair-0.1.7.dist-info/top_level.txt,sha256=7-VZwZN2CgB_n0NlSLk-rEUFh8ug21lESbsblOYuZqw,12
|
7
|
+
json_repair-0.1.7.dist-info/RECORD,,
|
@@ -1,7 +0,0 @@
|
|
1
|
-
json_repair/__init__.py,sha256=5Sn_TG7QYURJ6Fo9s7-vzngauLjfLQ1FVDgmZ35oS1c,52
|
2
|
-
json_repair/json_repair.py,sha256=MbP2LWsfJmB7KoeV3Q5Pqvi9r05kRsGOfCdrhXXF6EM,5138
|
3
|
-
json_repair-0.1.6.dist-info/LICENSE,sha256=wrjQo8MhNrNCicXtMe3MHmS-fx8AmQk1ue8AQwiiFV8,1076
|
4
|
-
json_repair-0.1.6.dist-info/METADATA,sha256=8X5GeJ-btfCFKp-6Ycq1rUHJa9X1pnNScz3VvjrXdVg,3482
|
5
|
-
json_repair-0.1.6.dist-info/WHEEL,sha256=yQN5g4mg4AybRjkgi-9yy4iQEFibGQmlz78Pik5Or-A,92
|
6
|
-
json_repair-0.1.6.dist-info/top_level.txt,sha256=7-VZwZN2CgB_n0NlSLk-rEUFh8ug21lESbsblOYuZqw,12
|
7
|
-
json_repair-0.1.6.dist-info/RECORD,,
|
File without changes
|
File without changes
|
File without changes
|