Skip to content
Myr

Added support for unicode json dumping. This fixes #535

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 5583fe8 · Browse files · 5 files+52 added−9 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+52 added−9 removed

  • other:+52 added−9 removed

5 files+52 added−9 removed

modified:docs/config.rst+10 added
140 unmodified lines
141 a regular traceback instead.142``PREFERRED_URL_SCHEME`` The URL scheme that should be used for143 URL generation if no URL scheme is144 available. This defaults to ``http``.145``JSON_AS_ASCII`` By default Flask serialize object to146 ascii-encoded JSON. If this is set to147 ``False`` Flask will not encode to ASCII148 and output strings as-is and return149 unicode strings. ``jsonfiy`` will150 automatically encode it in ``utf-8``151 then for transport for instance.152================================= =========================================153154.. admonition:: More on ``SERVER_NAME``155
34 unmodified lines
190191.. versionadded:: 0.9192 ``PREFERRED_URL_SCHEME``193194.. versionadded:: 0.10195 ``JSON_AS_ASCII``196197Configuring from Files198----------------------199200Configuration becomes more useful if you can store it in a separate file,
213 unmodified lines
modified:flask/testsuite/helpers.py+13 added
77 unmodified lines
78 rv = c.get(url)79 self.assert_equal(rv.mimetype, 'application/json')80 self.assert_equal(flask.json.loads(rv.data), d)8182 def test_json_as_unicode(self):83 app = flask.Flask(__name__)8485 app.config['JSON_AS_ASCII'] = True86 with app.app_context():87 rv = flask.json.dumps(u'\N{SNOWMAN}')88 self.assert_equal(rv, '"\\u2603"')8990 app.config['JSON_AS_ASCII'] = False91 with app.app_context():92 rv = flask.json.dumps(u'\N{SNOWMAN}')93 self.assert_equal(rv, u'"\u2603"')9495 def test_json_attr(self):96 app = flask.Flask(__name__)97 @app.route('/add', methods=['POST'])98 def add():
413 unmodified lines
modified:flask/app.py+2 added-1 removed
272 unmodified lines
273 'MAX_CONTENT_LENGTH': None,274 'SEND_FILE_MAX_AGE_DEFAULT': 12 * 60 * 60, # 12 hours275 'TRAP_BAD_REQUEST_ERRORS': False,276 'TRAP_HTTP_EXCEPTIONS': False,277 'PREFERRED_URL_SCHEME': 'http'277 'PREFERRED_URL_SCHEME': 'http',278 'JSON_AS_ASCII': True279 })280281 #: The rule object to use for URL rules created. This is used by282 #: :meth:`add_url_rule`. Defaults to :class:`werkzeug.routing.Rule`.
1476 unmodified lines
modified:flask/json.py+23 added-8 removed
66 unmodified lines
67 functions of this module but also :attr:`~flask.Request`.68 """697071def _dump_arg_defaults(kwargs):72 """Inject default arguments for dump functions."""73 if current_app:74 kwargs.setdefault('cls', current_app.json_encoder)75 if not current_app.config['JSON_AS_ASCII']:76 kwargs.setdefault('ensure_ascii', False)777879def _load_arg_defaults(kwargs):80 """Inject default arguments for load functions."""81 if current_app:82 kwargs.setdefault('cls', current_app.json_decoder)838485def dumps(obj, **kwargs):86 """Serialize ``obj`` to a JSON formatted ``str`` by using the application's87 configured encoder (:attr:`~flask.Flask.json_encoder`) if there is an88 application on the stack.8990 This function can return ``unicode`` strings or ascii-only bytestrings by91 default which coerce into unicode strings automatically. That behavior by92 default is controlled by the ``JSON_AS_ASCII`` configuration variable93 and can be overriden by the simplejson ``ensure_ascii`` parameter.94 """76 if current_app:77 kwargs.setdefault('cls', current_app.json_encoder)95 _dump_arg_defaults(kwargs)96 return _json.dumps(obj, **kwargs)979899def dump(obj, fp, **kwargs):100 """Like :func:`dumps` but writes into a file object."""83 if current_app:84 kwargs.setdefault('cls', current_app.json_encoder)101 _dump_arg_defaults(kwargs)102 return _json.dump(obj, fp, **kwargs)103104105def loads(s, **kwargs):106 """Unserialize a JSON object from a string ``s`` by using the application's107 configured decoder (:attr:`~flask.Flask.json_decoder`) if there is an108 application on the stack.109 """93 if current_app:94 kwargs.setdefault('cls', current_app.json_decoder)110 _load_arg_defaults(kwargs)111 return _json.loads(s, **kwargs)112113114def load(fp, **kwargs):115 """Like :func:`loads` but reads from a file object.116 """101 if current_app:102 kwargs.setdefault('cls', current_app.json_decoder)117 _load_arg_defaults(kwargs)118 return _json.load(fp, **kwargs)119120121def htmlsafe_dumps(obj, **kwargs):
46 unmodified lines
modified:CHANGES+4 added
28 unmodified lines
29 called.30- The config object is now available to the template as a real global and31 not through a context processor which makes it available even in imported32 templates by default.33- Added an option to generate non-ascii encoded JSON which should result34 in less bytes being transmitted over the network. It's disabled by35 default to not cause confusion with existing libraries that might expect36 ``flask.json.dumps`` to return bytestrings by default.3738Version 0.939-----------40
365 unmodified lines