FIX: Pylint reduce
[csit.git] / resources / libraries / python / parsers / JsonParser.py
1 # Copyright (c) 2021 Cisco and/or its affiliates.
2 # Licensed under the Apache License, Version 2.0 (the "License");
3 # you may not use this file except in compliance with the License.
4 # You may obtain a copy of the License at:
5 #
6 #     http://www.apache.org/licenses/LICENSE-2.0
7 #
8 # Unless required by applicable law or agreed to in writing, software
9 # distributed under the License is distributed on an "AS IS" BASIS,
10 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 # See the License for the specific language governing permissions and
12 # limitations under the License.
13
14 """Used to parse JSON files or JSON data strings to dictionaries"""
15
16 import json
17
18 from io import open
19
20
21 class JsonParser:
22     """Parses JSON data string or files containing JSON data strings"""
23     def __init__(self):
24         pass
25
26     @staticmethod
27     def parse_data(json_data):
28         """Return list parsed from JSON data string.
29
30         Translates JSON data into list of values/dictionaries/lists.
31
32         :param json_data: Data in JSON format.
33         :type json_data: str
34         :returns: JSON data parsed as python list.
35         :rtype: list
36         """
37         parsed_data = json.loads(json_data)
38         return parsed_data
39
40     @staticmethod
41     def parse_file(json_file):
42         """Return list parsed from file containing JSON string.
43
44         Translates JSON data found in file into list of
45         values/dictionaries/lists.
46
47         :param json_file: File with JSON type data.
48         :type json_file: str
49         :returns: JSON data parsed as python list.
50         :rtype: list
51         """
52         input_data = open(json_file, u"rt").read()
53         parsed_data = JsonParser.parse_data(input_data)
54         return parsed_data