Skip to content
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

Allow to flatten sequences/tuples #1905

Open
Mingun opened this issue Oct 11, 2020 · 2 comments
Open

Allow to flatten sequences/tuples #1905

Mingun opened this issue Oct 11, 2020 · 2 comments

Comments

@Mingun
Copy link
Contributor

Mingun commented Oct 11, 2020

It will be useful to allow flatten sequences, especially that will very helpful to model of XSD choice types.

Example for JSON (but it's not as expressive as for XML): Playground

use serde::{Deserialize, Serialize};

#[derive(Debug, PartialEq, Serialize, Deserialize)]
enum Enum { A, B, C }

#[derive(Debug, PartialEq, Serialize, Deserialize)]
struct List {
    #[serde(flatten)]
    items: Vec<Enum>,
}

#[test]
fn ser() {
    let list = List {
        items: vec![
            Enum::A,
            Enum::C,
            Enum::B,
        ]
    };
    assert_eq!(
        quick_xml::se::to_string(&list).unwrap(),
        r#"<List><A/><C/><B/></List>"#
    );
    // Currently failed with
    // "can only flatten structs and maps (got a sequence)"
}

#[test]
fn de() {
    let list: List = quick_xml::de::from_str(r#"
      <List>
        <A/>
        <C/>
        <B/>
      </List>
    "#).unwrap();
    assert_eq!(
        list,
        List {
            items: vec![
                Enum::A,
                Enum::C,
                Enum::B,
            ]
        }
    );
    // Currently failed with
    // "can only flatten structs and maps"
}
@praseodym
Copy link

praseodym commented Dec 8, 2022

Flattening is possible in combination with EnumMap from serde_with. Example:

use serde_with::EnumMap;

#[serde_with::serde_as]
#[derive(Serialize, Deserialize)]
struct VecEnumValues (
    #[serde_as(as = "EnumMap")]
    #[serde(flatten)]
    Vec<EnumValue>,
);

@Mingun Mingun mentioned this issue Jul 11, 2023
@cathaysia
Copy link

I expect flat a vec shoule be like this:

struct A{
   #[serde(flatten)]
   data: Vec<int>
}


fn main(){
    let a = A { 
        data: vec![1,2,3,4,5]
    };
    // ser a, then a should be 
    // A {
    //    data: 1,
    //    data: 2 ,
    //    data: 3,
    //    data: 4,
    //    data: 5
    // }
    //
}

Frankkkkk added a commit to Frankkkkk/quick-xml that referenced this issue Apr 19, 2024
Add a small example about flattened enum deserialization. This is a
small workaround around serde's [serde's
issue](serde-rs/serde#1905).

Big thanks to @Mingun for (most) of the code + help!

Signed-off-by: Frank Villaro-Dixon <[email protected]>
Frankkkkk added a commit to Frankkkkk/quick-xml that referenced this issue Apr 20, 2024
Add a small example about flattened enum deserialization. This is a
small workaround around serde's [serde's
issue](serde-rs/serde#1905).

Big thanks to @Mingun for (most) of the code + help!

Signed-off-by: Frank Villaro-Dixon <[email protected]>
Frankkkkk added a commit to Frankkkkk/quick-xml that referenced this issue Apr 21, 2024
Add a small example about flattened enum deserialization. This is a
small workaround around serde's [serde's
issue](serde-rs/serde#1905).

Big thanks to @Mingun for (most) of the code + help!

Signed-off-by: Frank Villaro-Dixon <[email protected]>
Frankkkkk added a commit to Frankkkkk/quick-xml that referenced this issue Apr 21, 2024
Add a small example about flattened enum deserialization. This is a
small workaround around serde's [serde's
issue](serde-rs/serde#1905).

Big thanks to @Mingun for (most) of the code + help!

Signed-off-by: Frank Villaro-Dixon <[email protected]>
Frankkkkk added a commit to Frankkkkk/quick-xml that referenced this issue Apr 25, 2024
Add a small example about flattened enum deserialization. This is a
small workaround around serde's [serde's
issue](serde-rs/serde#1905).

Big thanks to @Mingun for (most) of the code + help!

Signed-off-by: Frank Villaro-Dixon <[email protected]>
Mingun pushed a commit to Frankkkkk/quick-xml that referenced this issue Apr 26, 2024
Add a small example about flattened enum deserialization. This is a
small workaround around serde's [serde's
issue](serde-rs/serde#1905).

Big thanks to @Mingun for (most) of the code + help!

Signed-off-by: Frank Villaro-Dixon <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

3 participants