json-repair 0.16.1__tar.gz → 0.16.3__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.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: json_repair
3
- Version: 0.16.1
3
+ Version: 0.16.3
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
@@ -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.16.1"
6
+ version = "0.16.3"
7
7
  license = {file = "LICENSE"}
8
8
  authors = [
9
9
  { name="Stefano Baccianella", email="4247706+mangiucugna@users.noreply.github.com" },
@@ -1,4 +1,4 @@
1
1
  from .json_repair import repair_json as repair_json
2
2
  from .json_repair import loads as loads
3
- from .json_repair import loads as load
4
- from .json_repair import loads as from_file
3
+ from .json_repair import load as load
4
+ from .json_repair import from_file as from_file
@@ -11,7 +11,7 @@ This module will parse the JSON file following the BNF definition:
11
11
 
12
12
  <container> ::= <object> | <array>
13
13
  <array> ::= '[' [ <json> *(', ' <json>) ] ']' ; A sequence of JSON values separated by commas
14
- <object> ::= '{' [ <member> *(', ' <member>) ] '}' ; A sequence of 'members'
14
+ <object> ::= '{' [ <string> *(', ' <member>) ] '}' ; A sequence of 'members'
15
15
  <member> ::= <string> ': ' <json> ; A pair consisting of a name, and a JSON value
16
16
 
17
17
  If something is wrong (a missing parantheses or quotes for example) it will use a few simple heuristics to fix the JSON string:
@@ -55,16 +55,18 @@ class JSONParser:
55
55
  if char is False:
56
56
  return ""
57
57
  # <object> starts with '{'
58
- elif char == "{":
58
+ # but an object key must be a string
59
+ elif self.get_context() != "object_key" and char == "{":
59
60
  self.index += 1
60
61
  return self.parse_object()
61
62
  # <array> starts with '['
62
- elif char == "[":
63
+ # but an object key must be a string
64
+ elif self.get_context() != "object_key" and char == "[":
63
65
  self.index += 1
64
66
  return self.parse_array()
65
67
  # there can be an edge case in which a key is empty and at the end of an object
66
68
  # like "key": }. We return an empty string here to close the object properly
67
- elif char == "}":
69
+ elif self.get_context() != "object_key" and char == "}":
68
70
  self.log(
69
71
  "At the end of an object we found a key with missing value, skipping",
70
72
  "info",
@@ -78,10 +80,20 @@ class JSONParser:
78
80
  elif char == "“":
79
81
  return self.parse_string(string_quotes=["“", "”"])
80
82
  # <number> starts with [0-9] or minus
81
- elif self.get_context() != "" and char.isdigit() or char == "-" or char == ".":
83
+ elif (
84
+ self.get_context() != ""
85
+ and self.get_context() != "object_key"
86
+ and char.isdigit()
87
+ or char == "-"
88
+ or char == "."
89
+ ):
82
90
  return self.parse_number()
83
91
  # <boolean> could be (T)rue or (F)alse or (N)ull
84
- elif self.get_context() != "" and char.lower() in ["t", "f", "n"]:
92
+ elif (
93
+ self.get_context() != ""
94
+ and self.get_context() != "object_key"
95
+ and char.lower() in ["t", "f", "n"]
96
+ ):
85
97
  return self.parse_boolean_or_null()
86
98
  # This might be a <string> that is missing the starting '"'
87
99
  elif self.get_context() != "" and char.isalpha():
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: json_repair
3
- Version: 0.16.1
3
+ Version: 0.16.3
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
@@ -57,6 +57,7 @@ def test_repair_json():
57
57
  assert repair_json("'\"'") == '"\\\""'
58
58
  assert repair_json("'string\"") == '"string\\\""'
59
59
  assert repair_json('{foo: [}') == '{"foo": []}'
60
+ assert repair_json('''{ "a": "{ b": {} }" }''') == '{"a": "{ b"}'
60
61
  assert repair_json('{"key": "value:value"}') == '{"key": "value:value"}'
61
62
  assert repair_json('{“slanted_delimiter”: "value"}') == '{"slanted_delimiter": "value"}'
62
63
  assert (
File without changes
File without changes
File without changes