serde icon indicating copy to clipboard operation
serde copied to clipboard

Add new field attributes: from/try_from/from_str/into

Open nolanderc opened this issue 4 years ago • 6 comments

Addresses #1550.

The new field attributes from, try_from, from_str and into act as a shorthand for the deserialize_with and serialize_with attributes. They mirror the functionality found in the container attributes from, try_from and into with the exception of from_str, which is a new kind of attribute this PR adds. A field with the from_str attribute first deserializes the field as a String and then attempts to convert it into the correct type by calling FromStr::from_str.

Example

#[derive(Debug, Deserialize)]
struct Foo {
    #[serde(from = "i32")]
    sign: Sign,
    #[serde(from_str)]
    number: u32,
}

#[derive(Debug, PartialEq)]
enum Sign {
    Negative,
    Zero,
    Positive,
}

impl From<i32> for Sign {
    fn from(v: i32) -> Sign {
        match v {
            _ if v < 0 => Sign::Negative,
            _ if v == 0 => Sign::Zero,
            _ => Sign::Positive
        }
    }
}

fn main() {
    let json = r#"{ "sign": -1, "number": "14" }"#;

    let foo: Foo = serde_json::from_str(json).unwrap();

    assert_eq!(foo.sign, Sign::Negative);
    assert_eq!(foo.number, 14);
}

nolanderc avatar Sep 07 '19 11:09 nolanderc

@dtolnay , David, this is an interesting PR. This "from" / "into" approach can make for less boilerplate than with using variant type enums. I'm happy to spend some to finish what @nolanderc started, if you think it has legs.

rimutaka avatar Oct 15 '20 10:10 rimutaka

@dtolnay what is the current status of this PR. The feature offered by it is interesting for me in a current project, so I would also be willing to put some time into this if that means it has a good chance of getting it merged.

davidv1992 avatar Feb 23 '21 08:02 davidv1992

If you're adding from_str, then to_string (or display) would be pretty neat. It'd be nice not to have to write a wrapper for that.

abonander avatar May 22 '21 00:05 abonander

Agree that this feature would be very useful

gavrilikhin-d avatar Jun 03 '23 02:06 gavrilikhin-d

I also need exactly this!

Many crates implement e.g. From<String> on their type, but have no serde support. Please add this feature. We need it!

Also, what is the best current workaround?

jbirnick avatar Sep 05 '23 14:09 jbirnick

@jbirnick I do not know what the best is, but I am using deserialize_with = try_from_string_to_u32 of course this not working then for mixed types.

JHenneberg avatar Oct 05 '23 10:10 JHenneberg