Skip to content

Instantly share code, notes, and snippets.

@rockneurotiko
Created March 29, 2016 16:20
Show Gist options
  • Save rockneurotiko/53d3e0c4fce5df41160b to your computer and use it in GitHub Desktop.
Save rockneurotiko/53d3e0c4fce5df41160b to your computer and use it in GitHub Desktop.
Class that allows to save and load from json
import json
class DictSave(dict):
def save(self, fpath):
with open(fpath, 'w') as f:
json.dump(self, f)
def load(self, fpath):
with open(fpath, 'r') as f:
self = json.load(f)
return self
# Examples
if __name__ == "__main__":
a = DictSave()
a["test"] = 1
assert(a.get("test") == 1)
a.save("testjson.json")
b = DictSave().load("testjson.json")
assert(b.get("test") == 1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment