diff options
| author | Eugene Triguba <eugenetriguba@gmail.com> | 2020-01-27 20:34:16 -0600 |
|---|---|---|
| committer | Eugene Triguba <eugenetriguba@gmail.com> | 2020-01-27 20:35:36 -0600 |
| commit | e4b5dc647cbc560f5e9e1fa492b5653ca37a241c (patch) | |
| tree | dc3f0884b0869737d608c29bb7b937426a025df7 /config_file | |
| parent | Add start of json_parser implementaiton (diff) | |
| download | config-file-e4b5dc647cbc560f5e9e1fa492b5653ca37a241c.zip | |
Add nested_lookup, and get test working for json has() and stringify()
nested_lookup is a dependency that will help working
with XML, YAML, and JSON since we can search deeply nested
structures easily with it.
Diffstat (limited to 'config_file')
| -rw-r--r-- | config_file/parsers/json_parser.py | 17 |
1 files changed, 3 insertions, 14 deletions
diff --git a/config_file/parsers/json_parser.py b/config_file/parsers/json_parser.py index 504ff02..c017b37 100644 --- a/config_file/parsers/json_parser.py +++ b/config_file/parsers/json_parser.py @@ -1,5 +1,7 @@ import json +from nested_lookup import get_occurrence_of_key + from config_file.parsers.base_parser import BaseParser, ParsingError @@ -27,17 +29,4 @@ class JsonParser(BaseParser): return json.dumps(self.parsed_content) def has(self, search_key: str) -> bool: - return self._search(self.parsed_content, search_key) - - def _search(self, data: dict, search_item: str): - for key, value in data.items(): - if search_item in data: - return True - elif not isinstance(value, dict): - continue - elif search_item in value: - return True - else: - self._search(value, search_item) - - return False + return get_occurrence_of_key(self.parsed_content, key=search_key) > 0 |