def json_serialize ( obj ) : if isinstance ( obj , datetime ) : return obj . isoformat ( ) if hasattr ( obj , 'id' ) : return jsonify ( obj . id ) if hasattr ( obj , 'name' ) : return jsonify ( obj . name ) raise TypeError ( '{0} is not JSON serializable' . format ( obj ) )
json serializable types python
Simple generic JSON serializer for common objects .
def json_serialize(obj):
    """
    Simple generic JSON serializer for common objects.
    """
    if isinstance(obj, datetime):
        return obj.isoformat()

    if hasattr(obj, 'id'):
        return jsonify(obj.id)

    if hasattr(obj, 'name'):
        return jsonify(obj.name)

    raise TypeError('{0} is not JSON serializable'.format(obj))