Skip to content
Myr

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

4 files+30 added−17 removed

modified:flask/blueprints.py+2 added-2 removed
88 unmodified lines
89 warn_on_modifications = False90 _got_registered_once = False9192 #: 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 = None9899 def __init__(self, name, import_name, static_folder=None,100 static_url_path=None, template_folder=None,
320 unmodified lines
modified:flask/json.py+14 added-10 removed
90 unmodified lines
9192def _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 )101102 if not current_app.config['JSON_AS_ASCII']:103 kwargs.setdefault('ensure_ascii', False)104105 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 line
110111def _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)122123
158 unmodified lines
modified:tests/test_helpers.py+11 added-5 removed
269 unmodified lines
270 def test_blueprint_json_customization(self):271 class X(object):272 def __init__(self, val):273 self.val = val274275 class MyEncoder(flask.json.JSONEncoder):276 def default(self, o):277 if isinstance(o, X):278 return '<%d>' % o.val279280 return flask.json.JSONEncoder.default(self, o)281282 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)286287 def object_hook(self, obj):288 if len(obj) == 1 and '_foo' in obj:289 return X(obj['_foo'])290291 return obj292288 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 = MyDecoder296297 @bp.route('/bp', methods=['POST'])298 def index():299 return flask.json.dumps(flask.request.get_json()['x'])300301 app = flask.Flask(__name__)302 app.testing = True297 app.register_blueprint(blue)303 app.register_blueprint(bp)304305 c = app.test_client()306 rv = c.post('/bp', data=flask.json.dumps({307 'x': {'_foo': 42}
637 unmodified lines
modified:CHANGES+3 added
25 unmodified lines
26 (`#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`_)3233.. _#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/2254
612 unmodified lines