I am aware I can use a JSON encoder to make my custom class serializable. But is it possible for me to edit my custom class so it is compatible with json.dump()
?
For example can I just implement a method on the class that is called by json.dumps()
like this...
class GroundTruthEle:
def __init__(self, path, gt):
self.path = path
self.ground_truth = gt
self.results = [{
'params': {},
'result': [],
'accuracy': -1
}]
def __json__(self):
return self.__dict__
gt = GroundTruthEle('../images', ((255,0,0)...))
# Will json.dump call gt.__json__()?
json.dump(gt, outfile, ensure_ascii=False)