There were other questions asking how to serialize a object of a custom class. But the answers were specific to only dumping the object alone. I, instead want to dump a dictionary that has the custom class object as a value. I want to use the json.dump method
example_dict_format = {
"name_A": [message0, message1, ...],
"name_B": [message0, ...], ...
}
class Message:
def __init__(self, time, message):
self.time = time
self.message = message
def toJSON(self):
return json.dump(self.__dict__)
def __repr__(self):
return self.toJson()
The custom class is not that complex, it is simple enough to represent as just a dictionary. I tried adding __repr__ and toJSON methods to the class, did not work. I got the following error
raise TypeError(f'Object of type {o.__class__.__name__} '
TypeError: Object of type Message is not JSON serializable
Question: How do you dump such dictionary?