In my web application, I often need to serialize objects as JSON. Not all objects are JSON-serializable by default, so I use my own encode_complex method, which is passed to simplejson.dumps as follows: simplejson.dumps(context, default=self.encode_complex)
Is it possible to define my own magic method called __json__(self) , and then use code similar to the following in the encode_complex method?
def encode_complex(self, obj):
source share