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

Allow bounded numbers to be deserialized from strings #296

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

DrChat
Copy link
Contributor

@DrChat DrChat commented Mar 16, 2025

Fixes #295.

This allows bounded integers to be deserialized from strings.
The specific implementation comes from serde-rs/json#412.

Note: The diff is a bit of a mess because I had to indent everything.
This is the specific code that was added (for all three types):

            impl<'de, const MAX: $primitive> Deserialize<'de> for $lim<MAX> {
                fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
                    where D: serde::Deserializer<'de>
                {
                    struct Visitor<const MAX: $primitive>;

                    impl<'de, const MAX: $primitive> serde::de::Visitor<'de> for Visitor<MAX> {
                        type Value = $lim<MAX>;

                        fn expecting(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
                            f.write_str("integer as a number or string")
                        }

                        fn [<visit_ $primitive>]<E>(self, val: $primitive) -> Result<Self::Value, E>
                            where E: serde::de::Error
                        {
                            $lim::new(val).map_err(serde::de::Error::custom)
                        }

                        fn visit_str<E>(self, val: &str) -> Result<Self::Value, E>
                            where E: serde::de::Error
                        {
                            let v = val.parse().map_err(serde::de::Error::custom)?;
                            $lim::new(v).map_err(serde::de::Error::custom)
                        }
                    }

                    deserializer.deserialize_any(Visitor)
                }
            }

@sugyan
Copy link
Owner

sugyan commented Mar 17, 2025

I didn't know that this kind of conversion is possible.
But I don't understand why it is necessary to deserialize from string to integer. Can you tell me in what specific cases this is necessary?

@sugyan
Copy link
Owner

sugyan commented Mar 22, 2025

Oh... sorry, I didn't read the #295 one. I see, so you are saying that this kind of conversion is necessary when HTTP query parameters are received, etc.

@DrChat
Copy link
Contributor Author

DrChat commented Mar 22, 2025

All good! And yup - axum will treat all query parameters as strings when performing deserialization, so we'd have to handle that here in order to be compatible.

I'm also open to other ways to implement this too - this just seemed to be the best way to do it based on what I saw :)

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

Successfully merging this pull request may close these issues.

Bounded integer types cannot be deserialized from a string
2 participants