forked from jemn21819/AirBnB_clone
/
file_storage.py
executable file
·48 lines (41 loc) · 1.44 KB
/
file_storage.py
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
38
39
40
41
42
43
44
45
46
47
48
#!/usr/bin/python3
"""Module for FileStorage class"""
from models.base_model import BaseModel
from models.user import User
from models.state import State
from models.city import City
from models.amenity import Amenity
from models.place import Place
from models.review import Review
import json
import os
classes = {"Amenity": Amenity, "BaseModel": BaseModel, "City": City,
"Place": Place, "Review": Review, "State": State, "User": User}
class FileStorage:
"""Class for serialization and deserialization"""
__file_path = "file.json"
__objects = {}
def all(self):
"""Return the dictionary __objects"""
return self.__objects
def new(self, obj):
""" Instantiates a new object and saves it
to the dictionary with its new key """
key = obj.__class__.__name__ + "." + obj.id
self.__objects[key] = obj
def save(self):
""" Serializes the object to the JSON file """
json_objects = {}
for key in self.__objects:
json_objects[key] = self.__objects[key].to_dict()
with open(self.__file_path, 'w') as f:
json.dump(json_objects, f)
def reload(self):
"""deserializes the JSON file to __objects"""
try:
with open(self.__file_path, 'r') as f:
jf = json.load(f)
for key in jf:
self.__objects[key] = classes[jf[key]["__class__"]](**jf[key])
except:
pass