I would like to automatically convert a Python class into a dictionary so I can convert it to JSON. The question How to make a class JSON serializable suggests to use myClass.__dict__
, however, using this does not turn any inner classes into a JSON serializable object.
The following example...
class Thing():
def __init__(self, name):
self.name = name
self.children = [self.Thing2(self)]
class Thing2():
def __init__(self, parent):
self.name = parent.name + "'s child"
myThing = Thing("Clay")
print(myThing.__dict__)
yields the result...
{'name': 'Clay', 'children': [<__main__.Thing.Thing2 object at 0x00000257C4358B00>]}
which is still not JSON serializable. How do I convert a class AND INNER CLASSES into a JSON serializable object?