I'm struggling with the following class method:
class Base:
def to_json_string(list_dictionaries):
if list_dictionaries is None or len(list_dictionaries) == 0:
return "[]"
return json.dumps(list_dictionaries)
@classmethod
def save_to_file(cls, list_objs):
filename = cls.__name__ + ".json"
with open(filename, "w") as f:
f.write(cls(Base.to_json_string(list_objs)))
I have a class Rectangle that inherits from Base. To which I can pass in the class method save_to_file and it will create me a file named Rectangle.json with a list of objects JSON formatted.
Example:
r1 = Rectangle(10, 7, 2, 8)
r2 = Rectangle(2, 4)
Rectangle.save_to_file([r1, r2])
with open("Rectangle.json", "r") as file:
print(file.read())
Output:
[{"y": 8, "x": 2, "id": 1, "width": 10, "height": 7}, {"y": 0, "x": 0, "id": 2, "width": 2, "height": 4}]
But it's raising a TypeError message "TypeError: Object of type Rectangle is not JSON serializable"
I'm new to OOP in Python and still don't figure out how to use cls properly. So my error lays here:
@classmethod
def save_to_file(cls, list_objs):
filename = cls.__name__ + ".json"
with open(filename, "w") as f:
f.write(cls(Base.to_json_string(list_objs)))