json-repair 0.13.0__py3-none-any.whl → 0.13.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.
@@ -34,6 +34,7 @@ class JSONParser:
34
34
  self.index = 0
35
35
  # This is used in the object member parsing to manage the special cases of missing quotes in key or value
36
36
  self.context = ""
37
+ self.context_stack = []
37
38
 
38
39
  def parse(self) -> Union[Dict[str, Any], List[Any], str, float, int, bool, None]:
39
40
  return self.parse_json()
@@ -97,7 +98,7 @@ class JSONParser:
97
98
 
98
99
  # We are now searching for they string key
99
100
  # Context is used in the string parser to manage the lack of quotes
100
- self.context = "object_key"
101
+ self.update_context("object_key")
101
102
 
102
103
  self.skip_whitespaces_at()
103
104
 
@@ -119,12 +120,13 @@ class JSONParser:
119
120
  if (self.get_char_at() or "") != ":":
120
121
  self.insert_char_at(":")
121
122
  self.index += 1
122
- self.context = "object_value"
123
+ self.update_context("")
124
+ self.update_context("object_value")
123
125
  # The value can be any valid json
124
126
  value = self.parse_json()
125
127
 
126
128
  # Reset context since our job is done
127
- self.context = ""
129
+ self.update_context("")
128
130
  obj[key] = value
129
131
 
130
132
  if (self.get_char_at() or "") in [",", "'", '"']:
@@ -157,6 +159,9 @@ class JSONParser:
157
159
  while char and (char.isspace() or char == ","):
158
160
  self.index += 1
159
161
  char = self.get_char_at()
162
+ # If this is the right value of an object and we are closing the object, it means the array is over
163
+ if self.context == "object_value" and char == "}":
164
+ break
160
165
 
161
166
  # Especially at the end of an LLM generated json you might miss the last "]"
162
167
  char = self.get_char_at()
@@ -166,6 +171,7 @@ class JSONParser:
166
171
  # Remove trailing "," before adding the "]"
167
172
  self.remove_char_at()
168
173
  self.insert_char_at("]")
174
+ self.index -= 1
169
175
 
170
176
  self.index += 1
171
177
  return arr
@@ -319,6 +325,19 @@ class JSONParser:
319
325
  except IndexError:
320
326
  return
321
327
 
328
+ def update_context(self, value: str) -> None:
329
+ # If a value is provided update the context variable and save in stack
330
+ if value:
331
+ if self.context:
332
+ self.context_stack.append(self.context)
333
+ self.context = value
334
+ # Otherwise pop and update the context, or empty if the stack is empty
335
+ else:
336
+ try:
337
+ self.context = self.context_stack.pop()
338
+ except Exception:
339
+ self.context = ""
340
+
322
341
 
323
342
  def repair_json(
324
343
  json_str: str, return_objects: bool = False, skip_json_loads: bool = False
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: json_repair
3
- Version: 0.13.0
3
+ Version: 0.13.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=DB220fZ1BCf--9CeP6AzL2FCk9tpE1Eh3WxgFo33P88,15460
3
+ json_repair-0.13.1.dist-info/LICENSE,sha256=wrjQo8MhNrNCicXtMe3MHmS-fx8AmQk1ue8AQwiiFV8,1076
4
+ json_repair-0.13.1.dist-info/METADATA,sha256=gcfdAU5nhvlHlE3wMvGcwHRQUzgPT2p08cgDg7iyLvw,7200
5
+ json_repair-0.13.1.dist-info/WHEEL,sha256=GJ7t_kWBFywbagK5eo9IoUwLW6oyOeTKmQ-9iHFVNxQ,92
6
+ json_repair-0.13.1.dist-info/top_level.txt,sha256=7-VZwZN2CgB_n0NlSLk-rEUFh8ug21lESbsblOYuZqw,12
7
+ json_repair-0.13.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=BHDcvo8C-vc3QO4dhgD5cP7IZVkGWGDbv8hNmQH1B-I,14653
3
- json_repair-0.13.0.dist-info/LICENSE,sha256=wrjQo8MhNrNCicXtMe3MHmS-fx8AmQk1ue8AQwiiFV8,1076
4
- json_repair-0.13.0.dist-info/METADATA,sha256=E6v_JcBwXe4oMJhtxgF-9-24kG2j5LiH-ivqufNHFpA,7200
5
- json_repair-0.13.0.dist-info/WHEEL,sha256=GJ7t_kWBFywbagK5eo9IoUwLW6oyOeTKmQ-9iHFVNxQ,92
6
- json_repair-0.13.0.dist-info/top_level.txt,sha256=7-VZwZN2CgB_n0NlSLk-rEUFh8ug21lESbsblOYuZqw,12
7
- json_repair-0.13.0.dist-info/RECORD,,