blob: c7c20fb08ad0f61bba4c69e72f06d873e46f97a3 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
|
import json
from nested_lookup import get_occurrence_of_key
from config_file.parsers.base_parser import BaseParser, ParsingError
from config_file.parsers.parse_value import parse_value
from config_file.utils import split_on_dot
class JsonParser(BaseParser):
def __init__(self, file_contents: str):
"""Reads in the file contents into the parser."""
super().__init__(file_contents)
def parse(self, file_contents: str):
try:
return json.loads(file_contents)
except json.decoder.JSONDecodeError as error:
raise ParsingError(error)
def get(self, section_key, parse_type=True):
split_keys = split_on_dot(section_key)
result = self.parsed_content
for key in split_keys:
result = result[key]
return parse_value(result) if parse_type else result
def set(self, section_key, value):
pass
def delete(self, section_key):
pass
def stringify(self) -> str:
return json.dumps(self.parsed_content)
def has(self, search_key: str) -> bool:
return get_occurrence_of_key(self.parsed_content, key=search_key) > 0
|