I am trying to deserialize a field "name" into "firstname" and "lastname".
My struct:
struct User {
#[serde(rename = "name", deserialize_with = "deserialize_firstname")]
firstname: String,
#[serde(deserialize_with = "deserialize_lastname")]
#[serde(rename = "name")]
lastname: String,
}
And the deserialize function is the following:
fn deserialize_firstname<'de, D>(deserializer: D) -> Result<String, D::Error>
where
D: serde::Deserializer<'de>,
{
let s: &str = serde::Deserialize::deserialize(deserializer)?;
let re = Regex::new(r"^\W*([\w-]+)").unwrap();
let m = re.find(s);
match m {
Some(value) => Ok( value.as_str().to_string()),
None => Ok("".to_string()),
}
}
(it is about the same thing for lastname).
I get the following error:
err: DeserializeError { field: None, kind: Message("missing field `name`") }
My question: How to deserialize a field into two different fields?
Serde can flatten structs, making the Rust representation more nested than the serialized representation, but it can't unflatten, which you're trying to do here. Instead, you can do that yourself by annotating the entire struct. Here we have
UserSerdewhich reflects the serialized structure, and convert it toUser, which is what you're looking for. (playground)Note that if you're using this to store arbitrary human names, splitting them into first and last names is not a good idea. At least have a fallback that's just a normal
String.