Skip to content

Commit

Permalink
Add documentation about custom JSON serialization
Browse files Browse the repository at this point in the history
  • Loading branch information
JayH5 committed Sep 12, 2020
1 parent 7495f02 commit 1e5602b
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions docs/responses.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,30 @@ async def app(scope, receive, send):
await response(scope, receive, send)
```

#### Custom JSON serialization

If you need fine-grained control over JSON serialization, you can subclass
`JSONResponse` and override the `render` method.

For example, if you wanted to use a third-party JSON library such as
[orjson](https://pypi.org/project/orjson/):

```python
from typing import Any

import orjson
from starlette.responses import JSONResponse


class OrjsonResponse(JSONResponse):
def render(self, content: Any) -> bytes:
return orjson.dumps(content)
```

In general you *probably* want to stick with `JSONResponse` by default unless
you are micro-optimising a particular endpoint or need to serialize non-standard
object types.

### RedirectResponse

Returns an HTTP redirect. Uses a 307 status code by default.
Expand Down

0 comments on commit 1e5602b

Please sign in to comment.