Python 2.7 problems with sub-classing dict and mirroring to json
Question: So I am trying to create a class that behaves like a dict but also copies
itself to a .json file whenever a change is made to the dict. I've got it
working for the most part; but where I have trouble is when I append something
to a list inside the dict; it updates the dict but not the .json file
associated with the dict.

I am sorry for the lengthy code block, I tried to condense as much as possible
but it still turned out fairly lengthy.

    
    
    import json
    import os.path
    
    class JDict(dict):
        def __init__(self, filepath, *args, **kwargs):
            if str(filepath).split('.')[-1] == 'json':
                self.filepath = str(filepath)
            else:
                self.filepath = str('{}.json'.format(filepath))
    
            if os.path.isfile(self.filepath):
                super(JDict, self).__init__(self.read())
            else:
                super(JDict, self).__init__(*args, **kwargs)
                self.write()
    
        def __setitem__(self, key, value):
            dict.__setitem__(self, key, value)
            self.write()
    
        def write(self):
            with open(self.filepath, 'w') as outfile:
                json.dump(self, outfile, sort_keys = True, indent = 4,
                    ensure_ascii=False)
    
        def read(self):
            with open(self.filepath, 'r') as infile:
                jsonData = json.load(infile)
            self = jsonData
            return self
    
    def parseJson(filepath):
        with open(filepath, 'r') as infile:
            jsonData = json.load(infile) 
        return jsonData
    
    test = JDict("test.json", {
        "TestList": [
            "element1"
            ]
        })
    
    test["TestList"].append("element2")
    
    try:
        if test["TestList"][1] == parseJson("test.json")["TestList"][1]:
            print 'Success'
    except IndexError:
        print 'Failure'
    


Answer: So the reason I was having trouble was because **setitem** isn't called on the
dict (or even on a list for that matter) when you append an element to a
member list.

Soooo...if anyone else is having this issue; I ended up sub-classing both list
and dict datatypes and making them sort of helper classes to a new class
called QJson

all lists and dicts under the helper class QJson get converted to JDicts and
JLists respectively.

and QJson is itself a dict

The code is REALLY long and monolithic at this point so here is a link to my
[github](https://github.com/deeredman1991/QJson/blob/master/QJson.py) enjoy.
:)

