# TypeError: Object of type 'Decimal' is not JSON serializable

- **ID:** `python/flask-jsonify-error-nonserializable`
- **Domain:** python
- **Category:** type_error
- **Verification:** ai_generated
- **Fix Rate:** 80%

## Root Cause

Flask's jsonify uses default JSON encoder which cannot serialize Decimal.

## Version Compatibility

| Version | Status | Introduced | Deprecated |
|---------|--------|------------|------------|
| 3.x | active | — | — |

## Workarounds

1. **Create a custom JSON encoder and set it on the Flask app.** (90% success)
   ```
   from flask import Flask, jsonify
from json import JSONEncoder
class CustomEncoder(JSONEncoder):
    def default(self, obj):
        if isinstance(obj, Decimal):
            return str(obj)
        return super().default(obj)
app = Flask(__name__)
app.json_encoder = CustomEncoder
   ```
2. **Convert Decimal to string in the data before jsonify.** (85% success)
   ```
   data = {'price': str(decimal_value)}
return jsonify(data)
   ```

## Dead Ends

- **Converting Decimal to float manually, losing precision.** — Float may introduce rounding errors for financial data. (60% fail)
- **Using json.dumps with default=str but not integrating with jsonify.** — jsonify does not use the same encoder as json.dumps by default. (70% fail)
