I'd like to extend the JSONEncoder to call __json__ of an object to convert it to something serializable.
I've tried this:
import json
class Foo(object):
def __json__(self):
return dict(a=1, b=2)
class MyJSONEncoder(json.JSONEncoder):
"""
Use __json__ attr or callable of object for JSON serialization.
"""
def default(self, o):
if hasattr(o, '__json__'):
if callable(o.__json__):
o = o.__json__()
else:
o = o.__json__
return super(MyJSONEncoder, self).default(o)
json.dumps(Foo(), cls=MyJSONEncoder)
And get this:
Traceback (most recent call last):
File "./foo.py", line 20, in <module>
json.dumps(Foo(), cls=MyJSONEncoder)
File "/usr/lib64/python2.7/json/__init__.py", line 250, in dumps
sort_keys=sort_keys, **kw).encode(obj)
File "/usr/lib64/python2.7/json/encoder.py", line 207, in encode
chunks = self.iterencode(o, _one_shot=True)
File "/usr/lib64/python2.7/json/encoder.py", line 270, in iterencode
return _iterencode(o, 0)
File "./foo.py", line 18, in default
return super(MyJSONEncoder, self).default(o)
File "/usr/lib64/python2.7/json/encoder.py", line 184, in default
raise TypeError(repr(o) + " is not JSON serializable")
TypeError: {'a': 1, 'b': 2} is not JSON serializable