import json


def json_dumps(obj, dangerous=False, **kwargs):
    """drop-in for json.dumps that handles datetime objects.

    dangerous=True will replace unserializable values with the string '[unserializable]'.
    you should typically set this to True. it's False for legacy reasons."""
    def json_handler(obj):
        if hasattr(obj, 'isoformat'):
            return obj.isoformat()
        if dangerous:
            return '[unserializable: %s]' % (str(obj))
        raise TypeError('Object of type %s with value of %s is not JSON serializable' % (type(obj), repr(obj)))
    return json.dumps(obj, default=json_handler, **kwargs)