1. import json
    2. class A:
    3. a = 1
    4. class B:
    5. b = 2
    6. class Test:
    7. a = A()
    8. b = B()
    9. def to_dict(obj):
    10. d = {}
    11. for name in dir(obj):
    12. value = getattr(obj, name)
    13. if not name.startswidth("__") and not callable(value):
    14. d[name] = value
    15. return d
    16. test = Test()
    17. json.loads(json.dumps(test, default=to_dict))