use dependency injection to allow alternative json parser or encoder

This commit is contained in:
Hugh McNamara 2017-09-19 14:58:49 +01:00
parent 329ebf6a5d
commit 1d719252cb
2 changed files with 5 additions and 4 deletions

View File

@ -69,10 +69,10 @@ class Request(dict):
self.stream = None self.stream = None
@property @property
def json(self): def json(self, loads=json_loads):
if self.parsed_json is None: if self.parsed_json is None:
try: try:
self.parsed_json = json_loads(self.body) self.parsed_json = loads(self.body)
except Exception: except Exception:
if not self.body: if not self.body:
return None return None

View File

@ -237,7 +237,8 @@ class HTTPResponse(BaseHTTPResponse):
def json(body, status=200, headers=None, 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. Returns response object with body in json format.
@ -246,7 +247,7 @@ def json(body, status=200, headers=None,
:param headers: Custom Headers. :param headers: Custom Headers.
:param kwargs: Remaining arguments that are passed to the json encoder. :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) status=status, content_type=content_type)