Skip to content
Myr

Merge pull request #1898 from RaHus/per_blueprint_jsoncoding_#1710

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

Per blueprint jsoncoding #1710

authored by David Lord · committed by GitHub · parents 8d515a6, 4305ebd · Browse files · 3 files+53 added−2 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+53 added−2 removed

  • tests:+35 added−0 removed
  • other:+18 added−2 removed

3 files+53 added−2 removed

modified:flask/blueprints.py+7 added
87 unmodified lines
8889 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`.94 json_encoder = None95 #: Blueprint local JSON decoder class to use.96 # Set to None to use the :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,101 url_prefix=None, subdomain=None, url_defaults=None,102 root_path=None):
318 unmodified lines
modified:flask/json.py+11 added-2 removed
11 unmodified lines
12import uuid13from datetime import date14from .globals import current_app, request15from ._compat import text_type, PY216from .ctx import has_request_context1718from werkzeug.http import http_date19from jinja2 import Markup20
70 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_encoder94 kwargs.setdefault('cls', current_app.json_encoder)99 else current_app.json_encoder)100 if not current_app.config['JSON_AS_ASCII']:101 kwargs.setdefault('ensure_ascii', False)102 kwargs.setdefault('sort_keys', current_app.config['JSON_SORT_KEYS'])103 else:
3 unmodified lines
107108def _load_arg_defaults(kwargs):109 """Inject default arguments for load functions."""110 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_decoder106 kwargs.setdefault('cls', current_app.json_decoder)115 else current_app.json_decoder)116 else:117 kwargs.setdefault('cls', JSONDecoder)118119
158 unmodified lines
modified:tests/test_helpers.py+35 added
265 unmodified lines
266 'x': {'_foo': 42}267 }), content_type='application/json')268 assert rv.data == b'"<42>"'269270 def test_blueprint_json_customization(self):271 class X(object):272 def __init__(self, val):273 self.val = val274 class MyEncoder(flask.json.JSONEncoder):275 def default(self, o):276 if isinstance(o, X):277 return '<%d>' % o.val278 return flask.json.JSONEncoder.default(self, o)279 class MyDecoder(flask.json.JSONDecoder):280 def __init__(self, *args, **kwargs):281 kwargs.setdefault('object_hook', self.object_hook)282 flask.json.JSONDecoder.__init__(self, *args, **kwargs)283 def object_hook(self, obj):284 if len(obj) == 1 and '_foo' in obj:285 return X(obj['_foo'])286 return obj287288 blue = flask.Blueprint('blue', __name__)289 blue.json_encoder = MyEncoder290 blue.json_decoder = MyDecoder291 @blue.route('/bp', methods=['POST'])292 def index():293 return flask.json.dumps(flask.request.get_json()['x'])294295 app = flask.Flask(__name__)296 app.testing = True297 app.register_blueprint(blue)298299 c = app.test_client()300 rv = c.post('/bp', data=flask.json.dumps({301 'x': {'_foo': 42}302 }), content_type='application/json')303 assert rv.data == b'"<42>"'304305 def test_modified_url_encoding(self):306 class ModifiedRequest(flask.Request):307 url_charset = 'euc-kr'308 app = flask.Flask(__name__)
630 unmodified lines