{
  "id": "python/flask-jsonify-error-nonserializable",
  "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's jsonify uses default JSON encoder which cannot serialize Decimal.",
  "root_cause_type": "generic",
  "root_cause_zh": "Flask的jsonify使用默认JSON编码器，无法序列化Decimal。",
  "versions": [
    {
      "version": "3.x",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    }
  ],
  "os_specific": {},
  "dead_ends": [
    {
      "action": "Converting Decimal to float manually, losing precision.",
      "why_fails": "Float may introduce rounding errors for financial data.",
      "fail_rate": 0.6,
      "condition": "",
      "sources": []
    },
    {
      "action": "Using json.dumps with default=str but not integrating with jsonify.",
      "why_fails": "jsonify does not use the same encoder as json.dumps by default.",
      "fail_rate": 0.7,
      "condition": "",
      "sources": []
    }
  ],
  "workarounds": [
    {
      "action": "Create a custom JSON encoder and set it on the Flask app.",
      "success_rate": 0.9,
      "how": "from flask import Flask, jsonify\nfrom json import JSONEncoder\nclass CustomEncoder(JSONEncoder):\n    def default(self, obj):\n        if isinstance(obj, Decimal):\n            return str(obj)\n        return super().default(obj)\napp = Flask(__name__)\napp.json_encoder = CustomEncoder",
      "condition": "",
      "sources": []
    },
    {
      "action": "Convert Decimal to string in the data before jsonify.",
      "success_rate": 0.85,
      "how": "data = {'price': str(decimal_value)}\nreturn jsonify(data)",
      "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-07-22",
  "last_confirmed": "2025-01-01",
  "last_updated": "2025-01-01",
  "evidence_count": 0,
  "tags": [],
  "locale": "en",
  "aliases": []
}