Let json.* work even without app on the stack and added tests
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 b146d82 · Browse files · 2 files·+42 added−6 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+42 added−6 removed
- other:+42 added−6 removed
Provenance
- Commit
05c6502- Tree
bd9507e- Author
Armin Ronacher <armin.ronacher@active-4.com>- Committer
Armin Ronacher <armin.ronacher@active-4.com>- Parent
b146d82
2 files changed+42 added−6 removed
modified:flask/testsuite/helpers.py+30 added
98 unmodified lines99 self.assert_equal(rv, '"<\\u0000\\/script>"')100 rv = render('{{ "<!--<script>"|tojson|safe }}')101 self.assert_equal(rv, '"<\\u0021--<script>"')102 103+ def test_json_customization(self):104+ class X(object):105+ def __init__(self, val):106+ self.val = val107+ class MyEncoder(flask.json.JSONEncoder):108+ def default(self, o):109+ if isinstance(o, X):110+ return '<%d>' % o.val111+ return flask.json.JSONEncoder.default(self, o)112+ class MyDecoder(flask.json.JSONDecoder):113+ def __init__(self, *args, **kwargs):114+ kwargs.setdefault('object_hook', self.object_hook)115+ flask.json.JSONDecoder.__init__(self, *args, **kwargs)116+ def object_hook(self, obj):117+ if len(obj) == 1 and '_foo' in obj:118+ return X(obj['_foo'])119+ return obj120+ app = flask.Flask(__name__)121+ app.testing = True122+ app.json_encoder = MyEncoder123+ app.json_decoder = MyDecoder124+ @app.route('/', methods=['POST'])125+ def index():126+ return flask.json.dumps(flask.request.json['x'])127+ c = app.test_client()128+ rv = c.post('/', data=flask.json.dumps({129+ 'x': {'_foo': 42}130+ }), content_type='application/json')131+ self.assertEqual(rv.data, '"<42>"')132+133 def test_modified_url_encoding(self):134 class ModifiedRequest(flask.Request):135 url_charset = 'euc-kr'136 app = flask.Flask(__name__)362 unmodified linesmodified:flask/json.py+12 added-6 removed
68 unmodified lines69 70 71 def dumps(obj, **kwargs):72 """Serialize ``obj`` to a JSON formatted ``str`` by using the application's73- configured encoder (:attr:`~flask.Flask.json_encoder`).73+ configured encoder (:attr:`~flask.Flask.json_encoder`) if there is an74+ application on the stack.75 """76+ if current_app:75- kwargs.setdefault('cls', current_app.json_encoder)77+ kwargs.setdefault('cls', current_app.json_encoder)78 return _json.dumps(obj, **kwargs)79 80 81 def dump(obj, fp, **kwargs):82 """Like :func:`dumps` but writes into a file object."""83+ if current_app:81- kwargs.setdefault('cls', current_app.json_encoder)84+ kwargs.setdefault('cls', current_app.json_encoder)85 return _json.dump(obj, fp, **kwargs)86 87 88 def loads(s, **kwargs):89 """Unserialize a JSON object from a string ``s`` by using the application's87- configured decoder (:attr:`~flask.Flask.json_decoder`).90+ configured decoder (:attr:`~flask.Flask.json_decoder`) if there is an91+ application on the stack.92 """93+ if current_app:89- kwargs.setdefault('cls', current_app.json_decoder)94+ kwargs.setdefault('cls', current_app.json_decoder)95 return _json.loads(s, **kwargs)96 97 98 def load(fp, **kwargs):99 """Like :func:`loads` but reads from a file object.100 """101+ if current_app:96- kwargs.setdefault('cls', current_app.json_decoder)102+ kwargs.setdefault('cls', current_app.json_decoder)103 return _json.load(fp, **kwargs)104 105 106 def htmlsafe_dumps(obj, **kwargs):46 unmodified lines