fn de_String_to_numeric<'de, D, T>(deserializer: D) -> Result<T, D::Error>
where
D: Deserializer<'de>
{
let buf = String::deserialize(deserializer)?;
// convert into serde's custom Error type
buf.parse::<T>().map_err(serde::de::Error::custom)
}
I would like to reduce the boiler-plate deserializing code as some fields might have u8, u64, etc. Is there any way for us to define a type to hint serde via macro attribute, so I don't have to implement specific deserialize function for u8, u64, and on ?
PS: It works just fine if I don't have T in code, and I need to explicitly replace T with specific type to convert to.
Your de_String_to_numeric function looks fine except for the missing bounds on T, but rustc tells you in the error message that you need to add this to the where clause.
T: std::str::FromStr,
<T as std::str::FromStr>::Err: std::fmt::Display
Your function doesn't need a string, so you could optimize the code a bit. It doesn't look as neat anymore though.
Thank you, also thanks for additional information. Although I found the solution at similar time as your post. But I will mark your as solution as it drives deeper to the cause with added info. Thanks.