| 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 | |