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

Support deserializing structs from lists #89

Merged
merged 1 commit into from
Nov 13, 2023
Merged

Support deserializing structs from lists #89

merged 1 commit into from
Nov 13, 2023

Conversation

bryanburgers
Copy link
Contributor

@bryanburgers bryanburgers commented Nov 13, 2023

serde and serde_json have support for deserializing structs and
adjacently-tagged enums from sequences. I had no idea!

#[derive(serde::Deserialize)]
struct Person {
    name: String,
    age: u8,
}

let input = r#"["Arthur Dent", 42]"#;

let person: Person = serde_json::from_str(input).unwrap();
assert_eq!(person.name, "Arthur Dent");
assert_eq!(person.age, 42);

Fixes #87

serde and serde_json have support for deserializing structs and
adjacently-tagged enums from sequences. I had no idea!

    #[derive(serde::Deserialize)]
    struct Person {
      name: String,
      age: u8,
    }

    let input = r#"["Arthur Dent", 42]"#;

    let person: Person = serde_json::from_str(input).unwrap();
    assert_eq!(person.name, "Arthur Dent");
    assert_eq!(person.age, 42);

Fixes #87
Copy link

@jlajoie jlajoie left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Eww but also, neat? Had no clue 👍🏼

@bryanburgers bryanburgers merged commit e934f2f into main Nov 13, 2023
6 checks passed
@bryanburgers bryanburgers deleted the issue-87 branch November 13, 2023 14:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Adjacently tagged enum not deserialisable; ExpectedMap
2 participants