Skip to content
Myr

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

5 files+57 added−1 removed

modified:docs/config.rst+12 added-1 removed
147 unmodified lines
148 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 by
40 unmodified lines
207.. versionadded:: 0.9208 ``PREFERRED_URL_SCHEME``209210.. versionadded:: 0.10200 ``JSON_AS_ASCII``, ``JSONIFY_PRETTYPRINT_REGULAR``211 ``JSON_AS_ASCII``, ``JSON_SORT_KEYS``, ``JSONIFY_PRETTYPRINT_REGULAR``212213Configuring from Files214----------------------215
214 unmodified lines
modified:flask/testsuite/helpers.py+40 added
146 unmodified lines
147148 if not has_encoding('euc-kr'):149 test_modified_url_encoding = None150151 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')156157 @app.route('/')158 def index():159 return flask.jsonify(values=d)160161 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 ])190191192class SendfileTestCase(FlaskTestCase):193194 def test_send_file_regular(self):195 app = flask.Flask(__name__)
386 unmodified lines
modified:flask/app.py+1 added
289 unmodified lines
290 '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 })297298 #: The rule object to use for URL rules created. This is used by
1537 unmodified lines
modified:flask/json.py+2 added
90 unmodified lines
919293def _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'])101102103def _load_arg_defaults(kwargs):104 """Inject default arguments for load functions."""
107 unmodified lines
modified:CHANGES+2 added
61 unmodified lines
62- 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.6869Version 0.970-----------71
365 unmodified lines