-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
dd11065
commit 0e1b207
Showing
3 changed files
with
25 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
20 changes: 7 additions & 13 deletions
20
.../guide/request_data/body/deserializers.md → ...e/request_data/body/deserializers/json.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters