Skip to content

Commit f49cd6b

Browse files
authored
Merge pull request #11 from ctomkow/support_None_value
Support none value
2 parents 5fbbd49 + f4e91dd commit f49cd6b

File tree

4 files changed

+39
-9
lines changed

4 files changed

+39
-9
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,8 @@ parser.find_key_value(data, 'chain', 'B')
8181

8282
> Wildcard **'*'** can be used as key(s) to match any.
8383
84-
`find_key_value(data: dict | list, key: str, value: str | int | float | bool) -> list`
84+
`find_key_value(data: dict | list, key: str, value: str | int | float | bool | NoneType) -> list`
8585

8686
- Provide JSON data as a dictionary or a list, a key as a string,
87-
and a value as a string, integer, float, or boolean.
87+
and a value as a string, integer, float, boolean, or None.
8888
- Returns a list of set(s) that contain the key:value pair.

jsonparse/VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
0.9.0
1+
0.9.1

jsonparse/parser.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ def find_key_chain(self, data: Union[dict, list], keys: list) -> list:
146146
def find_key_value(self,
147147
data: Union[dict, list],
148148
key: str,
149-
value: Union[str, int, float, bool]) -> list:
149+
value: Union[str, int, float, bool, None]) -> list:
150150
"""
151151
Search JSON data that consists of key:value pairs for all instances of
152152
provided key and value pair. The parent set that contains the key:value
@@ -254,14 +254,14 @@ def _stack_all_key_values_in_dict(self, key: str, elem: dict) -> list:
254254
def _stack_all_key_and_value_in_dict(
255255
self,
256256
key: str,
257-
value: Union[str, int, float, bool],
257+
value: Union[str, int, float, bool, None],
258258
elem: dict) -> bool:
259259

260260
if type(elem) is not dict:
261261
raise TypeError
262262
elif type(key) is not str:
263263
raise TypeError
264-
elif not isinstance(value, (str, int, float, bool)):
264+
elif not isinstance(value, (str, int, float, bool, type(None))):
265265
raise TypeError
266266

267267
if len(elem) <= 0: # don't want an empty dict on the stack
@@ -394,14 +394,14 @@ def _valid_key_value_input(
394394
self,
395395
data: Union[dict, list],
396396
key: str,
397-
value: Union[str, int, float, bool]) -> bool:
397+
value: Union[str, int, float, bool, None]) -> bool:
398398

399399
if not isinstance(data, (dict, list)):
400400
raise TypeError
401401
elif not isinstance(key, str):
402402
raise TypeError
403403
elif not key: # if key is an empty string
404404
raise ValueError
405-
elif not isinstance(value, (str, int, float, bool)):
405+
elif not isinstance(value, (str, int, float, bool, type(None))):
406406
raise TypeError
407407
return True

tests/test_parser.py

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ def complex_json(self):
7070
{
7171
"id": "0003",
7272
"type": "donut",
73-
"exists": True,
73+
"exists": None,
7474
"ppu": 7,
7575
"batters":
7676
{
@@ -226,3 +226,33 @@ def test_find_key_value_not_found(self, parser, complex_json):
226226
)
227227

228228
assert result == []
229+
230+
def test_find_key_value_none(self, parser, complex_json):
231+
232+
result = parser.find_key_value(
233+
complex_json,
234+
"exists",
235+
None
236+
)
237+
238+
assert result == [{
239+
"id": "0003",
240+
"type": "donut",
241+
"exists": None,
242+
"ppu": 7,
243+
"batters":
244+
{
245+
"batter":
246+
[
247+
{"id": "1001", "type": "Lar"},
248+
{"id": "1002", "type": "Chocolate"}
249+
]
250+
},
251+
"on_top_thing":
252+
[
253+
{"id": "5001", "type": "None"},
254+
{"id": "5002", "type": "Glazed"},
255+
{"id": "5003", "type": "Chocolate"},
256+
{"id": "5004", "type": "Maple"}
257+
]
258+
}]

0 commit comments

Comments
 (0)