Reformat python libraries.
[csit.git] / resources / libraries / python / parsers / JsonParser.py
1 # Copyright (c) 2016 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
19 class JsonParser(object):
20     """Parses JSON data string or files containing JSON data strings"""
21     def __init__(self):
22         pass
23
24     @staticmethod
25     def parse_data(json_data):
26         """Return list parsed from JSON data string.
27
28         Translates JSON data into list of values/dictionaries/lists.
29
30         :param json_data: Data in JSON format.
31         :type json_data: str
32         :return: JSON data parsed as python list.
33         :rtype: list
34         """
35         parsed_data = json.loads(json_data)
36         return parsed_data
37
38     @staticmethod
39     def parse_file(json_file):
40         """Return list parsed from file containing JSON string.
41
42         Translates JSON data found in file into list of
43         values/dictionaries/lists.
44
45         :param json_file: File with JSON type data.
46         :type json_file: str
47         :return: JSON data parsed as python list.
48         :rtype: list
49         """
50         input_data = open(json_file).read()
51         parsed_data = JsonParser.parse_data(input_data)
52         return parsed_data