From 05c6502cbde016f0475233faa0c6364634cccac9 Mon Sep 17 00:00:00 2001 From: Armin Ronacher Date: Sun, 7 Oct 2012 23:41:41 +0200 Subject: [PATCH] Let json.* work even without app on the stack and added tests --- flask/json.py | 18 ++++++++++++------ flask/testsuite/helpers.py | 30 ++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+), 6 deletions(-) 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('{{ "