MCPcopy Index your code
hub / github.com/fluentpython/example-code-2e / JSONPlugin

Class JSONPlugin

21-async/mojifinder/bottle.py:1736–1763  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1734
1735
1736class JSONPlugin(object):
1737 name = 'json'
1738 api = 2
1739
1740 def __init__(self, json_dumps=json_dumps):
1741 self.json_dumps = json_dumps
1742
1743 def apply(self, callback, route):
1744 dumps = self.json_dumps
1745 if not dumps: return callback
1746 def wrapper(*a, **ka):
1747 try:
1748 rv = callback(*a, **ka)
1749 except HTTPError:
1750 rv = _e()
1751
1752 if isinstance(rv, dict):
1753 #Attempt to serialize, raises exception on failure
1754 json_response = dumps(rv)
1755 #Set content type only if serialization succesful
1756 response.content_type = 'application/json'
1757 return json_response
1758 elif isinstance(rv, HTTPResponse) and isinstance(rv.body, dict):
1759 rv.body = dumps(rv.body)
1760 rv.content_type = 'application/json'
1761 return rv
1762
1763 return wrapper
1764
1765
1766class TemplatePlugin(object):

Callers 1

__init__Method · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected