import json def handler(obj): if hasattr(obj, 'isoformat'): return obj.isoformat() else: raise TypeError, 'Object of type %s with value of %s is not JSON serializable' % (type(obj), repr(obj)) def json_dump(obj): return json.dumps(obj, default=handler)