# TypeError: datetime 类型的对象无法 JSON 序列化

- **ID:** `python/fastapi-jsonable-encoder-datetime`
- **领域:** python
- **类别:** type_error
- **验证级别:** ai_generated
- **修复率:** 80%

## 根因

在 JSONResponse 中直接返回 datetime（而非 Pydantic 模型），绕过了 FastAPI 的编码器；json.dumps 无法处理 datetime。

## 版本兼容性

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

## 解决方案

1. **** (93% 成功率)
   ```
   from fastapi.encoders import jsonable_encoder; return JSONResponse(jsonable_encoder(obj))
   ```
2. **** (90% 成功率)
   ```
   Return a Pydantic model with datetime field; FastAPI serializes it
   ```

## 无效尝试

- **** — Easy to miss; inconsistent formats across endpoints. (50% 失败率)
- **** — Not interoperable with browsers and unsafe. (90% 失败率)
