File tree Expand file tree Collapse file tree 3 files changed +78
-0
lines changed
Expand file tree Collapse file tree 3 files changed +78
-0
lines changed Original file line number Diff line number Diff line change 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 )
Original file line number Diff line number Diff line change 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.
Original file line number Diff line number Diff line change 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.
You can’t perform that action at this time.
0 commit comments