-
Notifications
You must be signed in to change notification settings - Fork 552
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
Is there a way to parse a number from string? #412
Comments
You could provide a Deserialize impl for UserId that handles integers and strings -- playground. Or if you want to handle strings only in this one place and not change the Deserialize impl, you would do that with a |
@dtolnay thanks, I thought about that too. But would not it be simpler for users just to have this as a macro attribute as I proposed above? This is a common problem with javascript, for example, where it does not have big integer numbers but floats only so services provide strings instead of numbers. |
Yes you're right! I missed the suggestion. We have been tracking this in serde-rs/serde#553 (comment) along with some other recurring customizations that would be valuable to provide in an auxiliary library. Would you be interested in picking some of the use cases from that thread and designing a library around them to provide implementations as serialize_with / deserialize_with functions? |
@dtolnay I'd try doing that, just depends on my free time. Now, it seems, I don't have any other choice, because my code really needs to parse string numbers and I want to do it gracefully :) So, this library obviously will not be able to use #[derive(Deserialize)]
struct A {
[serde(with = "my_auxiliary_library::string_to_int")]
value: u64
} Update: Ok, I think it is reasonable to provide both if possible: |
I made some very easy library because I needed this functionality very much. I think I will elaborate it later. |
Any official updates here? |
Thank you so much :) I have exactly the same trouble and your library really saved me from writing the same |
Let's say we have an example:
Is there a way to parse it into this:
At this moment it works perfectly and does not parse this because of types: the
u64
type is expected rather thanString
. However, I'd like to have an option to parse numbers from string too. What about doing it like this?I was unable to find something like this in the documentation, if there is such an option, feel free to point me right there.
The text was updated successfully, but these errors were encountered: