aboutsummaryrefslogtreecommitdiffstats
path: root/tests/optf-cmso/cmso/attlibs/JSONUtils.py
blob: 5df1e5c739b2817a21fe4bd1b59cefc4759a7fef (plain)
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
import json

from deepdiff import DeepDiff

class JSONUtils:
    """JSONUtils is common resource for simple json helper keywords."""
    
    def json_equals(self, left, right):
        """JSON Equals takes in two strings or json objects, converts them into json if needed and then compares them, returning if they are equal or not."""
        if isinstance(left, basestring):
            left_json = json.loads(left);
        else:
            left_json = left;
        if isinstance(right, basestring):
            right_json = json.loads(right);
        else:
            right_json = right;
            
        ddiff = DeepDiff(left_json, right_json, ignore_order=True);
        if ddiff == {}:
            return True;
        else:
            return False;
    
    def json_escape(self, jsonObject):
        jsonstr = json.dumps(jsonObject)
        outstr = jsonstr.replace('"', '\\"').replace('\n', '\\n')
        return  outstr    
    
    def make_list_into_dict(self, listOfDicts, key):
        """ Converts a list of dicts that contains a field that has a unique key into a dict of dicts """
        d = {}
        if isinstance(listOfDicts, list):
            for thisDict in listOfDicts:
                v = thisDict[key]
                d[v] = thisDict
        return d