Flask Jsonify Custom JsonEncoder

Oct 23, 2019
import decimalfrom flask import current_app as appfrom flask import Jsonifyfrom flask.json import JSONEncoderclass JsonEncoder(JSONEncoder):    def default(self, obj):        if isinstance(obj, decimal.Decimal):            return float(obj)        return JSONEncoder.default(self, obj)@app.route('/test_jsonify')def test_jsonify():    data = {        'float': 7.5,        'decimal': decimal.Decimal(7.5)    }    app.json_encoder = JsonEncoder    return jsonify(data)

❤️ Is this article helpful?

Buy me a coffee ☕ or support my work via PayPal to keep this space 🖖 and ad-free.

Do send some 💖 to @d_luaz or share this article.

✨ By Desmond Lua

A dream boy who enjoys making apps, travelling and making youtube videos. Follow me on @d_luaz

👶 Apps I built

Travelopy - discover travel places in Malaysia, Singapore, Taiwan, Japan.