Skip to content
Myr

Merge pull request #4160 from default-303/decimal_fix

Before adoption: this commit came in with the history the project adopted, so no landing records it.

json support for Decimal objects

authored by David Lord · committed by GitHub · parents 06cf349, 892ae95 · Browse files · 3 files+16 added−1 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

3 files+16 added−1 removed

  • tests:+6 added−0 removed
  • other:+10 added−1 removed

3 files+16 added−1 removed

modified:src/flask/json/__init__.py+8 added-1 removed
1import decimal2import io3import json as _json4import typing as t5import uuid
41 unmodified lines
47 serialized, they are handled before this method.48 """49 if isinstance(o, date):50 return http_date(o)50 if isinstance(o, uuid.UUID):51 if isinstance(o, (decimal.Decimal, uuid.UUID)):52 return str(o)53 if dataclasses and dataclasses.is_dataclass(o):54 return dataclasses.asdict(o)55 if hasattr(o, "__html__"):
61 unmodified lines
117 :param app: Use this app's config instead of the active app context118 or defaults.119 :param kwargs: Extra arguments passed to :func:`json.dumps`.120121 .. versionchanged:: 2.0.2122 :class:`decimal.Decimal` is supported by converting to a string.123124 .. versionchanged:: 2.0125 ``encoding`` is deprecated and will be removed in Flask 2.1.126127 .. versionchanged:: 1.0.3
199 unmodified lines
327 The default output omits indents and spaces after separators. In328 debug mode or if :data:`JSONIFY_PRETTYPRINT_REGULAR` is ``True``,329 the output will be formatted to be easier to read.330331 .. versionchanged:: 2.0.2332 :class:`decimal.Decimal` is supported by converting to a string.333334 .. versionchanged:: 0.11335 Added support for serializing top-level arrays. This introduces336 a security risk in ancient browsers. See :ref:`security-json`.337
20 unmodified lines
modified:tests/test_json.py+6 added
1import datetime2import decimal3import io4import uuid56import pytest
180 unmodified lines
187 rv_uuid = uuid.UUID(rv_x)188 assert rv_uuid == test_uuid189190191def test_json_decimal():192 rv = flask.json.dumps(decimal.Decimal("0.003"))193 assert rv == '"0.003"'194195196def test_json_attr(app, client):197 @app.route("/add", methods=["POST"])198 def add():199 json = flask.request.get_json()
209 unmodified lines
modified:CHANGES.rst+2 added
13 unmodified lines
14- Enhance typing of ``app.errorhandler`` decorator. :issue:`4095`15- Fix registering a blueprint twice with differing names. :issue:`4124`16- Fix the type of ``static_folder`` to accept ``pathlib.Path``.17 :issue:`4150`18- ``jsonify`` handles ``decimal.Decimal`` by encoding to ``str``.19 :issue:`4157`202122Version 2.0.123-------------
1154 unmodified lines