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

Deserialize a type using FromStr #908

Closed
dtolnay opened this issue Apr 28, 2017 · 5 comments · Fixed by jonasbb/serde_with#156
Closed

Deserialize a type using FromStr #908

dtolnay opened this issue Apr 28, 2017 · 5 comments · Fixed by jonasbb/serde_with#156
Labels

Comments

@dtolnay
Copy link
Member

dtolnay commented Apr 28, 2017

From IRC:

Is there a way to tell serde to desrialize my struct using it's FromStr implementation?

@dtolnay
Copy link
Member Author

dtolnay commented Apr 28, 2017

extern crate serde;

use std::str::FromStr;
use serde::{de, Deserialize, Deserializer};

struct S;

impl FromStr for S {
    type Err = String;
    fn from_str(s: &str) -> Result<Self, Self::Err> {
        unimplemented!()
    }
}

impl<'de> Deserialize<'de> for S {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
        where D: Deserializer<'de>
    {
        let s = String::deserialize(deserializer)?;
        FromStr::from_str(&s).map_err(de::Error::custom)
    }
}

@dtolnay dtolnay closed this as completed Apr 28, 2017
@clarfonthey
Copy link
Contributor

Considering how this is such a common pattern, is there not a way to do this with attributes?

@njam
Copy link

njam commented Jun 17, 2019

This is available in serde_with as display_fromstr.

jonasbb added a commit to jonasbb/serde_with that referenced this issue Aug 16, 2020
The crate already supports using Display and FromStr for de/serializing,
but only using the with-attribute and the serde_as system. Implementing
De/Serialize still required manual code.
This derive closes that gap.

Fixes #86
Closes serde-rs/serde#908
bors bot added a commit to jonasbb/serde_with that referenced this issue Aug 16, 2020
156: Add derive macros to implement De/Serialize based on Display and FromStr r=jonasbb a=jonasbb


The crate already supports using Display and FromStr for de/serializing,
but only using the with-attribute and the serde_as system. Implementing
De/Serialize still required manual code.
This derive closes that gap.

Fixes #86
Closes serde-rs/serde#908

Co-authored-by: Jonas Bushart <jonas@bushart.org>
battlmonstr added a commit to akula-bft/akula that referenced this issue Sep 12, 2021
@blueforesticarus
Copy link

blueforesticarus commented Nov 25, 2022

The serde-with method works for fields of a struct, but if you want to serialize/deserialize the whole struct as a string via Display and FromStr, there does not seem to be any easy way to do that.

@adwhit
Copy link

adwhit commented Nov 26, 2022

@blueforesticarus sure it works, see: https://docs.rs/serde_with/latest/serde_with/derive.DeserializeFromStr.html

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

Successfully merging a pull request may close this issue.

5 participants