# TypeError: datetime 类型对象不可 JSON 序列化。

- **ID:** `python/flask-jsonify-not-serializable`
- **领域:** python
- **类别:** type_error
- **验证级别:** ai_generated
- **修复率:** 80%

## 根因

jsonify（或响应）序列化包含 datetime、Decimal、UUID 或未注册 JSON 编码器的自定义类的对象。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| 2.x | active | — | — |
| 3.x | active | — | — |

## 解决方案

1. **** (95% 成功率)
   ```
   Custom encoder:
class CustomJSONProvider(DefaultJSONProvider):
    @staticmethod
    def default(o):
        if isinstance(o, datetime): return o.isoformat()
        return super().default(o)
app.json = CustomJSONProvider(app)
   ```
2. **** (90% 成功率)
   ```
   Or convert before returning: `return jsonify({'created': user.created_at.isoformat()})`
   ```

## 无效尝试

- **** — Produces `"<__main__.User object at 0x...>"`, useless to clients. (90% 失败率)
- **** — Breaks jsonify entirely. (95% 失败率)
