Take into account only not skipped flatten fields when choose serialization form
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Consequence: `FlattenSkipSerializing` - uses `serialize_struct` instead of `serialize_map`
authored by Mingun · committed by Mingun · parent 547d843 · Browse files · 3 files·+5 added−39 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+5 added−39 removed
- other:+5 added−39 removed
Provenance
- Commit
77a6a9d- Tree
1cb70fd- Author
Mingun <Alexander_Sergey@mail.ru>- Committer
Mingun <Alexander_Sergey@mail.ru>- Parent
547d843
3 files changed+5 added−39 removed
modified:serde_derive/src/internals/ast.rs+1 added-12 removed
61 unmodified lines62 cx: &Ctxt,63 item: &'a syn::DeriveInput,64 derive: Derive,65 ) -> Option<Container<'a>> {66- let mut attrs = attr::Container::from_ast(cx, item);66+ let attrs = attr::Container::from_ast(cx, item);67 68 let mut data = match &item.data {69 syn::Data::Enum(data) => Data::Enum(enum_from_ast(cx, &data.variants, attrs.default())),70 syn::Data::Struct(data) => {5 unmodified lines76 return None;77 }78 };79 80- let mut has_flatten = false;80 match &mut data {81 Data::Enum(variants) => {82 for variant in variants {83 variant.attrs.rename_by_rules(attrs.rename_all_rules());84 for field in &mut variant.fields {86- if field.attrs.flatten() {87- has_flatten = true;88- }85 field.attrs.rename_by_rules(86 variant87 .attrs88 .rename_all_rules()3 unmodified lines92 }93 }94 Data::Struct(_, fields) => {95 for field in fields {100- if field.attrs.flatten() {101- has_flatten = true;102- }96 field.attrs.rename_by_rules(attrs.rename_all_rules());97 }98 }99 }100 108- if has_flatten {109- attrs.mark_has_flatten();110- }111-101 let mut item = Container {102 ident: item.ident.clone(),103 attrs,104 data,101 unmodified linesmodified:serde_derive/src/internals/attr.rs-26 removed
214 unmodified lines215 type_try_from: Option<syn::Type>,216 type_into: Option<syn::Type>,217 remote: Option<syn::Path>,218 identifier: Identifier,219- /// True if container is a struct and has a field with `#[serde(flatten)]`,220- /// or is an enum with a struct variant which has a field with221- /// `#[serde(flatten)]`.222- ///223- /// ```ignore224- /// struct Container {225- /// #[serde(flatten)]226- /// some_field: (),227- /// }228- /// enum Container {229- /// Variant {230- /// #[serde(flatten)]231- /// some_field: (),232- /// },233- /// }234- /// ```235- has_flatten: bool,219 serde_path: Option<syn::Path>,220 is_packed: bool,221 /// Error message generated when type can't be deserialized222 expecting: Option<String>,362 unmodified lines585 type_try_from: type_try_from.get(),586 type_into: type_into.get(),587 remote: remote.get(),588 identifier: decide_identifier(cx, item, field_identifier, variant_identifier),606- has_flatten: false,589 serde_path: serde_path.get(),590 is_packed,591 expecting: expecting.get(),592 non_exhaustive,59 unmodified lines652 pub fn identifier(&self) -> Identifier {653 self.identifier654 }655 674- pub fn has_flatten(&self) -> bool {675- self.has_flatten676- }677-678- pub fn mark_has_flatten(&mut self) {679- self.has_flatten = true;680- }681-656 pub fn custom_serde_path(&self) -> Option<&syn::Path> {657 self.serde_path.as_ref()658 }659 1212 unmodified linesmodified:serde_derive/src/ser.rs+4 added-1 removed
295 unmodified lines296 fields.len(),297 u32::MAX298 );299 300- if cattrs.has_flatten() {300+ let has_non_skipped_flatten = fields301+ .iter()302+ .any(|field| field.attrs.flatten() && !field.attrs.skip_serializing());303+ if has_non_skipped_flatten {304 serialize_struct_as_map(params, fields, cattrs)305 } else {306 serialize_struct_as_struct(params, fields, cattrs)307 }1043 unmodified lines