From 1d719252cbc6ce79943e76d317cecd57b1872fe4 Mon Sep 17 00:00:00 2001 From: Hugh McNamara Date: Tue, 19 Sep 2017 14:58:49 +0100 Subject: [PATCH 1/3] use dependency injection to allow alternative json parser or encoder --- sanic/request.py | 4 ++-- sanic/response.py | 5 +++-- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/sanic/request.py b/sanic/request.py index 27ff011e..aa778a8d 100644 --- a/sanic/request.py +++ b/sanic/request.py @@ -69,10 +69,10 @@ class Request(dict): self.stream = None @property - def json(self): + def json(self, loads=json_loads): if self.parsed_json is None: try: - self.parsed_json = json_loads(self.body) + self.parsed_json = loads(self.body) except Exception: if not self.body: return None diff --git a/sanic/response.py b/sanic/response.py index 3b0ef449..f661758b 100644 --- a/sanic/response.py +++ b/sanic/response.py @@ -237,7 +237,8 @@ class HTTPResponse(BaseHTTPResponse): def json(body, status=200, headers=None, - content_type="application/json", **kwargs): + content_type="application/json", dumps=json_dumps, + **kwargs): """ Returns response object with body in json format. @@ -246,7 +247,7 @@ def json(body, status=200, headers=None, :param headers: Custom Headers. :param kwargs: Remaining arguments that are passed to the json encoder. """ - return HTTPResponse(json_dumps(body, **kwargs), headers=headers, + return HTTPResponse(dumps(body, **kwargs), headers=headers, status=status, content_type=content_type) From a8f764c1612ab17eb99924ac2363c05110a1911d Mon Sep 17 00:00:00 2001 From: Hugh McNamara Date: Tue, 19 Sep 2017 18:12:53 +0100 Subject: [PATCH 2/3] make method instead of property for alternative json decoding of request --- sanic/request.py | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/sanic/request.py b/sanic/request.py index aa778a8d..74200fe0 100644 --- a/sanic/request.py +++ b/sanic/request.py @@ -69,10 +69,10 @@ class Request(dict): self.stream = None @property - def json(self, loads=json_loads): + def json(self): if self.parsed_json is None: try: - self.parsed_json = loads(self.body) + self.parsed_json = json_loads(self.body) except Exception: if not self.body: return None @@ -80,6 +80,16 @@ class Request(dict): return self.parsed_json + def load_json(self, loads=json_loads): + try: + self.parsed_json = loads(self.body) + except Exception: + if not self.body: + return None + raise InvalidUsage("Failed when parsing body as json") + + return self.parsed_json + @property def token(self): """Attempt to return the auth header token. From 5cef1634edd029e914d0184627e98bcb63198173 Mon Sep 17 00:00:00 2001 From: Hugh McNamara Date: Fri, 22 Sep 2017 10:19:15 +0100 Subject: [PATCH 3/3] use json_loads function in json property of request --- sanic/request.py | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/sanic/request.py b/sanic/request.py index 74200fe0..43ecc511 100644 --- a/sanic/request.py +++ b/sanic/request.py @@ -71,12 +71,7 @@ class Request(dict): @property def json(self): if self.parsed_json is None: - try: - self.parsed_json = json_loads(self.body) - except Exception: - if not self.body: - return None - raise InvalidUsage("Failed when parsing body as json") + self.load_json() return self.parsed_json