import json
import atexit
class JSONController:
def __init__(self, filename):
self.handler = open(filename, 'r+', encoding='utf-8')
atexit.register(self.close)
def load(self):
text = self.handler.read()
return json.loads(text)
def save(self, data):
text = json.dumps(data, ensure_ascii=True, indent=4)
self.handler.seek(0)
self.handler.truncate()
self.handler.write(text)
def close(self):
self.handler.close()