'How to convert nested classes to json or dict?

I have class of classes as below:


class Base:
  def __init__(self):
    self.var1 = “variable”

class Child:
  def __init__(self):
    self.base = Base()
    self.var2 = False

class RootChild:
  def __init__(self):
    self.child = Child()
    self.var3 = “variable”
    self.base = Base()

I want to save an instance of ‘RootChild’ with all fields of it represented in json. There are a lot of topics about this task, but I could not understand their differences and use cases. What are most pythonic solutions to this problem?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source