json-repair 0.55.2__py3-none-any.whl → 0.56.0__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_parser.py +59 -19
- json_repair/json_repair.py +91 -12
- json_repair/parse_array.py +76 -15
- json_repair/parse_object.py +107 -10
- json_repair/schema_repair.py +508 -0
- json_repair/utils/constants.py +11 -0
- {json_repair-0.55.2.dist-info → json_repair-0.56.0.dist-info}/METADATA +67 -3
- {json_repair-0.55.2.dist-info → json_repair-0.56.0.dist-info}/RECORD +12 -11
- {json_repair-0.55.2.dist-info → json_repair-0.56.0.dist-info}/WHEEL +0 -0
- {json_repair-0.55.2.dist-info → json_repair-0.56.0.dist-info}/entry_points.txt +0 -0
- {json_repair-0.55.2.dist-info → json_repair-0.56.0.dist-info}/licenses/LICENSE +0 -0
- {json_repair-0.55.2.dist-info → json_repair-0.56.0.dist-info}/top_level.txt +0 -0
json_repair/json_parser.py
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
from
|
|
1
|
+
from collections.abc import Callable
|
|
2
|
+
from typing import TYPE_CHECKING, Any, TextIO
|
|
2
3
|
|
|
3
4
|
from .parse_array import parse_array as _parse_array
|
|
4
5
|
from .parse_comment import parse_comment as _parse_comment
|
|
@@ -10,11 +11,18 @@ from .utils.json_context import JsonContext
|
|
|
10
11
|
from .utils.object_comparer import ObjectComparer
|
|
11
12
|
from .utils.string_file_wrapper import StringFileWrapper
|
|
12
13
|
|
|
14
|
+
if TYPE_CHECKING:
|
|
15
|
+
from .schema_repair import SchemaRepairer
|
|
16
|
+
|
|
13
17
|
|
|
14
18
|
class JSONParser:
|
|
15
19
|
# Split the parse methods into separate files because this one was like 3000 lines
|
|
16
|
-
def parse_array(
|
|
17
|
-
|
|
20
|
+
def parse_array(
|
|
21
|
+
self,
|
|
22
|
+
schema: dict[str, Any] | bool | None = None,
|
|
23
|
+
path: str = "$",
|
|
24
|
+
) -> list[JSONReturnType]:
|
|
25
|
+
return _parse_array(self, schema, path)
|
|
18
26
|
|
|
19
27
|
def parse_comment(self) -> JSONReturnType:
|
|
20
28
|
return _parse_comment(self)
|
|
@@ -22,8 +30,12 @@ class JSONParser:
|
|
|
22
30
|
def parse_number(self) -> JSONReturnType:
|
|
23
31
|
return _parse_number(self)
|
|
24
32
|
|
|
25
|
-
def parse_object(
|
|
26
|
-
|
|
33
|
+
def parse_object(
|
|
34
|
+
self,
|
|
35
|
+
schema: dict[str, Any] | bool | None = None,
|
|
36
|
+
path: str = "$",
|
|
37
|
+
) -> JSONReturnType:
|
|
38
|
+
return _parse_object(self, schema, path)
|
|
27
39
|
|
|
28
40
|
def parse_string(self) -> JSONReturnType:
|
|
29
41
|
return _parse_string(self)
|
|
@@ -53,8 +65,8 @@ class JSONParser:
|
|
|
53
65
|
# We could add a guard in the code for each call but that would make this code unreadable, so here's this neat trick
|
|
54
66
|
# Replace self.log with a noop
|
|
55
67
|
self.logging = logging
|
|
68
|
+
self.logger: list[dict[str, str]] = []
|
|
56
69
|
if logging:
|
|
57
|
-
self.logger: list[dict[str, str]] = []
|
|
58
70
|
self.log = self._log
|
|
59
71
|
else:
|
|
60
72
|
# No-op
|
|
@@ -71,11 +83,26 @@ class JSONParser:
|
|
|
71
83
|
# may not be desirable in some use cases and the user would prefer json_repair to return an exception.
|
|
72
84
|
# So strict mode was added to disable some of those heuristics.
|
|
73
85
|
self.strict = strict
|
|
86
|
+
self.schema_repairer: SchemaRepairer | None = None
|
|
74
87
|
|
|
75
88
|
def parse(
|
|
76
89
|
self,
|
|
77
|
-
) -> JSONReturnType
|
|
78
|
-
|
|
90
|
+
) -> JSONReturnType:
|
|
91
|
+
return self._parse_top_level(self.parse_json)
|
|
92
|
+
|
|
93
|
+
def parse_with_schema(
|
|
94
|
+
self,
|
|
95
|
+
repairer: "SchemaRepairer",
|
|
96
|
+
schema: dict[str, Any] | bool,
|
|
97
|
+
) -> JSONReturnType:
|
|
98
|
+
"""Parse with schema guidance enabled for all nested values."""
|
|
99
|
+
self.schema_repairer = repairer
|
|
100
|
+
return self._parse_top_level(lambda: self.parse_json(schema, "$"))
|
|
101
|
+
|
|
102
|
+
# Consolidate top-level parsing so we handle multiple sequential JSON values consistently
|
|
103
|
+
# (including update semantics and strict-mode validation).
|
|
104
|
+
def _parse_top_level(self, parse_element: Callable[[], JSONReturnType]) -> JSONReturnType:
|
|
105
|
+
json = parse_element()
|
|
79
106
|
if self.index < len(self.json_str):
|
|
80
107
|
self.log(
|
|
81
108
|
"The parser returned early, checking if there's more json elements",
|
|
@@ -83,19 +110,17 @@ class JSONParser:
|
|
|
83
110
|
json = [json]
|
|
84
111
|
while self.index < len(self.json_str):
|
|
85
112
|
self.context.reset()
|
|
86
|
-
j =
|
|
113
|
+
j = parse_element()
|
|
87
114
|
if j:
|
|
88
115
|
if ObjectComparer.is_same_object(json[-1], j):
|
|
89
|
-
#
|
|
116
|
+
# Treat repeated objects as updates: keep the newest value.
|
|
90
117
|
json.pop()
|
|
91
118
|
else:
|
|
92
119
|
if not json[-1]:
|
|
93
120
|
json.pop()
|
|
94
121
|
json.append(j)
|
|
95
122
|
else:
|
|
96
|
-
# this was a bust, move the index
|
|
97
123
|
self.index += 1
|
|
98
|
-
# If nothing extra was found, don't return an array
|
|
99
124
|
if len(json) == 1:
|
|
100
125
|
self.log(
|
|
101
126
|
"There were no more elements, returning the element without the array",
|
|
@@ -106,13 +131,23 @@ class JSONParser:
|
|
|
106
131
|
"Multiple top-level JSON elements found in strict mode, raising an error",
|
|
107
132
|
)
|
|
108
133
|
raise ValueError("Multiple top-level JSON elements found in strict mode.")
|
|
109
|
-
if self.logging:
|
|
110
|
-
return json, self.logger
|
|
111
134
|
return json
|
|
112
135
|
|
|
113
136
|
def parse_json(
|
|
114
137
|
self,
|
|
138
|
+
schema: dict[str, Any] | bool | None = None,
|
|
139
|
+
path: str = "$",
|
|
115
140
|
) -> JSONReturnType:
|
|
141
|
+
"""Parse the next JSON value and, when configured, enforce schema constraints."""
|
|
142
|
+
repairer = self.schema_repairer if self.schema_repairer is not None and schema not in (None, True) else None
|
|
143
|
+
if repairer is not None:
|
|
144
|
+
# Resolve references once and decide whether schema-guided repairs are needed.
|
|
145
|
+
schema = repairer.resolve_schema(schema)
|
|
146
|
+
if schema is True:
|
|
147
|
+
repairer = None
|
|
148
|
+
elif schema is False:
|
|
149
|
+
raise ValueError("Schema does not allow any values.")
|
|
150
|
+
|
|
116
151
|
while True:
|
|
117
152
|
char = self.get_char_at()
|
|
118
153
|
# None means that we are at the end of the string provided
|
|
@@ -121,19 +156,24 @@ class JSONParser:
|
|
|
121
156
|
# <object> starts with '{'
|
|
122
157
|
if char == "{":
|
|
123
158
|
self.index += 1
|
|
124
|
-
|
|
159
|
+
value = self.parse_object(schema, path) if repairer else self.parse_object()
|
|
160
|
+
return repairer.repair_value(value, schema, path) if repairer else value
|
|
125
161
|
# <array> starts with '['
|
|
126
162
|
if char == "[":
|
|
127
163
|
self.index += 1
|
|
128
|
-
|
|
164
|
+
value = self.parse_array(schema, path) if repairer else self.parse_array()
|
|
165
|
+
return repairer.repair_value(value, schema, path) if repairer else value
|
|
129
166
|
# <string> starts with a quote
|
|
130
167
|
if not self.context.empty and (char in STRING_DELIMITERS or char.isalpha()):
|
|
131
|
-
|
|
168
|
+
value = self.parse_string()
|
|
169
|
+
return repairer.repair_value(value, schema, path) if repairer else value
|
|
132
170
|
# <number> starts with [0-9] or minus
|
|
133
171
|
if not self.context.empty and (char.isdigit() or char == "-" or char == "."):
|
|
134
|
-
|
|
172
|
+
value = self.parse_number()
|
|
173
|
+
return repairer.repair_value(value, schema, path) if repairer else value
|
|
135
174
|
if char in ["#", "/"]:
|
|
136
|
-
|
|
175
|
+
value = self.parse_comment()
|
|
176
|
+
return repairer.repair_value(value, schema, path) if repairer else value
|
|
137
177
|
# If everything else fails, we just ignore and move on
|
|
138
178
|
self.index += 1
|
|
139
179
|
|
json_repair/json_repair.py
CHANGED
|
@@ -29,6 +29,7 @@ from pathlib import Path
|
|
|
29
29
|
from typing import Any, Literal, TextIO, overload
|
|
30
30
|
|
|
31
31
|
from .json_parser import JSONParser
|
|
32
|
+
from .schema_repair import SchemaRepairer, load_schema_model, schema_from_input
|
|
32
33
|
from .utils.constants import JSONReturnType
|
|
33
34
|
|
|
34
35
|
|
|
@@ -42,6 +43,7 @@ def repair_json(
|
|
|
42
43
|
chunk_length: int = 0,
|
|
43
44
|
stream_stable: bool = False,
|
|
44
45
|
strict: bool = False,
|
|
46
|
+
schema: Any | None = None,
|
|
45
47
|
**json_dumps_args: Any,
|
|
46
48
|
) -> str: ...
|
|
47
49
|
|
|
@@ -56,6 +58,7 @@ def repair_json(
|
|
|
56
58
|
chunk_length: int = 0,
|
|
57
59
|
stream_stable: bool = False,
|
|
58
60
|
strict: bool = False,
|
|
61
|
+
schema: Any | None = None,
|
|
59
62
|
**json_dumps_args: Any,
|
|
60
63
|
) -> JSONReturnType | tuple[JSONReturnType, list[dict[str, str]]]: ...
|
|
61
64
|
|
|
@@ -69,6 +72,7 @@ def repair_json(
|
|
|
69
72
|
chunk_length: int = 0,
|
|
70
73
|
stream_stable: bool = False,
|
|
71
74
|
strict: bool = False,
|
|
75
|
+
schema: Any | None = None,
|
|
72
76
|
**json_dumps_args: Any,
|
|
73
77
|
) -> JSONReturnType | tuple[JSONReturnType, list[dict[str, str]]]:
|
|
74
78
|
"""
|
|
@@ -84,24 +88,46 @@ def repair_json(
|
|
|
84
88
|
chunk_length (int, optional): Size in bytes of the file chunks to read at once. Ignored if `json_fd` is None. Do not use! Use `from_file` or `load` instead. Defaults to 1MB.
|
|
85
89
|
stream_stable (bool, optional): When the json to be repaired is the accumulation of streaming json at a certain moment.If this parameter to True will keep the repair results stable.
|
|
86
90
|
strict (bool, optional): If True, surface structural problems (duplicate keys, missing separators, empty keys/values, etc.) as ValueError instead of repairing them.
|
|
91
|
+
schema (Any, optional): JSON Schema dict, boolean schema, or pydantic v2 model used to guide repairs. Schema guidance is skipped for already-valid JSON unless `skip_json_loads=True`.
|
|
87
92
|
Returns:
|
|
88
93
|
Union[JSONReturnType, Tuple[JSONReturnType, List[Dict[str, str]]]]: The repaired JSON or a tuple with the repaired JSON and repair log when logging is True.
|
|
89
94
|
"""
|
|
95
|
+
# Schema-guided repairs and strict mode are mutually exclusive to avoid conflicting behavior.
|
|
96
|
+
if schema is not None and strict:
|
|
97
|
+
raise ValueError("schema and strict cannot be used together.")
|
|
98
|
+
|
|
90
99
|
parser = JSONParser(json_str, json_fd, logging, chunk_length, stream_stable, strict)
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
100
|
+
# When JSON is already valid, skip schema guidance unless the caller explicitly disables json.loads.
|
|
101
|
+
if not skip_json_loads:
|
|
102
|
+
loaded_json: JSONReturnType | None
|
|
94
103
|
try:
|
|
95
|
-
|
|
104
|
+
loaded_json = json.load(json_fd) if json_fd else json.loads(json_str)
|
|
96
105
|
except json.JSONDecodeError:
|
|
97
|
-
|
|
106
|
+
loaded_json = None
|
|
107
|
+
else:
|
|
108
|
+
if logging:
|
|
109
|
+
return loaded_json, []
|
|
110
|
+
if return_objects:
|
|
111
|
+
return loaded_json
|
|
112
|
+
if loaded_json == "":
|
|
113
|
+
return ""
|
|
114
|
+
return json.dumps(loaded_json, **json_dumps_args)
|
|
115
|
+
|
|
116
|
+
# Schema guidance only happens in parser mode.
|
|
117
|
+
schema_obj = schema_from_input(schema) if schema is not None else None
|
|
118
|
+
parsed_json: JSONReturnType
|
|
119
|
+
if schema_obj is None:
|
|
120
|
+
parsed_json = parser.parse()
|
|
121
|
+
else:
|
|
122
|
+
repairer = SchemaRepairer(schema_obj, parser.logger if logging else None)
|
|
123
|
+
parsed_json = parser.parse_with_schema(repairer, schema_obj)
|
|
124
|
+
# Post-parse validation ensures we reject values that cannot satisfy the schema.
|
|
125
|
+
repairer.validate(parsed_json, schema_obj)
|
|
98
126
|
# It's useful to return the actual object instead of the json string,
|
|
99
127
|
# it allows this lib to be a replacement of the json library
|
|
100
|
-
if
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
if logging and not isinstance(parsed_json, tuple):
|
|
104
|
-
return parsed_json, []
|
|
128
|
+
if logging:
|
|
129
|
+
return parsed_json, parser.logger
|
|
130
|
+
if return_objects:
|
|
105
131
|
return parsed_json
|
|
106
132
|
# Avoid returning only a pair of quotes if it's an empty string
|
|
107
133
|
if parsed_json == "":
|
|
@@ -115,6 +141,7 @@ def loads(
|
|
|
115
141
|
logging: bool = False,
|
|
116
142
|
stream_stable: bool = False,
|
|
117
143
|
strict: bool = False,
|
|
144
|
+
schema: Any | None = None,
|
|
118
145
|
) -> JSONReturnType | tuple[JSONReturnType, list[dict[str, str]]] | str:
|
|
119
146
|
"""
|
|
120
147
|
This function works like `json.loads()` except that it will fix your JSON in the process.
|
|
@@ -125,6 +152,7 @@ def loads(
|
|
|
125
152
|
skip_json_loads (bool, optional): If True, skip calling the built-in json.loads() function to verify that the json is valid before attempting to repair. Defaults to False.
|
|
126
153
|
logging (bool, optional): If True, return a tuple with the repaired json and a log of all repair actions. Defaults to False.
|
|
127
154
|
strict (bool, optional): If True, surface structural problems (duplicate keys, missing separators, empty keys/values, etc.) as ValueError instead of repairing them.
|
|
155
|
+
schema (Any, optional): JSON Schema dict, boolean schema, or pydantic v2 model used to guide repairs. Schema guidance is skipped for already-valid JSON unless `skip_json_loads=True`.
|
|
128
156
|
|
|
129
157
|
Returns:
|
|
130
158
|
Union[JSONReturnType, Tuple[JSONReturnType, List[Dict[str, str]]], str]: The repaired JSON object or a tuple with the repaired JSON object and repair log.
|
|
@@ -136,6 +164,7 @@ def loads(
|
|
|
136
164
|
logging=logging,
|
|
137
165
|
stream_stable=stream_stable,
|
|
138
166
|
strict=strict,
|
|
167
|
+
schema=schema,
|
|
139
168
|
)
|
|
140
169
|
|
|
141
170
|
|
|
@@ -145,6 +174,7 @@ def load(
|
|
|
145
174
|
logging: bool = False,
|
|
146
175
|
chunk_length: int = 0,
|
|
147
176
|
strict: bool = False,
|
|
177
|
+
schema: Any | None = None,
|
|
148
178
|
) -> JSONReturnType | tuple[JSONReturnType, list[dict[str, str]]]:
|
|
149
179
|
"""
|
|
150
180
|
This function works like `json.load()` except that it will fix your JSON in the process.
|
|
@@ -156,6 +186,7 @@ def load(
|
|
|
156
186
|
logging (bool, optional): If True, return a tuple with the repaired json and a log of all repair actions. Defaults to False.
|
|
157
187
|
chunk_length (int, optional): Size in bytes of the file chunks to read at once. Defaults to 1MB.
|
|
158
188
|
strict (bool, optional): If True, surface structural problems (duplicate keys, missing separators, empty keys/values, etc.) as ValueError instead of repairing them.
|
|
189
|
+
schema (Any, optional): JSON Schema dict, boolean schema, or pydantic v2 model used to guide repairs. Schema guidance is skipped for already-valid JSON unless `skip_json_loads=True`.
|
|
159
190
|
|
|
160
191
|
Returns:
|
|
161
192
|
Union[JSONReturnType, Tuple[JSONReturnType, List[Dict[str, str]]]]: The repaired JSON object or a tuple with the repaired JSON object and repair log.
|
|
@@ -167,6 +198,7 @@ def load(
|
|
|
167
198
|
skip_json_loads=skip_json_loads,
|
|
168
199
|
logging=logging,
|
|
169
200
|
strict=strict,
|
|
201
|
+
schema=schema,
|
|
170
202
|
)
|
|
171
203
|
|
|
172
204
|
|
|
@@ -176,6 +208,7 @@ def from_file(
|
|
|
176
208
|
logging: bool = False,
|
|
177
209
|
chunk_length: int = 0,
|
|
178
210
|
strict: bool = False,
|
|
211
|
+
schema: Any | None = None,
|
|
179
212
|
) -> JSONReturnType | tuple[JSONReturnType, list[dict[str, str]]]:
|
|
180
213
|
"""
|
|
181
214
|
This function is a wrapper around `load()` so you can pass the filename as string
|
|
@@ -186,6 +219,7 @@ def from_file(
|
|
|
186
219
|
logging (bool, optional): If True, return a tuple with the repaired json and a log of all repair actions. Defaults to False.
|
|
187
220
|
chunk_length (int, optional): Size in bytes of the file chunks to read at once. Defaults to 1MB.
|
|
188
221
|
strict (bool, optional): If True, surface structural problems (duplicate keys, missing separators, empty keys/values, etc.) as ValueError instead of repairing them.
|
|
222
|
+
schema (Any, optional): JSON Schema dict, boolean schema, or pydantic v2 model used to guide repairs. Schema guidance is skipped for already-valid JSON unless `skip_json_loads=True`.
|
|
189
223
|
|
|
190
224
|
Returns:
|
|
191
225
|
Union[JSONReturnType, Tuple[JSONReturnType, List[Dict[str, str]]]]: The repaired JSON object or a tuple with the repaired JSON object and repair log.
|
|
@@ -197,6 +231,7 @@ def from_file(
|
|
|
197
231
|
logging=logging,
|
|
198
232
|
chunk_length=chunk_length,
|
|
199
233
|
strict=strict,
|
|
234
|
+
schema=schema,
|
|
200
235
|
)
|
|
201
236
|
|
|
202
237
|
|
|
@@ -211,6 +246,10 @@ def cli(inline_args: list[str] | None = None) -> int:
|
|
|
211
246
|
- -o, --output TARGET (str): If specified, the output will be written to TARGET filename instead of stdout.
|
|
212
247
|
- --ensure_ascii (bool): Pass ensure_ascii=True to json.dumps(). Will pass False otherwise.
|
|
213
248
|
- --indent INDENT (int): Number of spaces for indentation (Default 2).
|
|
249
|
+
- --skip-json-loads (bool): Skip initial json.loads validation (needed to force schema on valid JSON).
|
|
250
|
+
- --schema SCHEMA (str): Path to a JSON Schema file that guides repairs.
|
|
251
|
+
- --schema-model MODEL (str): Pydantic v2 model in 'module:ClassName' form that guides repairs.
|
|
252
|
+
- --strict (bool): Raise on duplicate keys, missing separators, empty keys/values, and other unrecoverable structures instead of repairing them.
|
|
214
253
|
|
|
215
254
|
Returns:
|
|
216
255
|
int: Exit code of the CLI operation.
|
|
@@ -252,6 +291,21 @@ def cli(inline_args: list[str] | None = None) -> int:
|
|
|
252
291
|
default=2,
|
|
253
292
|
help="Number of spaces for indentation (Default 2)",
|
|
254
293
|
)
|
|
294
|
+
parser.add_argument(
|
|
295
|
+
"--skip-json-loads",
|
|
296
|
+
action="store_true",
|
|
297
|
+
help="Skip initial json.loads validation (needed to force schema on valid JSON)",
|
|
298
|
+
)
|
|
299
|
+
parser.add_argument(
|
|
300
|
+
"--schema",
|
|
301
|
+
metavar="SCHEMA",
|
|
302
|
+
help="Path to a JSON Schema file that guides repairs",
|
|
303
|
+
)
|
|
304
|
+
parser.add_argument(
|
|
305
|
+
"--schema-model",
|
|
306
|
+
metavar="MODEL",
|
|
307
|
+
help="Pydantic v2 model in 'module:ClassName' form that guides repairs",
|
|
308
|
+
)
|
|
255
309
|
parser.add_argument(
|
|
256
310
|
"--strict",
|
|
257
311
|
action="store_true",
|
|
@@ -269,15 +323,40 @@ def cli(inline_args: list[str] | None = None) -> int:
|
|
|
269
323
|
print("Error: You cannot pass both --inline and --output", file=sys.stderr)
|
|
270
324
|
sys.exit(1)
|
|
271
325
|
|
|
326
|
+
if args.schema and args.schema_model:
|
|
327
|
+
print("Error: You cannot pass both --schema and --schema-model", file=sys.stderr)
|
|
328
|
+
sys.exit(1)
|
|
329
|
+
|
|
330
|
+
if args.strict and (args.schema or args.schema_model):
|
|
331
|
+
print("Error: --strict cannot be used with --schema or --schema-model", file=sys.stderr)
|
|
332
|
+
sys.exit(1)
|
|
333
|
+
|
|
272
334
|
ensure_ascii = args.ensure_ascii
|
|
273
335
|
|
|
274
336
|
try:
|
|
337
|
+
schema = None
|
|
338
|
+
if args.schema:
|
|
339
|
+
with Path(args.schema).open() as fd:
|
|
340
|
+
schema = json.load(fd)
|
|
341
|
+
elif args.schema_model:
|
|
342
|
+
schema = load_schema_model(args.schema_model)
|
|
343
|
+
|
|
275
344
|
# Use from_file if a filename is provided; otherwise read from stdin.
|
|
276
345
|
if args.filename:
|
|
277
|
-
result = from_file(
|
|
346
|
+
result = from_file(
|
|
347
|
+
args.filename,
|
|
348
|
+
skip_json_loads=args.skip_json_loads,
|
|
349
|
+
strict=args.strict,
|
|
350
|
+
schema=schema,
|
|
351
|
+
)
|
|
278
352
|
else:
|
|
279
353
|
data = sys.stdin.read()
|
|
280
|
-
result = loads(
|
|
354
|
+
result = loads(
|
|
355
|
+
data,
|
|
356
|
+
skip_json_loads=args.skip_json_loads,
|
|
357
|
+
strict=args.strict,
|
|
358
|
+
schema=schema,
|
|
359
|
+
)
|
|
281
360
|
if args.inline or args.output:
|
|
282
361
|
with Path(args.output or args.filename).open(mode="w") as fd:
|
|
283
362
|
json.dump(result, fd, indent=args.indent, ensure_ascii=ensure_ascii)
|
json_repair/parse_array.py
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
from typing import TYPE_CHECKING
|
|
1
|
+
from typing import TYPE_CHECKING, Any
|
|
2
2
|
|
|
3
3
|
from .utils.constants import STRING_DELIMITERS, JSONReturnType
|
|
4
4
|
from .utils.json_context import ContextValues
|
|
@@ -6,51 +6,112 @@ from .utils.object_comparer import ObjectComparer
|
|
|
6
6
|
|
|
7
7
|
if TYPE_CHECKING:
|
|
8
8
|
from .json_parser import JSONParser
|
|
9
|
+
from .schema_repair import SchemaRepairer
|
|
9
10
|
|
|
10
11
|
|
|
11
|
-
def parse_array(
|
|
12
|
+
def parse_array(
|
|
13
|
+
self: "JSONParser",
|
|
14
|
+
schema: dict[str, Any] | bool | None = None,
|
|
15
|
+
path: str = "$",
|
|
16
|
+
) -> list[JSONReturnType]:
|
|
12
17
|
# <array> ::= '[' [ <json> *(', ' <json>) ] ']' ; A sequence of JSON values separated by commas
|
|
13
|
-
|
|
18
|
+
# Only activate schema-guided parsing if a repairer is available and schema looks array-like.
|
|
19
|
+
schema_repairer: SchemaRepairer | None = None
|
|
20
|
+
items_schema: object | None = None
|
|
21
|
+
additional_items: object | None = None
|
|
22
|
+
if schema is not None and schema is not True:
|
|
23
|
+
repairer = self.schema_repairer
|
|
24
|
+
if repairer is not None:
|
|
25
|
+
schema = repairer.resolve_schema(schema)
|
|
26
|
+
if schema is False:
|
|
27
|
+
raise ValueError("Schema does not allow any values.")
|
|
28
|
+
if schema is not True and repairer.is_array_schema(schema):
|
|
29
|
+
schema_repairer = repairer
|
|
30
|
+
items_schema = schema.get("items")
|
|
31
|
+
additional_items = schema.get("additionalItems", None)
|
|
32
|
+
|
|
33
|
+
arr: list[JSONReturnType] = []
|
|
14
34
|
self.context.set(ContextValues.ARRAY)
|
|
15
|
-
# Stop when you either find the closing parentheses or you have iterated over the entire string
|
|
16
35
|
char = self.get_char_at()
|
|
36
|
+
idx = 0
|
|
37
|
+
|
|
17
38
|
while char and char not in ["]", "}"]:
|
|
18
39
|
self.skip_whitespaces()
|
|
19
|
-
|
|
40
|
+
|
|
41
|
+
# Resolve per-item schema (tuple schemas + additionalItems) when schema guidance is active.
|
|
42
|
+
item_schema: dict[str, Any] | bool | None = None
|
|
43
|
+
drop_item = False
|
|
44
|
+
if schema_repairer is not None:
|
|
45
|
+
if isinstance(items_schema, list):
|
|
46
|
+
if idx < len(items_schema):
|
|
47
|
+
raw_schema = items_schema[idx]
|
|
48
|
+
# Tuple schemas must contain dict/bool entries only.
|
|
49
|
+
if raw_schema is not None and not isinstance(raw_schema, (dict, bool)):
|
|
50
|
+
raise ValueError("Schema must be an object.")
|
|
51
|
+
item_schema = raw_schema
|
|
52
|
+
else:
|
|
53
|
+
if additional_items is False:
|
|
54
|
+
drop_item = True
|
|
55
|
+
elif isinstance(additional_items, dict):
|
|
56
|
+
item_schema = additional_items
|
|
57
|
+
else:
|
|
58
|
+
item_schema = True
|
|
59
|
+
elif isinstance(items_schema, dict):
|
|
60
|
+
item_schema = items_schema
|
|
61
|
+
else:
|
|
62
|
+
item_schema = True
|
|
63
|
+
|
|
64
|
+
item_path = f"{path}[{idx}]"
|
|
65
|
+
|
|
20
66
|
if char in STRING_DELIMITERS:
|
|
21
|
-
#
|
|
22
|
-
# So we are going to check if this string is followed by a : or not
|
|
23
|
-
# And either parse the string or parse the object
|
|
67
|
+
# A string followed by ':' is often a missing object start; treat it as an object.
|
|
24
68
|
i = 1
|
|
25
69
|
i = self.skip_to_character(char, i)
|
|
26
70
|
i = self.scroll_whitespaces(idx=i + 1)
|
|
27
|
-
|
|
71
|
+
if self.get_char_at(i) == ":":
|
|
72
|
+
if schema_repairer is not None and not drop_item:
|
|
73
|
+
# Schema-guided object parsing, then enforce schema on the parsed object.
|
|
74
|
+
value = self.parse_object(item_schema, item_path)
|
|
75
|
+
value = schema_repairer.repair_value(value, item_schema, item_path)
|
|
76
|
+
else:
|
|
77
|
+
# No schema (or dropping): still parse to keep the cursor in sync.
|
|
78
|
+
value = self.parse_object()
|
|
79
|
+
else:
|
|
80
|
+
value = self.parse_string()
|
|
81
|
+
if schema_repairer is not None and not drop_item:
|
|
82
|
+
# Apply schema constraints/coercions to scalar values when configured.
|
|
83
|
+
value = schema_repairer.repair_value(value, item_schema, item_path)
|
|
28
84
|
else:
|
|
29
|
-
|
|
85
|
+
if schema_repairer is not None and not drop_item:
|
|
86
|
+
# Use schema-aware parsing to guide nested repairs.
|
|
87
|
+
value = self.parse_json(item_schema, item_path)
|
|
88
|
+
else:
|
|
89
|
+
# Parse normally (or discard) to keep the index aligned.
|
|
90
|
+
value = self.parse_json()
|
|
30
91
|
|
|
31
|
-
# It is possible that parse_json() returns nothing valid, so we increase by 1, unless we find an array separator
|
|
32
92
|
if ObjectComparer.is_strictly_empty(value) and self.get_char_at() not in ["]", ","]:
|
|
33
93
|
self.index += 1
|
|
34
94
|
elif value == "..." and self.get_char_at(-1) == ".":
|
|
35
95
|
self.log(
|
|
36
96
|
"While parsing an array, found a stray '...'; ignoring it",
|
|
37
97
|
)
|
|
38
|
-
|
|
98
|
+
elif not drop_item:
|
|
39
99
|
arr.append(value)
|
|
100
|
+
elif schema_repairer is not None:
|
|
101
|
+
# Record drops for visibility when schema forbids extra tuple items.
|
|
102
|
+
schema_repairer._log("Dropped extra array item not covered by schema", item_path)
|
|
40
103
|
|
|
41
|
-
|
|
104
|
+
idx += 1
|
|
42
105
|
char = self.get_char_at()
|
|
43
106
|
while char and char != "]" and (char.isspace() or char == ","):
|
|
44
107
|
self.index += 1
|
|
45
108
|
char = self.get_char_at()
|
|
46
109
|
|
|
47
|
-
# Especially at the end of an LLM generated json you might miss the last "]"
|
|
48
110
|
if char != "]":
|
|
49
111
|
self.log(
|
|
50
112
|
"While parsing an array we missed the closing ], ignoring it",
|
|
51
113
|
)
|
|
52
114
|
|
|
53
115
|
self.index += 1
|
|
54
|
-
|
|
55
116
|
self.context.reset()
|
|
56
117
|
return arr
|