Merge pull request #948 from chiuczek/json-dependency-injection
Use dependency injection to allow alternative json parser or encoder
This commit is contained in:
commit
4ee042c330
|
@ -71,12 +71,17 @@ class Request(dict):
|
||||||
@property
|
@property
|
||||||
def json(self):
|
def json(self):
|
||||||
if self.parsed_json is None:
|
if self.parsed_json is None:
|
||||||
try:
|
self.load_json()
|
||||||
self.parsed_json = json_loads(self.body)
|
|
||||||
except Exception:
|
return self.parsed_json
|
||||||
if not self.body:
|
|
||||||
return None
|
def load_json(self, loads=json_loads):
|
||||||
raise InvalidUsage("Failed when parsing body as json")
|
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
|
return self.parsed_json
|
||||||
|
|
||||||
|
|
|
@ -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)
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user