-
-
Notifications
You must be signed in to change notification settings - Fork 793
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support for Flattening #1179
Support for Flattening #1179
Conversation
There are two issues with the current implementation I'm not sure how to resolve. One is that you cannot The reason for both of them is the same: the inner type buffers up the values for the outer type as well. The solution I can see is that both generated deserializers would have to share the same buffer which would require a method on the I'm not sure yet if that works but the idea is that instead of the Then the generated enum deserializer would have to serializer over some sort of Alternatively maybe |
458ec96
to
49e302d
Compare
serde/src/private/de.rs
Outdated
@@ -2197,14 +2199,18 @@ impl<'a, 'de, E> MapAccess<'de> for FlatMapAccess<'a, 'de, E> | |||
T: DeserializeSeed<'de>, | |||
{ | |||
while let Some(item) = self.iter.next() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I still find this logic hard to follow, can you at least add some comments so future contributors know what's going on
I think I acted on all comments. |
This adds docs for the `#[serde(flatten)]` featur from this pull request: serde-rs/serde#1179
This adds docs for the `#[serde(flatten)]` feature from this pull request: serde-rs/serde#1179
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is great work. Thank you! I am ready to accept this but will file some follow-up issues.
Was this tested on tuple structs? Flattening newtypes specifically is the overwhelmingly significant usecase for me here. |
@Ralith please file an issue for flattening newtypes. |
@mitsuhiko really wanted this as well, thanks for pushing it through. |
Third time is the charm. This replaces #1177 and #1178 and adds support for
#[serde(flatten)]
. This pull request can provide the features for #119 as well as #941.What it does
This pull request adds support for flattening:
#[serde(flatten)]
field attribute which tells serde to flatten the container.Currently I only added support for maps and structs but I think this can be extended further to more types (including newtypes). Additionally right now the key must be a string. This requirement does not make a lot of sense and most likely any type of key can be supported with a bit of extra work.
Because of current limitations on the serializer interface structs that use flattening will be serialized as maps and not as structs.
Example usage
JSON:
Rust debug output of struct:
What's done
#[serde(flatten)]
on map types#[serde(flatten)]
on structs#[serde(flatten)]
on enum structs (without special tagging)#[serde(flatten)]
on newtypefeature = "deserialize_in_place"
Thanks so much @dtolnay and @oli-obk for the help in getting this to the current state.
Notes for the reviewer
visit_i32
etc.) for the field. This seems wasteful. Is there a better way to do this? In case not, maybe it's okay to throw away the handling for anything other than string and bytes?format_args!
at the moment. Some of the current helper methods do not work with unknown field info.Content
are now public internally so I can reuse them.