json-repair 0.15.6__py3-none-any.whl → 0.16.1__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.
@@ -64,7 +64,7 @@ class JSONParser:
64
64
  return self.parse_array()
65
65
  # there can be an edge case in which a key is empty and at the end of an object
66
66
  # like "key": }. We return an empty string here to close the object properly
67
- elif char == "}" and self.get_context() == "object_value":
67
+ elif char == "}":
68
68
  self.log(
69
69
  "At the end of an object we found a key with missing value, skipping",
70
70
  "info",
@@ -78,13 +78,13 @@ class JSONParser:
78
78
  elif char == "“":
79
79
  return self.parse_string(string_quotes=["“", "”"])
80
80
  # <number> starts with [0-9] or minus
81
- elif char.isdigit() or char == "-" or char == ".":
81
+ elif self.get_context() != "" and char.isdigit() or char == "-" or char == ".":
82
82
  return self.parse_number()
83
83
  # <boolean> could be (T)rue or (F)alse or (N)ull
84
- elif char.lower() in ["t", "f", "n"]:
84
+ elif self.get_context() != "" and char.lower() in ["t", "f", "n"]:
85
85
  return self.parse_boolean_or_null()
86
86
  # This might be a <string> that is missing the starting '"'
87
- elif char.isalpha():
87
+ elif self.get_context() != "" and char.isalpha():
88
88
  return self.parse_string()
89
89
  # If everything else fails, we just ignore and move on
90
90
  else:
@@ -131,6 +131,8 @@ class JSONParser:
131
131
  "info",
132
132
  )
133
133
  break
134
+ elif key == "":
135
+ self.index += 1
134
136
 
135
137
  # We reached the end here
136
138
  if (self.get_char_at() or "}") == "}":
@@ -172,6 +174,7 @@ class JSONParser:
172
174
  def parse_array(self) -> List[Any]:
173
175
  # <array> ::= '[' [ <json> *(', ' <json>) ] ']' ; A sequence of JSON values separated by commas
174
176
  arr = []
177
+ self.set_context("array")
175
178
  # Stop when you either find the closing parentheses or you have iterated over the entire string
176
179
  while (self.get_char_at() or "]") != "]":
177
180
  self.skip_whitespaces_at()
@@ -210,6 +213,7 @@ class JSONParser:
210
213
  self.index -= 1
211
214
 
212
215
  self.index += 1
216
+ self.reset_context()
213
217
  return arr
214
218
 
215
219
  def parse_string(self, string_quotes=False) -> str:
@@ -294,25 +298,41 @@ class JSONParser:
294
298
  else:
295
299
  # Check if eventually there is a rstring delimiter, otherwise we bail
296
300
  i = 1
297
- context = self.get_context()
298
301
  next_c = self.get_char_at(i)
299
302
  while next_c and next_c != rstring_delimiter:
300
303
  # If we are in an object context, let's check for the right delimiters
301
304
  if (
302
- (context == "object_key" and next_c == ":")
303
- or (context == "object_value" and next_c in ["}", ","])
304
- or (context == "" and next_c in ["]", ","])
305
+ next_c == lstring_delimiter
306
+ or ("object_key" in self.context and next_c == ":")
307
+ or ("object_value" in self.context and next_c in ["}", ","])
308
+ or ("array" in self.context and next_c in ["]", ","])
305
309
  ):
306
310
  break
307
311
  i += 1
308
312
  next_c = self.get_char_at(i)
309
313
  if next_c == rstring_delimiter:
310
- self.log(
311
- "While parsing a string, we a misplaced quote that would have closed the string but has a different meaning here, ignoring it",
312
- "info",
313
- )
314
- self.index += 1
315
- char = self.get_char_at()
314
+ # But this might not be it! This could be just a missing comma
315
+ # We need to check if we find a rstring_delimiter and a colon after
316
+ i += 1
317
+ next_c = self.get_char_at(i)
318
+ while next_c and next_c != rstring_delimiter:
319
+ i += 1
320
+ next_c = self.get_char_at(i)
321
+ i += 1
322
+ next_c = self.get_char_at(i)
323
+ while next_c and next_c != ":":
324
+ if next_c in [lstring_delimiter, rstring_delimiter, ","]:
325
+ break
326
+ i += 1
327
+ next_c = self.get_char_at(i)
328
+ # Only if we fail to find a ':' then we know this is misplaced quote
329
+ if next_c != ":":
330
+ self.log(
331
+ "While parsing a string, we a misplaced quote that would have closed the string but has a different meaning here, ignoring it",
332
+ "info",
333
+ )
334
+ self.index += 1
335
+ char = self.get_char_at()
316
336
 
317
337
  if (
318
338
  char
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: json_repair
3
- Version: 0.15.6
3
+ Version: 0.16.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
@@ -0,0 +1,7 @@
1
+ json_repair/__init__.py,sha256=AlNie5y6BZBioGi5fzTAUvum_y0U5aL5aNsuQ_68LQc,175
2
+ json_repair/json_repair.py,sha256=P3jzLaa70VyyE-kjRhijJ4gEPMXba3FmsocJF7bPB9I,21417
3
+ json_repair-0.16.1.dist-info/LICENSE,sha256=wrjQo8MhNrNCicXtMe3MHmS-fx8AmQk1ue8AQwiiFV8,1076
4
+ json_repair-0.16.1.dist-info/METADATA,sha256=UCkuATUR-x0kFLbunLN_htTmUy5T5ZAzXOFhEVeISNc,7355
5
+ json_repair-0.16.1.dist-info/WHEEL,sha256=GJ7t_kWBFywbagK5eo9IoUwLW6oyOeTKmQ-9iHFVNxQ,92
6
+ json_repair-0.16.1.dist-info/top_level.txt,sha256=7-VZwZN2CgB_n0NlSLk-rEUFh8ug21lESbsblOYuZqw,12
7
+ json_repair-0.16.1.dist-info/RECORD,,
@@ -1,7 +0,0 @@
1
- json_repair/__init__.py,sha256=AlNie5y6BZBioGi5fzTAUvum_y0U5aL5aNsuQ_68LQc,175
2
- json_repair/json_repair.py,sha256=qQosSMpX3r1adMubgHNZ9gT0lSlZBn3Id_98yS1ITwk,20270
3
- json_repair-0.15.6.dist-info/LICENSE,sha256=wrjQo8MhNrNCicXtMe3MHmS-fx8AmQk1ue8AQwiiFV8,1076
4
- json_repair-0.15.6.dist-info/METADATA,sha256=BNtrdGCSn8NC9gBrWdmX2bRQv6VJgEcwgCM5_SWLXUY,7355
5
- json_repair-0.15.6.dist-info/WHEEL,sha256=GJ7t_kWBFywbagK5eo9IoUwLW6oyOeTKmQ-9iHFVNxQ,92
6
- json_repair-0.15.6.dist-info/top_level.txt,sha256=7-VZwZN2CgB_n0NlSLk-rEUFh8ug21lESbsblOYuZqw,12
7
- json_repair-0.15.6.dist-info/RECORD,,