json-repair 0.1.5__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 +19 -5
- {json_repair-0.1.5.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.5.dist-info/RECORD +0 -7
- {json_repair-0.1.5.dist-info → json_repair-0.1.7.dist-info}/LICENSE +0 -0
- {json_repair-0.1.5.dist-info → json_repair-0.1.7.dist-info}/WHEEL +0 -0
- {json_repair-0.1.5.dist-info → json_repair-0.1.7.dist-info}/top_level.txt +0 -0
json_repair/json_repair.py
CHANGED
@@ -41,12 +41,18 @@ 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() != ":":
|
47
51
|
self.insert_char_at(":")
|
48
52
|
self.index += 1
|
53
|
+
self.context = "object_value"
|
49
54
|
value = self.parse_json()
|
55
|
+
self.context = ""
|
50
56
|
obj[key] = value
|
51
57
|
|
52
58
|
if self.get_char_at() == ",":
|
@@ -82,8 +88,10 @@ class JSONParser:
|
|
82
88
|
return arr
|
83
89
|
|
84
90
|
def parse_string(self):
|
91
|
+
fixed_quotes = False
|
85
92
|
if self.get_char_at() != '"':
|
86
93
|
self.insert_char_at('"')
|
94
|
+
fixed_quotes = True
|
87
95
|
else:
|
88
96
|
self.index += 1
|
89
97
|
|
@@ -94,7 +102,8 @@ class JSONParser:
|
|
94
102
|
while (
|
95
103
|
(char := self.get_char_at()) != '"'
|
96
104
|
and char is not False
|
97
|
-
and (self.context != "object_key" or 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 != "}"))
|
98
107
|
):
|
99
108
|
self.index += 1
|
100
109
|
|
@@ -102,7 +111,7 @@ class JSONParser:
|
|
102
111
|
if self.get_char_at() != '"':
|
103
112
|
self.insert_char_at('"')
|
104
113
|
# A fallout of the previous special case, we need to update the index only if we didn't enter that case
|
105
|
-
if self.
|
114
|
+
if (self.get_char_at() == '"'):
|
106
115
|
self.index += 1
|
107
116
|
|
108
117
|
return self.json_str[start:end]
|
@@ -148,8 +157,13 @@ class JSONParser:
|
|
148
157
|
|
149
158
|
|
150
159
|
def repair_json(json_str: str, return_objects: bool = False) -> any:
|
151
|
-
|
152
|
-
|
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
|
+
|
153
167
|
if return_objects:
|
154
168
|
return parsed_json
|
155
|
-
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=dxVR6iFDHLjlskxf_55n95CFHtfc8PfVs4gD4PMam9U,4912
|
3
|
-
json_repair-0.1.5.dist-info/LICENSE,sha256=wrjQo8MhNrNCicXtMe3MHmS-fx8AmQk1ue8AQwiiFV8,1076
|
4
|
-
json_repair-0.1.5.dist-info/METADATA,sha256=hukRglu9HI5Dpad9rcgJGxt2zavgLMc-vYQx3E0MhUU,3482
|
5
|
-
json_repair-0.1.5.dist-info/WHEEL,sha256=yQN5g4mg4AybRjkgi-9yy4iQEFibGQmlz78Pik5Or-A,92
|
6
|
-
json_repair-0.1.5.dist-info/top_level.txt,sha256=7-VZwZN2CgB_n0NlSLk-rEUFh8ug21lESbsblOYuZqw,12
|
7
|
-
json_repair-0.1.5.dist-info/RECORD,,
|
File without changes
|
File without changes
|
File without changes
|