diff --git a/flask/json.py b/flask/json.py index 149cfbab..1593a326 100644 --- a/flask/json.py +++ b/flask/json.py @@ -70,30 +70,36 @@ class JSONDecoder(_json.JSONDecoder): def dumps(obj, **kwargs): """Serialize ``obj`` to a JSON formatted ``str`` by using the application's - configured encoder (:attr:`~flask.Flask.json_encoder`). + configured encoder (:attr:`~flask.Flask.json_encoder`) if there is an + application on the stack. """ - kwargs.setdefault('cls', current_app.json_encoder) + if current_app: + kwargs.setdefault('cls', current_app.json_encoder) return _json.dumps(obj, **kwargs) def dump(obj, fp, **kwargs): """Like :func:`dumps` but writes into a file object.""" - kwargs.setdefault('cls', current_app.json_encoder) + if current_app: + kwargs.setdefault('cls', current_app.json_encoder) return _json.dump(obj, fp, **kwargs) def loads(s, **kwargs): """Unserialize a JSON object from a string ``s`` by using the application's - configured decoder (:attr:`~flask.Flask.json_decoder`). + configured decoder (:attr:`~flask.Flask.json_decoder`) if there is an + application on the stack. """ - kwargs.setdefault('cls', current_app.json_decoder) + if current_app: + kwargs.setdefault('cls', current_app.json_decoder) return _json.loads(s, **kwargs) def load(fp, **kwargs): """Like :func:`loads` but reads from a file object. """ - kwargs.setdefault('cls', current_app.json_decoder) + if current_app: + kwargs.setdefault('cls', current_app.json_decoder) return _json.load(fp, **kwargs) diff --git a/flask/testsuite/helpers.py b/flask/testsuite/helpers.py index 58592668..ee5312e0 100644 --- a/flask/testsuite/helpers.py +++ b/flask/testsuite/helpers.py @@ -100,6 +100,36 @@ class JSONTestCase(FlaskTestCase): rv = render('{{ "