Option for jsonify handles dates by converting to isoformat #466

brianmhunt opened this Issue Mar 30, 2012 · 2 comments


None yet
2 participants

When jsonify is called on an object with a 'date' it throws:

TypeError: datetime.date(2012, 3, 30) is not JSON serializable

There is an innocuous fix - see http://stackoverflow.com/questions/455580

def date_handler(obj):
    if hasattr(obj, 'isoformat'):
        return obj.isoformat()
        raise TypeError, 'Object of type %s with value of %s is not JSON serializable' % (type(obj), repr(obj))

And https://github.com/mitsuhiko/flask/blob/master/flask/helpers.py#L142 becomes:

json_str = json.dumps(dict(*args, **kwargs), indent=None, default=date_handler)

This strikes me as preferable to TypeError.

Alternatively, jsonify could accept a default argument that is passed to json.dumps.


kisielk commented Mar 30, 2012

See #220 which is basically asking for a more general version of the same thing.

brianmhunt closed this Mar 30, 2012

Thanks kisielk. I was surprised this issue was not already listed, but I must have overlooked it. Closing as a duplicate.

@jfinkels jfinkels added a commit to jfinkels/flask that referenced this issue Apr 1, 2012

@jfinkels jfinkels One possible solution to issue #466. d5d7408
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment