Skip to content

How decode a struct(json) in from field form? #2686

Answered by mamcx
mamcx asked this question in Q&A
Discussion options

You must be logged in to vote

For the posterity: In reddit I get a solution, and using:

//Serde with FromStr/Display
pub mod from {
    pub mod serde_str {
        use serde::{de::Error as _, Deserialize, Deserializer, Serialize, Serializer};

        pub fn deserialize<'de, D, T>(deserializer: D) -> Result<T, D::Error>
        where
            D: Deserializer<'de>,
            T: std::str::FromStr,
            <T as std::str::FromStr>::Err: std::fmt::Display,
        {
            String::deserialize(deserializer)?
                .parse::<T>()
                .map_err(|e| D::Error::custom(format!("{}", e)))
        }

        pub fn serialize<S, T>(value: &T, serializer: S) -> Result<S::Ok, S::Error>
        where

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by robjtede
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
1 participant