Generally, we can define __str__ method to make str(obj) return what we want.
But now I want to define my Model object to return a default JSON string when using json.dumps(obj).
Is there any nice way for me to declare a method in the class to do this?
class MyClass:
...
def __json__(self):
return {'name': self.name, 'age': self.age}
obj = MyClass()
json.dumps(obj) # returns the same as json.dumps(obj.__json__)
loads'd?loadsreturnsdictis ok. @user2357112