Skip to content

Commit

Permalink
Restructure
Browse files Browse the repository at this point in the history
  • Loading branch information
LukeMathWalker committed Dec 31, 2023
1 parent dd11065 commit 0e1b207
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 14 deletions.
15 changes: 15 additions & 0 deletions docs/guide/request_data/body/deserializers/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Overview

Deserializers transform the raw body into a Rust type.
They take care of parsing, basic validation and security safeguards.
They're the family of extractors you'll use most often in your Pavex application.

Out of the box, Pavex supports the following formats:

* [JSON](json.md)

## Tower of abstractions

All deserializers are built on top of Pavex's [low-level primitives](../byte_wrappers.md).
As such, they all share the same security safeguards, such as body size limits.
Check out [the relevant guide](../byte_wrappers.md) for details on how to configure them.
Original file line number Diff line number Diff line change
@@ -1,49 +1,43 @@
# Deserializers

Deserializers transform the raw body into a Rust type.
They take care of parsing, basic validation and security safeguards.
They're the family of extractors you'll use most often in your Pavex application.

## Json
# Json

[`JsonBody<T>`][JsonBody] buffers the body in memory and deserializes it as JSON,
according to the type `T` you specify.

### Extraction
## Extraction

You inject [`JsonBody<T>`][JsonBody] in your handler to access the parsed body:

--8<-- "doc_examples/guide/request_data/json/project-extraction.snap"

### Deserialization
## Deserialization

The newly defined struct must be **deserializable**—i.e. it must implement the [`serde::Deserialize`][serde::Deserialize] trait.
You can derive [`serde::Deserialize`][serde::Deserialize] in most cases.

--8<-- "doc_examples/guide/request_data/json/project-struct_with_attr.snap"

### Avoiding allocations
## Avoiding allocations

If you want to minimize memory usage, you can try to avoid unnecessary heap memory allocations when deserializing
string-like fields from the body of the incoming request.
Pavex supports this use case—**you can borrow from the request body**.

#### Escape sequences
### Escape sequences

It is not always possible to avoid allocations, though.
In particular,
Pavex must allocate a new `String` if the JSON string you are trying to deserialize contains escape sequences,
such as `\n` or a `\"`.
Using a `&str` in this case would result in a runtime error when attempting the deserialization.

#### Cow
### Cow

We recommend using [`Cow<'_, str>`][Cow] as your field type for string-like parameters.
It borrows from the request's body if possible, it allocates a new `String` if it can't be avoided.

[`Cow<'_, str>`][Cow] strikes a balance between performance and robustness: you don't have to worry about a runtime error
if the field contains escape sequences, but you tried to use `&str` as its field type.

[JsonBody]: ../../../api_reference/pavex/request/body/struct.JsonBody.html
[JsonBody]: ../../../../api_reference/pavex/request/body/struct.JsonBody.html
[serde::Deserialize]: https://docs.rs/serde/latest/serde/trait.Deserialize.html
[Cow]: https://doc.rust-lang.org/std/borrow/enum.Cow.html
4 changes: 3 additions & 1 deletion mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,9 @@ nav:
- guide/request_data/query/query_parameters.md
- "Body":
- guide/request_data/body/index.md
- guide/request_data/body/deserializers.md
- "Deserializers":
- guide/request_data/body/deserializers/index.md
- guide/request_data/body/deserializers/json.md
- guide/request_data/body/byte_wrappers.md
- "Dependency injection":
- guide/dependency_injection/index.md
Expand Down

0 comments on commit 0e1b207

Please sign in to comment.