From 46f83665ef74880bbc4a1c1f17ccededa7aaa939 Mon Sep 17 00:00:00 2001 From: David Lord Date: Mon, 24 Apr 2017 10:09:50 -0700 Subject: [PATCH] clean up blueprint json support add changelog for #1898 --- CHANGES | 3 +++ flask/blueprints.py | 4 ++-- flask/json.py | 24 ++++++++++++++---------- tests/test_helpers.py | 16 +++++++++++----- 4 files changed, 30 insertions(+), 17 deletions(-) diff --git a/CHANGES b/CHANGES index d7f15d66..ae592ba8 100644 --- a/CHANGES +++ b/CHANGES @@ -27,8 +27,11 @@ Major release, unreleased - ``MethodView`` can inherit method handlers from base classes. (`#1936`_) - Errors caused while opening the session at the beginning of the request are handled by the app's error handlers. (`#2254`_) +- Blueprints gained ``json_encoder`` and ``json_decoder`` attributes to + override the app's encoder and decoder. (`#1898`_) .. _#1489: https://github.com/pallets/flask/pull/1489 +.. _#1898: https://github.com/pallets/flask/pull/1898 .. _#1936: https://github.com/pallets/flask/pull/1936 .. _#2017: https://github.com/pallets/flask/pull/2017 .. _#2223: https://github.com/pallets/flask/pull/2223 diff --git a/flask/blueprints.py b/flask/blueprints.py index 62675204..57d77512 100644 --- a/flask/blueprints.py +++ b/flask/blueprints.py @@ -90,10 +90,10 @@ class Blueprint(_PackageBoundObject): _got_registered_once = False #: Blueprint local JSON decoder class to use. - # Set to None to use the :class:`~flask.app.Flask.json_encoder`. + #: Set to ``None`` to use the app's :class:`~flask.app.Flask.json_encoder`. json_encoder = None #: Blueprint local JSON decoder class to use. - # Set to None to use the :class:`~flask.app.Flask.json_decoder`. + #: Set to ``None`` to use the app's :class:`~flask.app.Flask.json_decoder`. json_decoder = None def __init__(self, name, import_name, static_folder=None, diff --git a/flask/json.py b/flask/json.py index 77b5fce1..bf8a8843 100644 --- a/flask/json.py +++ b/flask/json.py @@ -92,13 +92,16 @@ class JSONDecoder(_json.JSONDecoder): def _dump_arg_defaults(kwargs): """Inject default arguments for dump functions.""" if current_app: - bp = current_app.blueprints.get(request.blueprint, - None) if has_request_context() else None - kwargs.setdefault('cls', - bp.json_encoder if bp and bp.json_encoder - else current_app.json_encoder) + bp = current_app.blueprints.get(request.blueprint) if request else None + kwargs.setdefault( + 'cls', + bp.json_encoder if bp and bp.json_encoder + else current_app.json_encoder + ) + if not current_app.config['JSON_AS_ASCII']: kwargs.setdefault('ensure_ascii', False) + kwargs.setdefault('sort_keys', current_app.config['JSON_SORT_KEYS']) else: kwargs.setdefault('sort_keys', True) @@ -108,11 +111,12 @@ def _dump_arg_defaults(kwargs): def _load_arg_defaults(kwargs): """Inject default arguments for load functions.""" if current_app: - bp = current_app.blueprints.get(request.blueprint, - None) if has_request_context() else None - kwargs.setdefault('cls', - bp.json_decoder if bp and bp.json_decoder - else current_app.json_decoder) + bp = current_app.blueprints.get(request.blueprint) if request else None + kwargs.setdefault( + 'cls', + bp.json_decoder if bp and bp.json_decoder + else current_app.json_decoder + ) else: kwargs.setdefault('cls', JSONDecoder) diff --git a/tests/test_helpers.py b/tests/test_helpers.py index c811e1b7..325713c0 100644 --- a/tests/test_helpers.py +++ b/tests/test_helpers.py @@ -271,30 +271,36 @@ class TestJSON(object): class X(object): def __init__(self, val): self.val = val + class MyEncoder(flask.json.JSONEncoder): def default(self, o): if isinstance(o, X): return '<%d>' % o.val + return flask.json.JSONEncoder.default(self, o) + class MyDecoder(flask.json.JSONDecoder): def __init__(self, *args, **kwargs): kwargs.setdefault('object_hook', self.object_hook) flask.json.JSONDecoder.__init__(self, *args, **kwargs) + def object_hook(self, obj): if len(obj) == 1 and '_foo' in obj: return X(obj['_foo']) + return obj - blue = flask.Blueprint('blue', __name__) - blue.json_encoder = MyEncoder - blue.json_decoder = MyDecoder - @blue.route('/bp', methods=['POST']) + bp = flask.Blueprint('bp', __name__) + bp.json_encoder = MyEncoder + bp.json_decoder = MyDecoder + + @bp.route('/bp', methods=['POST']) def index(): return flask.json.dumps(flask.request.get_json()['x']) app = flask.Flask(__name__) app.testing = True - app.register_blueprint(blue) + app.register_blueprint(bp) c = app.test_client() rv = c.post('/bp', data=flask.json.dumps({