Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions docs/config.rst
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,14 @@ The following configuration values are used internally by Flask:

Default: ``True``

.. py:data:: JSON_STRICT

Raise an error when encountering values that cannot be serialized in a
strictly JSON compliant manner (such as ``NaN`` and ``Infinity``),
rather than encoding them as such.

Default: ``False``

.. py:data:: JSONIFY_PRETTYPRINT_REGULAR

``jsonify`` responses will be output with newlines, spaces, and indentation
Expand Down
1 change: 1 addition & 0 deletions flask/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,7 @@ class Flask(_PackageBoundObject):
'PREFERRED_URL_SCHEME': 'http',
'JSON_AS_ASCII': True,
'JSON_SORT_KEYS': True,
'JSON_STRICT': False,
'JSONIFY_PRETTYPRINT_REGULAR': False,
'JSONIFY_MIMETYPE': 'application/json',
'TEMPLATES_AUTO_RELOAD': None,
Expand Down
7 changes: 7 additions & 0 deletions flask/json/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,9 @@ def _dump_arg_defaults(kwargs):
if not current_app.config['JSON_AS_ASCII']:
kwargs.setdefault('ensure_ascii', False)

if current_app.config['JSON_STRICT']:
kwargs.setdefault('allow_nan', False)

kwargs.setdefault('sort_keys', current_app.config['JSON_SORT_KEYS'])
else:
kwargs.setdefault('sort_keys', True)
Expand Down Expand Up @@ -173,6 +176,10 @@ def dumps(obj, **kwargs):
default which coerce into unicode strings automatically. That behavior by
default is controlled by the ``JSON_AS_ASCII`` configuration variable
and can be overridden by the simplejson ``ensure_ascii`` parameter.

If the ``JSON_STRICT`` config parameter is set to True, this function will
raise a :exc:`ValueError` if it encounters values that cannot be represented in
a JSON specification compliant manner.
"""
_dump_arg_defaults(kwargs)
encoding = kwargs.pop('encoding', None)
Expand Down
8 changes: 8 additions & 0 deletions tests/test_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -1298,6 +1298,14 @@ def test_jsonify_prettyprint(app, req_ctx):
assert rv.data == pretty_response


@pytest.mark.parametrize('test_value', [float('nan'), float('Infinity')])
def test_json_strict(app, req_ctx, test_value):
app.config.update({'JSON_STRICT': True})

with pytest.raises(ValueError) as e:
flask.jsonify(test_value)
assert 'not JSON compliant' in str(e.value)

def test_jsonify_mimetype(app, req_ctx):
app.config.update({"JSONIFY_MIMETYPE": 'application/vnd.api+json'})
msg = {
Expand Down
2 changes: 1 addition & 1 deletion tests/test_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ def test_json_dump_to_file(self, app, app_ctx):
rv = flask.json.load(out)
assert rv == test_data

@pytest.mark.parametrize('test_value', [0, -1, 1, 23, 3.14, 's', "longer string", True, False, None])
@pytest.mark.parametrize('test_value', [0, -1, 1, 23, 3.14, float('Infinity'), 's', "longer string", True, False, None])
def test_jsonify_basic_types(self, test_value, app, client):
"""Test jsonify with basic types."""

Expand Down