json-repair 0.4.4__tar.gz → 0.5.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.
- {json_repair-0.4.4 → json_repair-0.5.0}/PKG-INFO +1 -1
- {json_repair-0.4.4 → json_repair-0.5.0}/pyproject.toml +1 -1
- {json_repair-0.4.4 → json_repair-0.5.0}/src/json_repair/json_repair.py +47 -49
- {json_repair-0.4.4 → json_repair-0.5.0}/src/json_repair.egg-info/PKG-INFO +2 -2
- {json_repair-0.4.4 → json_repair-0.5.0}/tests/test_json_repair.py +6 -0
- {json_repair-0.4.4 → json_repair-0.5.0}/tests/test_performance.py +5 -5
- {json_repair-0.4.4 → json_repair-0.5.0}/LICENSE +0 -0
- {json_repair-0.4.4 → json_repair-0.5.0}/README.md +0 -0
- {json_repair-0.4.4 → json_repair-0.5.0}/setup.cfg +0 -0
- {json_repair-0.4.4 → json_repair-0.5.0}/src/json_repair/__init__.py +0 -0
- {json_repair-0.4.4 → json_repair-0.5.0}/src/json_repair.egg-info/SOURCES.txt +0 -0
- {json_repair-0.4.4 → json_repair-0.5.0}/src/json_repair.egg-info/dependency_links.txt +0 -0
- {json_repair-0.4.4 → json_repair-0.5.0}/src/json_repair.egg-info/top_level.txt +0 -0
@@ -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.
|
6
|
+
version = "0.5.0"
|
7
7
|
license = {file = "LICENSE"}
|
8
8
|
authors = [
|
9
9
|
{ name="Stefano Baccianella", email="4247706+mangiucugna@users.noreply.github.com" },
|
@@ -61,6 +61,8 @@ class JSONParser:
|
|
61
61
|
# <string> starts with '"'
|
62
62
|
elif char == '"':
|
63
63
|
return self.parse_string()
|
64
|
+
elif char == "'":
|
65
|
+
return self.parse_string(use_single_quotes=True)
|
64
66
|
# <number> starts with [0-9] or minus
|
65
67
|
elif char.isdigit() or char == "-":
|
66
68
|
return self.parse_number()
|
@@ -100,18 +102,19 @@ class JSONParser:
|
|
100
102
|
# Context is used in the string parser to manage the lack of quotes
|
101
103
|
self.context = "object_key"
|
102
104
|
|
103
|
-
# <member> starts with a <string>
|
104
105
|
self.skip_whitespaces_at()
|
105
|
-
|
106
|
+
|
107
|
+
# <member> starts with a <string>
|
108
|
+
key = ""
|
106
109
|
while key == "" and self.get_char_at():
|
107
|
-
key = self.parse_string(
|
110
|
+
key = self.parse_string(
|
111
|
+
use_single_quotes=(self.json_str[self.index] == "'")
|
112
|
+
)
|
108
113
|
|
109
114
|
# We reached the end here
|
110
115
|
if key == "}":
|
111
116
|
continue
|
112
117
|
|
113
|
-
# Reset context
|
114
|
-
self.context = ""
|
115
118
|
# An extreme case of missing ":" after a key
|
116
119
|
if (self.get_char_at() or "") != ":":
|
117
120
|
self.insert_char_at(":")
|
@@ -119,6 +122,8 @@ class JSONParser:
|
|
119
122
|
self.context = "object_value"
|
120
123
|
# The value can be any valid json
|
121
124
|
value = self.parse_json()
|
125
|
+
|
126
|
+
# Reset context since our job is done
|
122
127
|
self.context = ""
|
123
128
|
obj[key] = value
|
124
129
|
|
@@ -141,6 +146,7 @@ class JSONParser:
|
|
141
146
|
while (self.get_char_at() or "]") != "]":
|
142
147
|
value = self.parse_json()
|
143
148
|
|
149
|
+
# It is possible that parse_json() returns nothing valid, so we stop
|
144
150
|
if not value:
|
145
151
|
break
|
146
152
|
|
@@ -153,9 +159,10 @@ class JSONParser:
|
|
153
159
|
char = self.get_char_at()
|
154
160
|
|
155
161
|
# Especially at the end of an LLM generated json you might miss the last "]"
|
156
|
-
|
162
|
+
char = self.get_char_at()
|
163
|
+
if char and char != "]":
|
157
164
|
# Sometimes when you fix a missing "]" you'll have a trailing "," there that makes the JSON invalid
|
158
|
-
if
|
165
|
+
if char == ",":
|
159
166
|
# Remove trailing "," before adding the "]"
|
160
167
|
self.remove_char_at()
|
161
168
|
self.insert_char_at("]")
|
@@ -163,18 +170,24 @@ class JSONParser:
|
|
163
170
|
self.index += 1
|
164
171
|
return arr
|
165
172
|
|
166
|
-
def parse_string(self) -> str:
|
173
|
+
def parse_string(self, use_single_quotes=False) -> str:
|
167
174
|
# <string> is a string of valid characters enclosed in quotes
|
175
|
+
# i.e. { name: "John" }
|
168
176
|
# Somehow all weird cases in an invalid JSON happen to be resolved in this function, so be careful here
|
177
|
+
|
169
178
|
# Flag to manage corner cases related to missing starting quote
|
170
179
|
fixed_quotes = False
|
171
|
-
|
172
|
-
if
|
173
|
-
|
180
|
+
string_terminator = '"'
|
181
|
+
if use_single_quotes:
|
182
|
+
string_terminator = "'"
|
183
|
+
char = self.get_char_at()
|
184
|
+
if char != string_terminator:
|
185
|
+
self.insert_char_at(string_terminator)
|
174
186
|
fixed_quotes = True
|
175
187
|
else:
|
176
188
|
self.index += 1
|
177
|
-
|
189
|
+
|
190
|
+
# Start position of the string (to use later in the return value)
|
178
191
|
start = self.index
|
179
192
|
|
180
193
|
# Here things get a bit hairy because a string missing the final quote can also be a key or a value in an object
|
@@ -184,38 +197,26 @@ class JSONParser:
|
|
184
197
|
# * It iterated over the entire sequence
|
185
198
|
# * If we are fixing missing quotes in an object, when it finds the special terminators
|
186
199
|
char = self.get_char_at()
|
187
|
-
while
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
not fixed_quotes
|
194
|
-
or self.context != "object_value"
|
195
|
-
or (char != "," and char != "}")
|
196
|
-
)
|
197
|
-
):
|
200
|
+
while char and char != string_terminator:
|
201
|
+
if fixed_quotes:
|
202
|
+
if self.context == "object_key" and (char == ":" or char.isspace()):
|
203
|
+
break
|
204
|
+
elif self.context == "object_value" and (char == "," or char == "}"):
|
205
|
+
break
|
198
206
|
self.index += 1
|
199
207
|
char = self.get_char_at()
|
200
208
|
|
201
|
-
|
202
|
-
if (
|
203
|
-
fixed_quotes
|
204
|
-
and self.context == "object_key"
|
205
|
-
and (self.get_char_at() or "").isspace()
|
206
|
-
):
|
207
|
-
# skip whitespaces
|
209
|
+
if char and fixed_quotes and self.context == "object_key" and char.isspace():
|
208
210
|
self.skip_whitespaces_at()
|
209
|
-
|
210
|
-
|
211
|
-
if (self.get_char_at() or "") not in [":", ","]:
|
211
|
+
if self.get_char_at() not in [":", ","]:
|
212
212
|
return ""
|
213
213
|
|
214
214
|
end = self.index
|
215
|
-
|
216
|
-
self.insert_char_at('"')
|
215
|
+
|
217
216
|
# A fallout of the previous special case in the while loop, we need to update the index only if we had a closing quote
|
218
|
-
if
|
217
|
+
if char != string_terminator:
|
218
|
+
self.insert_char_at(string_terminator)
|
219
|
+
else:
|
219
220
|
self.index += 1
|
220
221
|
|
221
222
|
return self.json_str[start:end]
|
@@ -223,8 +224,9 @@ class JSONParser:
|
|
223
224
|
def parse_number(self) -> Union[float, int]:
|
224
225
|
# <number> is a valid real number expressed in one of a number of given formats
|
225
226
|
number_str = ""
|
227
|
+
number_chars = set("0123456789-.eE")
|
226
228
|
char = self.get_char_at()
|
227
|
-
while char and
|
229
|
+
while char and char in number_chars:
|
228
230
|
number_str += char
|
229
231
|
self.index += 1
|
230
232
|
char = self.get_char_at()
|
@@ -239,18 +241,14 @@ class JSONParser:
|
|
239
241
|
|
240
242
|
def parse_boolean_or_null(self) -> Union[bool, None]:
|
241
243
|
# <boolean> is one of the literal strings 'true', 'false', or 'null' (unquoted)
|
242
|
-
|
243
|
-
|
244
|
-
|
245
|
-
|
246
|
-
|
247
|
-
|
248
|
-
|
249
|
-
|
250
|
-
return None
|
251
|
-
else:
|
252
|
-
# This is a string then
|
253
|
-
return self.parse_string()
|
244
|
+
boolean_map = {"true": (True, 4), "false": (False, 5), "null": (None, 4)}
|
245
|
+
for key, (value, length) in boolean_map.items():
|
246
|
+
if self.json_str.startswith(key, self.index):
|
247
|
+
self.index += length
|
248
|
+
return value
|
249
|
+
|
250
|
+
# This is a string then
|
251
|
+
return self.parse_string()
|
254
252
|
|
255
253
|
def insert_char_at(self, char: str) -> None:
|
256
254
|
self.json_str = self.json_str[: self.index] + char + self.json_str[self.index :]
|
@@ -8,6 +8,7 @@ def test_repair_json():
|
|
8
8
|
assert repair_json("\"") == '""'
|
9
9
|
assert repair_json("\n") == '""'
|
10
10
|
assert repair_json('{"key": true, "key2": false, "key3": null}') == '{"key": true, "key2": false, "key3": null}'
|
11
|
+
assert repair_json("{'key': 'string', 'key2': false, \"key3\": null, \"key4\": unquoted}") == '{"key": "string", "key2": false, "key3": null, "key4": "unquoted"}'
|
11
12
|
assert (
|
12
13
|
repair_json('{"name": "John", "age": 30, "city": "New York"}')
|
13
14
|
== '{"name": "John", "age": 30, "city": "New York"}'
|
@@ -66,6 +67,10 @@ def test_repair_json():
|
|
66
67
|
repair_json('{"text": "The quick brown fox,"}')
|
67
68
|
== '{"text": "The quick brown fox,"}'
|
68
69
|
)
|
70
|
+
assert (
|
71
|
+
repair_json('{"text": "The quick brown fox won\'t jump"}')
|
72
|
+
== '{"text": "The quick brown fox won\'t jump"}'
|
73
|
+
)
|
69
74
|
assert {
|
70
75
|
repair_json('{"value_1": "value_2": "data"}') == '{"value_1": "value_2", "data": ""}'
|
71
76
|
}
|
@@ -119,6 +124,7 @@ def test_repair_json_with_objects():
|
|
119
124
|
assert repair_json("[", True) == []
|
120
125
|
assert repair_json("{", True) == {}
|
121
126
|
assert repair_json('{"key": "value:value"}', True) == {"key": "value:value"}
|
127
|
+
assert repair_json("{'key': 'string', 'key2': false, \"key3\": null, \"key4\": unquoted}", True) == {"key": "string", "key2": False, "key3": None, "key4": "unquoted"}
|
122
128
|
assert repair_json('{"name": "John", "age": 30, "city": "New', True) == {
|
123
129
|
"name": "John",
|
124
130
|
"age": 30,
|
@@ -282,7 +282,7 @@ def test_true_true(benchmark):
|
|
282
282
|
mean_time = benchmark.stats.get("median")
|
283
283
|
|
284
284
|
# Define your time threshold in seconds (100ms in this case)
|
285
|
-
max_time = 1
|
285
|
+
max_time = 1 / 10 ** 6 # 1 microsecond
|
286
286
|
|
287
287
|
# Assert that the average time is below the threshold
|
288
288
|
assert mean_time < max_time, f"Benchmark exceeded threshold: {mean_time:.3f}s > {max_time:.3f}s"
|
@@ -293,7 +293,7 @@ def test_true_false(benchmark):
|
|
293
293
|
mean_time = benchmark.stats.get("median")
|
294
294
|
|
295
295
|
# Define your time threshold in seconds (100ms in this case)
|
296
|
-
max_time =
|
296
|
+
max_time = 160 * (1 / 10 ** 6) # 160 microsecond
|
297
297
|
|
298
298
|
# Assert that the average time is below the threshold
|
299
299
|
assert mean_time < max_time, f"Benchmark exceeded threshold: {mean_time:.3f}s > {max_time:.3f}s"
|
@@ -303,8 +303,8 @@ def test_false_true(benchmark):
|
|
303
303
|
# Retrieve the median execution time
|
304
304
|
mean_time = benchmark.stats.get("median")
|
305
305
|
|
306
|
-
# Define your time threshold in seconds (
|
307
|
-
max_time =
|
306
|
+
# Define your time threshold in seconds (ms in this case)
|
307
|
+
max_time = 0.9 / 10 ** 3 # 0.9 millisecond
|
308
308
|
|
309
309
|
# Assert that the average time is below the threshold
|
310
310
|
assert mean_time < max_time, f"Benchmark exceeded threshold: {mean_time:.3f}s > {max_time:.3f}s"
|
@@ -315,7 +315,7 @@ def test_false_false(benchmark):
|
|
315
315
|
mean_time = benchmark.stats.get("median")
|
316
316
|
|
317
317
|
# Define your time threshold in seconds (100ms in this case)
|
318
|
-
max_time =
|
318
|
+
max_time = 190 * (1 / 10 ** 6) # 190 microsecond
|
319
319
|
|
320
320
|
# Assert that the average time is below the threshold
|
321
321
|
assert mean_time < max_time, f"Benchmark exceeded threshold: {mean_time:.3f}s > {max_time:.3f}s"
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|