title: Python JSON encoder to support datetime?
body: <p>is there any elegant way to make Python JSON encoder support datetime? some 3rd party module or easy hack?</p>
  
  <p>I am using tornado's database wrapper to fetch some rows from db to generate a json. The query result includes a regular MySQL timestamp column.</p>
  
  <p>It's quite annoying that Python's default json encoder doesn't support its own datetime type, which is so common in all kinds of database queries.</p>
  
  <p>I don't want to modify Python's own json encoder. any good practice? Thanks a lot!</p>
  
  <p>ps: I found a dirty hack by modifying the Python JSON encoder default method:</p>
  
  <p>Change:</p>
  
  <pre><code>def default(self, o):
      raise TypeError(repr(o) + " is not JSON serializable")
  </code></pre>
  
  <p>To:</p>
  
  <pre><code>def default(self, o):
      from datetime import date
      from datetime import datetime
      if isinstance(o, datetime):
          return o.isoformat()
      elif isinstance(o, date):
          return o.isoformat()
      else:
          raise TypeError(repr(o) + " is not JSON serializable")
  </code></pre>
  
  <p>well, it will be a temporary solution just for dev environment. </p>
  
  <p>But for long term solution or production environment, this is quite ugly, and I have to do the modification every time I deploy to a new server. </p>
  
  <p>Is there a better way? I do not want to modify Python code itself, neither Tornado source code. Is there something I can do with my own project code to make this happen? preferably in one pace.</p>
  
  <p>Thanks a lot!</p>