title: TypeError: Object of type X is not JSON serializable
body: <p>How could I return a proper dict that has an array of Objects on it?
  I'm making a flask API and I'm not getting a good result that I can consume later as a json object.</p>
  
  <p>Here's the output and how I'm making it
  Output:</p>
  
  <pre><code>{
      "starttime": "2018-10-19 12:10:00",
      "endtime": "2018-10-19 12:11:00",
      "env": "TEST",
      "statistics": "{\"TEST\": {\"queryUsers\": {\"calls\": 43, \"avgtime\": 1, \"errors\": 0, \"first_error\": null, \"last_error\": null, \"timeouts\": 0, \"first_to\": null, \"last_to\": null},
  ...
  </code></pre>
  
  <p>Router:</p>
  
  <pre><code>def get(self, env):
  if env == 'TEST':
      start = "2018-10-19 12:10:00"
      end = "2018-10-19 12:11:00"
      stats = self.scontroller.getStatistics(start, end, 'ALL')
  
      return {'starttime': start, 'endtime': end, 'env': env, 'statistics': json.dumps(stats, cls=MyJSONEncoder)}, 200
  
  return "ENV not found", 404
  </code></pre>
  
  <p>And if i try to return this instead</p>
  
  <pre><code>return {'starttime': start, 'endtime': end, 'env': env, 'statistics': stats}, 200
  </code></pre>
  
  <p>I'll get:</p>
  
  <pre><code>    raise TypeError(f'Object of type {o.__class__.__name__} '
      TypeError: Object of type Statistic is not JSON serializable
  </code></pre>
  
  <p>My json encoder:</p>
  
  <pre><code>class MyJSONEncoder(JSONEncoder):
      def default(self, obj):
          if isinstance(obj, Metric):
              pass
          if isinstance(obj, Statistic):
              return vars(obj)
  
          return JSONEncoder.default(self, obj)
  </code></pre>
  
  <p>The dict that should go in the statistics follows the model below:</p>
  
  <pre><code>{
    "ENVNAME":{
              "APINAME":{
                             &lt;Object Statistic&gt;,
                             &lt;Object Statistic&gt;,
                             &lt;Object Statistic&gt;,
                             &lt;Object Statistic&gt;
                         },
              "ANOTHERAPI": {...}
             }
  }
  </code></pre>
  
  <p>And it's mounted that way:</p>
  
  <p>Statistic.py</p>
  
  <pre><code>class Statistic():
  
  def __init__(self, calls, avgtime, errors, first_error, last_error, timeouts, first_to, last_to):
      self.calls = calls
      self.avgtime = avgtime
      self.errors = errors
      self.first_error = first_error
      self.last_error = last_error
      self.timeouts = timeouts
      self.first_to = first_to
      self.last_to = last_to
  </code></pre>
  
  <p>On the controller:</p>
  
  <pre><code>stats[env][api] = Statistic(calls, avgtime, errors, first_er, last_er, tos, first_to, last_to)
  </code></pre>
  
  <p>Any help is appreciated.</p>
  
  <p>Thank you!</p>