{
  "id": "python/flask-jsonify-non-serializable-object",
  "signature": "TypeError: Object of type Product is not JSON serializable",
  "signature_zh": "类型错误：Product 类型的对象无法进行 JSON 序列化",
  "regex": "TypeError:\\ Object\\ of\\ type\\ Product\\ is\\ not\\ JSON\\ serializable",
  "domain": "python",
  "category": "type_error",
  "subcategory": null,
  "root_cause": "在 Flask 的 JSON 响应中直接返回了自定义对象（如 ORM 模型），而 Flask 默认的 JSON 编码器无法处理非基本类型。",
  "root_cause_type": "generic",
  "root_cause_zh": "在 Flask 的 JSON 响应中直接返回了自定义对象（如 ORM 模型），而 Flask 默认的 JSON 编码器无法处理非基本类型。",
  "versions": [
    {
      "version": "3.x",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    }
  ],
  "os_specific": {},
  "dead_ends": [
    {
      "action": "",
      "why_fails": "虽然可以避免报错，但输出的 JSON 是对象的字符串表示，丢失了字段结构，前端无法使用。",
      "fail_rate": 0.7,
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "why_fails": "json.dumps 同样无法处理自定义对象，会抛出相同的 TypeError，即使使用 default 参数也很难正确处理复杂模型。",
      "fail_rate": 0.9,
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "why_fails": "如果对象包含关联对象或日期时间等类型，手动转换容易遗漏或出错，导致后续序列化失败或数据不完整。",
      "fail_rate": 0.6,
      "condition": "",
      "sources": []
    }
  ],
  "workarounds": [
    {
      "action": "",
      "success_rate": 0.95,
      "how": "class CustomJSONEncoder(JSONEncoder):\n    def default(self, obj):\n        if isinstance(obj, Product):\n            return {'id': obj.id, 'name': obj.name}\n        return super().default(obj)\napp.json_encoder = CustomJSONEncoder",
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "success_rate": 0.9,
      "how": "from marshmallow import Schema, fields\nclass ProductSchema(Schema):\n    id = fields.Int()\n    name = fields.Str()\nproduct_schema = ProductSchema()\nreturn product_schema.dump(product)",
      "condition": "",
      "sources": []
    }
  ],
  "workarounds_zh": [],
  "transition_graph": {
    "leads_to": [],
    "preceded_by": [],
    "frequently_confused_with": []
  },
  "official_doc_url": null,
  "official_doc_section": null,
  "error_code": null,
  "verification_tier": "ai_generated",
  "confidence": 0.85,
  "fix_success_rate": 0.8,
  "resolvable": "true",
  "first_seen": "2024-03-15",
  "last_confirmed": "2025-01-01",
  "last_updated": "2025-01-01",
  "evidence_count": 0,
  "tags": [],
  "locale": "en",
  "aliases": []
}