-
Notifications
You must be signed in to change notification settings - Fork 751
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
Labels
Comments
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)
}
} |
Considering how this is such a common pattern, is there not a way to do this with attributes? |
This is available in serde_with as |
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
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. |
2 tasks
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
From IRC:
The text was updated successfully, but these errors were encountered: