aboutsummaryrefslogtreecommitdiffstats
path: root/config_file/parsers/json_parser.py
blob: c017b37e4d90637034b801f2d50801253ec0b971 (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
import json

from nested_lookup import get_occurrence_of_key

from config_file.parsers.base_parser import BaseParser, ParsingError


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):
        pass

    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