Custom Response implementation which uses our custom and faster json serializer and deserializer.
| 168 | |
| 169 | |
| 170 | class Response(webob.Response): |
| 171 | """ |
| 172 | Custom Response implementation which uses our custom and faster json serializer and |
| 173 | deserializer. |
| 174 | """ |
| 175 | |
| 176 | def __init__( |
| 177 | self, |
| 178 | body=None, |
| 179 | status=None, |
| 180 | headerlist=None, |
| 181 | app_iter=None, |
| 182 | content_type=None, |
| 183 | *args, |
| 184 | **kwargs, |
| 185 | ): |
| 186 | # Do some sanity checking, and turn json_body into an actual body |
| 187 | if ( |
| 188 | app_iter is None |
| 189 | and body is None |
| 190 | and ("json_body" in kwargs or "json" in kwargs) |
| 191 | ): |
| 192 | if "json_body" in kwargs: |
| 193 | json_body = kwargs.pop("json_body") |
| 194 | else: |
| 195 | json_body = kwargs.pop("json") |
| 196 | |
| 197 | body = json_encode(json_body).encode("utf-8") |
| 198 | |
| 199 | if content_type is None: |
| 200 | content_type = "application/json" |
| 201 | |
| 202 | super(Response, self).__init__( |
| 203 | body, status, headerlist, app_iter, content_type, *args, **kwargs |
| 204 | ) |
| 205 | |
| 206 | def _json_body__get(self): |
| 207 | return json_decode(self.body.decode(self.charset or "utf-8")) |
| 208 | |
| 209 | def _json_body__set(self, value): |
| 210 | self.body = json_encode(value).encode("utf-8") |
| 211 | |
| 212 | def _json_body__del(self): |
| 213 | return super(Response, self)._json_body__del() |
| 214 | |
| 215 | json = json_body = property(_json_body__get, _json_body__set, _json_body__del) |
| 216 | |
| 217 | |
| 218 | class Router(object): |
no outgoing calls