Order JSON keys by default to avoid trashing HTTP caches
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Armin Ronacher · committed by Armin Ronacher · parent 3d9055b · Browse files · 5 files·+57 added−1 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
5 files+57 added−1 removed
- other:+57 added−1 removed
Provenance
- Commit
77d293c- Tree
d1a9561- Author
Armin Ronacher <armin.ronacher@active-4.com>- Committer
Armin Ronacher <armin.ronacher@active-4.com>- Parent
3d9055b
5 files changed+57 added−1 removed
modified:docs/config.rst+12 added-1 removed
147 unmodified lines148 and output strings as-is and return149 unicode strings. ``jsonfiy`` will150 automatically encode it in ``utf-8``151 then for transport for instance.152+``JSON_SORT_KEYS`` By default Flask will serialize JSON153+ objects in a way that the keys are154+ ordered. This is done in order to155+ ensure that independent of the hash seed156+ of the dictionary the return value will157+ be consistent to not trash external HTTP158+ caches. You can override the default159+ behavior by changing this variable.160+ This is not recommended but might give161+ you a performance improvement on the162+ cost of cachability.163 ``JSONIFY_PRETTYPRINT_REGULAR`` If this is set to ``True`` (the default)164 jsonify responses will be pretty printed165 if they are not requested by an166 XMLHttpRequest object (controlled by40 unmodified lines207 .. versionadded:: 0.9208 ``PREFERRED_URL_SCHEME``209 210 .. versionadded:: 0.10200- ``JSON_AS_ASCII``, ``JSONIFY_PRETTYPRINT_REGULAR``211+ ``JSON_AS_ASCII``, ``JSON_SORT_KEYS``, ``JSONIFY_PRETTYPRINT_REGULAR``212 213 Configuring from Files214 ----------------------215 214 unmodified linesmodified:flask/testsuite/helpers.py+40 added
146 unmodified lines147 148 if not has_encoding('euc-kr'):149 test_modified_url_encoding = None150 151+ def test_json_key_sorting(self):152+ app = flask.Flask(__name__)153+ app.testing = True154+ self.assert_equal(app.config['JSON_SORT_KEYS'], True)155+ d = dict.fromkeys(range(20), 'foo')156 157+ @app.route('/')158+ def index():159+ return flask.jsonify(values=d)160+161+ c = app.test_client()162+ rv = c.get('/')163+ lines = [x.strip() for x in rv.data.strip().decode('utf-8').splitlines()]164+ self.assert_equal(lines, [165+ '{',166+ '"values": {',167+ '"0": "foo",',168+ '"1": "foo",',169+ '"2": "foo",',170+ '"3": "foo",',171+ '"4": "foo",',172+ '"5": "foo",',173+ '"6": "foo",',174+ '"7": "foo",',175+ '"8": "foo",',176+ '"9": "foo",',177+ '"10": "foo",',178+ '"11": "foo",',179+ '"12": "foo",',180+ '"13": "foo",',181+ '"14": "foo",',182+ '"15": "foo",',183+ '"16": "foo",',184+ '"17": "foo",',185+ '"18": "foo",',186+ '"19": "foo"',187+ '}',188+ '}'189+ ])190+191+192 class SendfileTestCase(FlaskTestCase):193 194 def test_send_file_regular(self):195 app = flask.Flask(__name__)386 unmodified linesmodified:flask/app.py+1 added
289 unmodified lines290 'TRAP_BAD_REQUEST_ERRORS': False,291 'TRAP_HTTP_EXCEPTIONS': False,292 'PREFERRED_URL_SCHEME': 'http',293 'JSON_AS_ASCII': True,294+ 'JSON_SORT_KEYS': True,295 'JSONIFY_PRETTYPRINT_REGULAR': True,296 })297 298 #: The rule object to use for URL rules created. This is used by1537 unmodified linesmodified:flask/json.py+2 added
90 unmodified lines91 92 93 def _dump_arg_defaults(kwargs):94 """Inject default arguments for dump functions."""95+ kwargs.setdefault('sort_keys', True)96 if current_app:97 kwargs.setdefault('cls', current_app.json_encoder)98 if not current_app.config['JSON_AS_ASCII']:99 kwargs.setdefault('ensure_ascii', False)100+ kwargs.setdefault('sort_keys', current_app.config['JSON_SORT_KEYS'])101 102 103 def _load_arg_defaults(kwargs):104 """Inject default arguments for load functions."""107 unmodified linesmodified:CHANGES+2 added
61 unmodified lines62 - Changed how the teardown system is informed about exceptions. This is now63 more reliable in case something handles an exception halfway through64 the error handling process.65 - Added the ``JSONIFY_PRETTYPRINT_REGULAR`` configuration variable.66+- Flask now orders JSON keys by default to not trash HTTP caches due to67+ different hash seeds between different workers.68 69 Version 0.970 -----------71 365 unmodified lines