{
  "id": "python/flask-jsonify-error-non-serializable",
  "signature": "TypeError: Object of type Decimal is not JSON serializable",
  "signature_zh": "类型错误：Decimal 类型的对象不可 JSON 序列化",
  "regex": "TypeError:\\ Object\\ of\\ type\\ Decimal\\ is\\ not\\ JSON\\ serializable",
  "domain": "python",
  "category": "type_error",
  "subcategory": null,
  "root_cause": "Flask 的 jsonify 函数无法序列化 Decimal 类型数据",
  "root_cause_type": "generic",
  "root_cause_zh": "Flask 的 jsonify 函数无法序列化 Decimal 类型数据",
  "versions": [
    {
      "version": "3.9",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": "3.10",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    }
  ],
  "os_specific": {},
  "dead_ends": [
    {
      "action": "使用 str() 转换但丢失精度",
      "why_fails": "Decimal 精度可能丢失",
      "fail_rate": 0.6,
      "condition": "",
      "sources": []
    },
    {
      "action": "使用 json.dumps 替代 jsonify",
      "why_fails": "Flask 响应需要特定格式",
      "fail_rate": 0.7,
      "condition": "",
      "sources": []
    }
  ],
  "workarounds": [
    {
      "action": "自定义 JSON 编码器",
      "success_rate": 0.95,
      "how": "from flask import Flask, jsonify\nfrom decimal import Decimal\nclass CustomJSONEncoder(JSONEncoder):\n    def default(self, obj):\n        if isinstance(obj, Decimal):\n            return float(obj)\n        return super().default(obj)\napp.json_encoder = CustomJSONEncoder",
      "condition": "",
      "sources": []
    },
    {
      "action": "手动转换为 float 或 str",
      "success_rate": 0.9,
      "how": "data = {'price': float(decimal_value)}",
      "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": "2025-04-08",
  "last_confirmed": "2025-01-01",
  "last_updated": "2025-01-01",
  "evidence_count": 0,
  "tags": [],
  "locale": "en",
  "aliases": []
}