title: Flask jsonify a list of objects
body: <p>I have a list of objects that I need to jsonify.  I've looked at the flask jsonify docs, but I'm just not getting it.</p>
  
  <p>My class has several inst-vars, each of which is a string: <code>gene_id</code>, <code>gene_symbol</code>, <code>p_value</code>.  What do I need to do to make this serializable as JSON?</p>
  
  <p>My naive code:</p>
  
  <pre><code>jsonify(eqtls = my_list_of_eqtls)
  </code></pre>
  
  <p>Results in:</p>
  
  <pre><code>TypeError: &lt;__main__.EqtlByGene object at 0x1073ff790&gt; is not JSON serializable
  </code></pre>
  
  <p>Presumably I have to tell jsonify how to serialize an <code>EqtlByGene</code>, but I can't find an example that shows how to serialize an instance of a class.</p>
  
  <p>I've been trying to follow some of the suggestions show below to create my own JSONEncoder subclass.  My code is now:</p>
  
  <pre><code>class EqtlByGene(Resource):
  
      def __init__(self, gene_id, gene_symbol, p_value):
          self.gene_id = gene_id
          self.gene_symbol = gene_symbol
          self.p_value = p_value
  
  class EqtlJSONEncoder(JSONEncoder):
      def default(self, obj):
          if isinstance(obj, EqtlByGene):
              return {
                     'gene_id'     : obj.gene_id,
                     'gene_symbol' : obj.gene_symbol,
                     'p_value'     : obj.p_value
              }
          return super(EqtlJSONEncoder, self).default(obj)
  
  class EqtlByGeneList(Resource):
      def get(self):
          eqtl1 = EqtlByGene(1, 'EGFR', 0.1)
          eqtl2 = EqtlByGene(2, 'PTEN', 0.2)
          eqtls = [eqtl1, eqtl2]
          return jsonify(eqtls_by_gene = eqtls)
  
  api.add_resource(EqtlByGeneList, '/eqtl/eqtlsbygene')
  app.json_encoder(EqtlJSONEncoder)
  if __name__ == '__main__':
      app.run(debug=True)
  </code></pre>
  
  <p>When I try to reach it via curl, I get:</p>
  
  <pre><code>TypeError(repr(o) + " is not JSON serializable")
  </code></pre>