Skip to content

Commit 530a6cc

Browse files
authored
Merge pull request #4 from ctomkow/key_chain_hang_bug
fix bug when key not found in key_chain
2 parents a1eab6b + 2501f02 commit 530a6cc

File tree

3 files changed

+18
-3
lines changed

3 files changed

+18
-3
lines changed

jsonparse/VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
0.6.1
1+
0.6.2

jsonparse/parser.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,10 @@ def key_chain(self, data: Union[dict, list], keys: list) -> list:
116116

117117
while len(keys) >= 1:
118118

119+
# if key is not found, return empty list
120+
if self._queue_size() == 0 and len(keys) >= 1:
121+
return []
122+
119123
queue_size_snapshot = self._queue_size()
120124
key_found = False
121125

tests/test_parser.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,6 @@ def complex_json(self):
9090
}
9191
]
9292

93-
# all_inst_of_key
9493
def test_key(self, parser, complex_json):
9594

9695
result = parser.key(complex_json, "id")
@@ -101,6 +100,11 @@ def test_key(self, parser, complex_json):
101100
'1004', '1003', '1002', '1001'
102101
]
103102

103+
def test_key_not_found(self, parser, complex_json):
104+
105+
result = parser.key(complex_json, "key_not_in_data")
106+
assert result == []
107+
104108
def test_key_empty_key(self, parser, complex_json):
105109

106110
try:
@@ -131,7 +135,6 @@ def test_key_not_list_or_dict_data(self, parser):
131135
except TypeError:
132136
assert True
133137

134-
# all_inst_of_key_chain
135138
def test_key_chain(self, parser, complex_json):
136139

137140
result = parser.key_chain(
@@ -180,6 +183,14 @@ def test_key_chain_not_list_or_dict_data(self, parser):
180183
except TypeError:
181184
assert True
182185

186+
def test_key_chain_key_not_found(self, parser, complex_json):
187+
188+
result = parser.key_chain(
189+
complex_json,
190+
["key_not_in_data"]
191+
)
192+
assert result == []
193+
183194
def test_key_chain_wildcard(self, parser, complex_json):
184195

185196
result = parser.key_chain(

0 commit comments

Comments
 (0)