Skip to content

Commit 2833723

Browse files
committed
Added docs for flattening
This adds docs for the `#[serde(flatten)]` featur from this pull request: serde-rs/serde#1179
1 parent 026b17a commit 2833723

File tree

3 files changed

+78
-0
lines changed

3 files changed

+78
-0
lines changed

_src/SUMMARY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
* [Enum representations](enum-representations.md)
2424
* [Borrowing data](borrow.md)
2525
* [Default value for a field](attr-default.md)
26+
* [Struct flattening](attr-flatten.md)
2627
* [Handwritten generic type bounds](attr-bound.md)
2728
* [Deserialize for custom map type](deserialize-map.md)
2829
* [Array of values without buffering](stream-array.md)

_src/attr-flatten.md

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
# Struct flattening
2+
3+
Limited support for flattening of data is supported by serde through the `#[serde(flatten)]`
4+
attribute. It can be used for a variety of common purposes when working with JSON data
5+
in particular.
6+
7+
## Refactor common elements
8+
9+
For instance flatten can be used to move common
10+
11+
```rust
12+
#[derive(Serialize, Deserialize, Debug)]
13+
struct PaginatedResponse<U> {
14+
#[serde(flatten)]
15+
pagination: Pagination,
16+
items: Vec<U>
17+
}
18+
19+
#[derive(Serialize, Deserialize, Debug)]
20+
struct Pagination {
21+
limit: u64,
22+
offset: u64,
23+
total: u64,
24+
}
25+
26+
#[derive(Serialize, Deserialize, Debug)]
27+
struct User {
28+
id: String,
29+
username: String,
30+
email: Option<String>,
31+
}
32+
```
33+
34+
Then `PaginatedResponse<User>` can be deserialized from this data:
35+
36+
```json
37+
{
38+
"limit": 100,
39+
"offset": 200,
40+
"total": 10553,
41+
"items": [
42+
{"id": "49824073-979f-4814-be10-5ea416ee1c2f", username": "john_doe"},
43+
...
44+
]
45+
}
46+
```
47+
48+
## Capture additional data
49+
50+
A second common usecase for flatten is to collect all remaining data in a struct
51+
into a hashmap:
52+
53+
```rust
54+
55+
#[derive(Serialize, Deserialize, Debug)]
56+
struct Object {
57+
id: String,
58+
#[serde(rename = "type")]
59+
ty: String,
60+
#[serde(flatten)]
61+
extra: HashMap<String, String>,
62+
}
63+
```
64+
65+
This way additional data in an object can be collected into the `extra` hash map
66+
for later processing.

_src/field-attrs.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,17 @@
1717
`default = "empty_value"` would invoke `empty_value()` and `default =
1818
"SomeTrait::some_default"` would invoke `SomeTrait::some_default()`.
1919

20+
- #### `#[serde(flatten)]`
21+
22+
Flatten the contents of this field into the container it's defined in.
23+
24+
This removes one level of structure in a map or a value that serializes into
25+
a map. Structs are automatically converted into maps when flattening is
26+
used. This can for instance be used to capture the remaining fields in a
27+
JSON object into a hash map or to move common keys into a separate object.
28+
29+
This feature currently cannot be used with internally or untagged enums.
30+
2031
- ##### `#[serde(skip)]`
2132

2233
Skip this field: do not serialize or deserialize it.

0 commit comments

Comments
 (0)