title: Can I covert python class to dictionary or json like Newtonsoft.Json.JsonConvert() in C#
body: <p>I'd like to make a class which can convert class to dictionary or json directly with sizable list object</p>
  <p>I'm using this dictionary uploading mongodb data</p>
  <p>I found a way multi layer dictionary converting, using dict() by overriding using __iter__ object</p>
  <pre><code>###############################################################
  # __iter__ override
  
  class Singleton(type):
      def __iter__(self):
          for attr1_name in dir(self):
              attr1 = getattr(self, attr1_name)
              if not &quot;__&quot; in attr1_name:
                  if callable(attr1):
                      for attr2_name in dir(attr1):
                          if not &quot;__&quot; in attr2_name:
                              attr2 = getattr(attr1, attr2_name)
                          yield (attr1_name, dict(attr1)) 
                  else:
                      yield (attr1_name, attr1)
  
  ################################################################
  # data structure
  
  class OrderItem(metaclass=Singleton):
      ridx = -1
      tray = -1
      batter = -1
      fry = -1
  
  class Data(metaclass=Singleton):
      request_id = 0
      order_id = &quot;random_str&quot;
      items = OrderItem
      items2 = list(OrderItem)
  
      def __init__(self) -&gt; None:
          self.items2.append(OrderItem)
          self.items2.append(OrderItem)
          
  class Order(metaclass=Singleton):
      key = &quot;&quot;
      command = &quot;order&quot;
      data = Data
  
  o = Order
  d = dict(o)
  print(d)  
  </code></pre>
  <p>This return</p>
  <pre><code>{'command': 'order', 'data': {'items': {'batter': -1, 'fry': -1, 'ridx': -1, 'tray': -1}, 'items2': [('batter', -1), ('fry', -1), ('ridx', -1), ('tray', -1)], 'order_id': 'random_str', 'request_id': 0}, 'key': ''}
  </code></pre>
  <p>I want items value is list of OrderItem class. like this</p>
  <pre><code>{&quot;command&quot;: &quot;order&quot;, &quot;data&quot;: {&quot;items&quot;: [{&quot;batter&quot;: -1, &quot;fry&quot;: -1, &quot;ridx&quot;: -1, &quot;tray&quot;: -1}, {&quot;batter&quot;: -1, &quot;fry&quot;: -1, &quot;ridx&quot;: -1, &quot;tray&quot;: -1}], &quot;order_id&quot;: &quot;random_str&quot;, &quot;request_id&quot;: 0}, &quot;key&quot;: &quot;&quot;}
  </code></pre>
  <p>Is there any way to dict() output like this?</p>
  <p>Or other ways of converting class object to multilayer list dictionary</p>