clean up blueprint json support
Before adoption: this commit came in with the history the project adopted, so no landing records it.
add changelog for #1898
authored by David Lord · committed by David Lord · parent 74b3f7e · Browse files · 4 files·+30 added−17 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
4 files+30 added−17 removed
- tests:+11 added−5 removed
- other:+19 added−12 removed
Provenance
- Commit
46f8366- Tree
6094e39- Author
David Lord <davidism@gmail.com>- Committer
David Lord <davidism@gmail.com>- Parent
74b3f7e
4 files changed+30 added−17 removed
modified:flask/blueprints.py+2 added-2 removed
88 unmodified lines89 warn_on_modifications = False90 _got_registered_once = False91 92 #: Blueprint local JSON decoder class to use.93- # Set to None to use the :class:`~flask.app.Flask.json_encoder`.93+ #: Set to ``None`` to use the app's :class:`~flask.app.Flask.json_encoder`.94 json_encoder = None95 #: Blueprint local JSON decoder class to use.96- # Set to None to use the :class:`~flask.app.Flask.json_decoder`.96+ #: Set to ``None`` to use the app's :class:`~flask.app.Flask.json_decoder`.97 json_decoder = None98 99 def __init__(self, name, import_name, static_folder=None,100 static_url_path=None, template_folder=None,320 unmodified linesmodified:flask/json.py+14 added-10 removed
90 unmodified lines91 92 def _dump_arg_defaults(kwargs):93 """Inject default arguments for dump functions."""94 if current_app:95- bp = current_app.blueprints.get(request.blueprint,96- None) if has_request_context() else None97- kwargs.setdefault('cls',98- bp.json_encoder if bp and bp.json_encoder99- else current_app.json_encoder)95+ bp = current_app.blueprints.get(request.blueprint) if request else None96+ kwargs.setdefault(97+ 'cls',98+ bp.json_encoder if bp and bp.json_encoder99+ else current_app.json_encoder100+ )101+102 if not current_app.config['JSON_AS_ASCII']:103 kwargs.setdefault('ensure_ascii', False)104+105 kwargs.setdefault('sort_keys', current_app.config['JSON_SORT_KEYS'])106 else:107 kwargs.setdefault('sort_keys', True)108 kwargs.setdefault('cls', JSONEncoder)1 unmodified line110 111 def _load_arg_defaults(kwargs):112 """Inject default arguments for load functions."""113 if current_app:111- bp = current_app.blueprints.get(request.blueprint,112- None) if has_request_context() else None113- kwargs.setdefault('cls',114- bp.json_decoder if bp and bp.json_decoder115- else current_app.json_decoder)114+ bp = current_app.blueprints.get(request.blueprint) if request else None115+ kwargs.setdefault(116+ 'cls',117+ bp.json_decoder if bp and bp.json_decoder118+ else current_app.json_decoder119+ )120 else:121 kwargs.setdefault('cls', JSONDecoder)122 123 158 unmodified linesmodified:tests/test_helpers.py+11 added-5 removed
269 unmodified lines270 def test_blueprint_json_customization(self):271 class X(object):272 def __init__(self, val):273 self.val = val274+275 class MyEncoder(flask.json.JSONEncoder):276 def default(self, o):277 if isinstance(o, X):278 return '<%d>' % o.val279+280 return flask.json.JSONEncoder.default(self, o)281+282 class MyDecoder(flask.json.JSONDecoder):283 def __init__(self, *args, **kwargs):284 kwargs.setdefault('object_hook', self.object_hook)285 flask.json.JSONDecoder.__init__(self, *args, **kwargs)286+287 def object_hook(self, obj):288 if len(obj) == 1 and '_foo' in obj:289 return X(obj['_foo'])290+291 return obj292 288- blue = flask.Blueprint('blue', __name__)289- blue.json_encoder = MyEncoder290- blue.json_decoder = MyDecoder291- @blue.route('/bp', methods=['POST'])293+ bp = flask.Blueprint('bp', __name__)294+ bp.json_encoder = MyEncoder295+ bp.json_decoder = MyDecoder296+297+ @bp.route('/bp', methods=['POST'])298 def index():299 return flask.json.dumps(flask.request.get_json()['x'])300 301 app = flask.Flask(__name__)302 app.testing = True297- app.register_blueprint(blue)303+ app.register_blueprint(bp)304 305 c = app.test_client()306 rv = c.post('/bp', data=flask.json.dumps({307 'x': {'_foo': 42}637 unmodified linesmodified:CHANGES+3 added
25 unmodified lines26 (`#1489`_).27 - ``MethodView`` can inherit method handlers from base classes. (`#1936`_)28 - Errors caused while opening the session at the beginning of the request are29 handled by the app's error handlers. (`#2254`_)30+- Blueprints gained ``json_encoder`` and ``json_decoder`` attributes to31+ override the app's encoder and decoder. (`#1898`_)32 33 .. _#1489: https://github.com/pallets/flask/pull/148934+.. _#1898: https://github.com/pallets/flask/pull/189835 .. _#1936: https://github.com/pallets/flask/pull/193636 .. _#2017: https://github.com/pallets/flask/pull/201737 .. _#2223: https://github.com/pallets/flask/pull/222338 .. _#2254: https://github.com/pallets/flask/pull/2254612 unmodified lines