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

- **ID:** `python/flask-session-serialization-error`
- **领域:** python
- **类别:** type_error
- **验证级别:** ai_generated
- **修复率:** 80%

## 根因

在Flask会话cookie中存储不可JSON序列化的值（datetime、Decimal、自定义类）。

## 版本兼容性

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

## 解决方案

1. **** (95% 成功率)
   ```
   session['ts'] = dt.isoformat()\n# read back: datetime.fromisoformat(session['ts'])
   ```
2. **** (90% 成功率)
   ```
   app.config['SESSION_TYPE'] = 'redis'\nSession(app)
   ```

## 无效尝试

- **** — Unrelated to serialization. (95% 失败率)
- **** — Only affects expiry, not encoding. (90% 失败率)
