Skip to content
Myr

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

2 files+42 added−6 removed

modified:flask/testsuite/helpers.py+30 added
98 unmodified lines
99 self.assert_equal(rv, '"<\\u0000\\/script>"')100 rv = render('{{ "<!--<script>"|tojson|safe }}')101 self.assert_equal(rv, '"<\\u0021--<script>"')102103 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>"')132133 def test_modified_url_encoding(self):134 class ModifiedRequest(flask.Request):135 url_charset = 'euc-kr'136 app = flask.Flask(__name__)
362 unmodified lines
modified:flask/json.py+12 added-6 removed
68 unmodified lines
697071def 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)798081def 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)868788def 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)969798def 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)104105106def htmlsafe_dumps(obj, **kwargs):
46 unmodified lines