json-repair 0.1.4__tar.gz → 0.1.6__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.1.4
3
+ Version: 0.1.6
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.1.4"
6
+ version = "0.1.6"
7
7
  license = {file = "LICENSE"}
8
8
  authors = [
9
9
  { name="Stefano Baccianella", email="4247706+mangiucugna@users.noreply.github.com" },
@@ -46,7 +46,9 @@ class JSONParser:
46
46
  if self.get_char_at() != ":":
47
47
  self.insert_char_at(":")
48
48
  self.index += 1
49
+ self.context = "object_value"
49
50
  value = self.parse_json()
51
+ self.context = ""
50
52
  obj[key] = value
51
53
 
52
54
  if self.get_char_at() == ",":
@@ -95,6 +97,7 @@ class JSONParser:
95
97
  (char := self.get_char_at()) != '"'
96
98
  and char is not False
97
99
  and (self.context != "object_key" or char != ":")
100
+ and (self.context != "object_value" or (char != "," and char != "}"))
98
101
  ):
99
102
  self.index += 1
100
103
 
@@ -102,7 +105,7 @@ class JSONParser:
102
105
  if self.get_char_at() != '"':
103
106
  self.insert_char_at('"')
104
107
  # A fallout of the previous special case, we need to update the index only if we didn't enter that case
105
- if self.context != "object_key" or char != ":":
108
+ if (self.context != "object_key" or char != ":") and (self.context != "object_value" or (char != "," and char != "}")):
106
109
  self.index += 1
107
110
 
108
111
  return self.json_str[start:end]
@@ -153,5 +156,3 @@ def repair_json(json_str: str, return_objects: bool = False) -> any:
153
156
  if return_objects:
154
157
  return parsed_json
155
158
  return json.dumps(parsed_json)
156
-
157
- print(repair_json('{"name": "John", "age": 30, "city": New York}'))
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: json-repair
3
- Version: 0.1.4
3
+ Version: 0.1.6
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
@@ -24,8 +24,13 @@ def test_repair_json():
24
24
  repair_json('{"name": "John", "age": 30, city: "New York"}')
25
25
  == '{"name": "John", "age": 30, "city": "New York"}'
26
26
  )
27
- assert ( repair_json('{"name": "John", "age": 30, "city": New York}')
28
- == '{"name": "John", "age": 30, "city": "New York}"}'
27
+ assert (
28
+ repair_json('{"name": "John", "age": 30, "city": New York}')
29
+ == '{"name": "John", "age": 30, "city": "New York"}'
30
+ )
31
+ assert (
32
+ repair_json('{"name": John, "age": 30, "city": "New York"}')
33
+ == '{"name": "John", "age": 30, "city": "New York"}'
29
34
  )
30
35
  assert repair_json("[1, 2, 3,") == "[1, 2, 3]"
31
36
  assert (
@@ -76,7 +81,7 @@ def test_repair_json_with_objects():
76
81
  assert repair_json('{"name": "John", "age": 30, "city": New York}', True) == {
77
82
  "name": "John",
78
83
  "age": 30,
79
- "city": "New York}",
84
+ "city": "New York",
80
85
  }
81
86
  assert repair_json("[1, 2, 3,", True) == [1, 2, 3]
82
87
  assert repair_json('{"employees":["John", "Anna",', True) == {
@@ -95,4 +100,4 @@ def test_repair_json_with_objects():
95
100
  }
96
101
  assert repair_json('{"employees":["John", "Anna", "Peter', True) == {
97
102
  "employees": ["John", "Anna", "Peter"]
98
- }
103
+ }
File without changes
File without changes
File without changes