json-repair 0.21.0__py3-none-any.whl → 0.23.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.
@@ -1,7 +1,7 @@
1
1
  """
2
2
  This module will parse the JSON file following the BNF definition:
3
3
 
4
- <json> ::= <primitive> | <container>
4
+ <json> ::= <container>
5
5
 
6
6
  <primitive> ::= <number> | <string> | <boolean>
7
7
  ; Where:
@@ -89,15 +89,32 @@ class JSONParser:
89
89
  def parse(
90
90
  self,
91
91
  ) -> Union[JSONReturnType, Tuple[JSONReturnType, List[Dict[str, str]]]]:
92
+ json = self.parse_json()
93
+ if self.index < len(self.json_str):
94
+ json = [json]
95
+ last_index = self.index
96
+ while self.index < len(self.json_str):
97
+ j = self.parse_json()
98
+ if j != "":
99
+ json.append(j)
100
+ if self.index == last_index:
101
+ self.index += 1
102
+ last_index = self.index
103
+ if len(json) == 1:
104
+ json = json[0]
105
+ elif len(json) == 0:
106
+ json = ""
92
107
  if self.logger.log_level == "none":
93
- return self.parse_json()
108
+ return json
94
109
  else:
95
- return self.parse_json(), self.logger.log
110
+ return json, self.logger.log
96
111
 
97
112
  def parse_json(
98
113
  self,
99
114
  ) -> JSONReturnType:
100
115
  char = self.get_char_at()
116
+ # This parser will ignore any basic element (string or number) that is not inside an array or object
117
+ is_in_context = len(self.context) > 0
101
118
  # False means that we are at the end of the string provided, is the base case for recursion
102
119
  if char is False:
103
120
  return ""
@@ -120,10 +137,10 @@ class JSONParser:
120
137
  )
121
138
  return ""
122
139
  # <string> starts with a quote
123
- elif char in ['"', "'", "“"] or char.isalpha():
140
+ elif is_in_context and (char in ['"', "'", "“"] or char.isalpha()):
124
141
  return self.parse_string()
125
142
  # <number> starts with [0-9] or minus
126
- elif char.isdigit() or char == "-" or char == ".":
143
+ elif is_in_context and (char.isdigit() or char == "-" or char == "."):
127
144
  return self.parse_number()
128
145
  # If everything else fails, we just ignore and move on
129
146
  else:
@@ -304,14 +321,6 @@ class JSONParser:
304
321
  "While parsing a string, we found a literal instead of a quote",
305
322
  "info",
306
323
  )
307
- if self.get_context() == "":
308
- # A string literal in the wild isn't a valid json and not something we can fix
309
- self.log(
310
- "While parsing a string, we found a literal outside of context, ignoring it",
311
- "info",
312
- )
313
- self.index += 1
314
- return self.parse_json()
315
324
  self.log(
316
325
  "While parsing a string, we found no starting quote. Will add the quote back",
317
326
  "info",
@@ -656,3 +665,6 @@ def from_file(
656
665
  fd.close()
657
666
 
658
667
  return jsonobj
668
+
669
+
670
+ repair_json("[]{}")
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: json_repair
3
- Version: 0.21.0
3
+ Version: 0.23.0
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=IIzSm1DsCRrr8seF3UeMZXwxcq-tE3j-8d1WBxvEJvE,178
2
+ json_repair/json_repair.py,sha256=XXnrrQxMpH2N-zSPHDqI24OaaQd3EyVSUJAgxpfEHfg,27651
3
+ json_repair-0.23.0.dist-info/LICENSE,sha256=wrjQo8MhNrNCicXtMe3MHmS-fx8AmQk1ue8AQwiiFV8,1076
4
+ json_repair-0.23.0.dist-info/METADATA,sha256=xkGqObtmpYdxnYoTBzCPhM1g4xXcMlOjV3SuN4EO2LM,7333
5
+ json_repair-0.23.0.dist-info/WHEEL,sha256=GJ7t_kWBFywbagK5eo9IoUwLW6oyOeTKmQ-9iHFVNxQ,92
6
+ json_repair-0.23.0.dist-info/top_level.txt,sha256=7-VZwZN2CgB_n0NlSLk-rEUFh8ug21lESbsblOYuZqw,12
7
+ json_repair-0.23.0.dist-info/RECORD,,
@@ -1,7 +0,0 @@
1
- json_repair/__init__.py,sha256=IIzSm1DsCRrr8seF3UeMZXwxcq-tE3j-8d1WBxvEJvE,178
2
- json_repair/json_repair.py,sha256=ry94U3QoJwVgyG1qeQNEb8Qt8NtCLpCGR41GBA7tozY,27320
3
- json_repair-0.21.0.dist-info/LICENSE,sha256=wrjQo8MhNrNCicXtMe3MHmS-fx8AmQk1ue8AQwiiFV8,1076
4
- json_repair-0.21.0.dist-info/METADATA,sha256=obBsHuNN7Ph5zX77VHmER2O9A61F3MXGBreEowdr-so,7333
5
- json_repair-0.21.0.dist-info/WHEEL,sha256=GJ7t_kWBFywbagK5eo9IoUwLW6oyOeTKmQ-9iHFVNxQ,92
6
- json_repair-0.21.0.dist-info/top_level.txt,sha256=7-VZwZN2CgB_n0NlSLk-rEUFh8ug21lESbsblOYuZqw,12
7
- json_repair-0.21.0.dist-info/RECORD,,