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

Deserialize true/false from 1/0 #456

Closed
TonalidadeHidrica opened this issue May 19, 2022 · 5 comments · Fixed by #462
Closed

Deserialize true/false from 1/0 #456

TonalidadeHidrica opened this issue May 19, 2022 · 5 comments · Fixed by #462
Labels
enhancement New feature or request

Comments

@TonalidadeHidrica
Copy link

It'd be useful to have bool_from_int shown in serde-rs/serde#1344.

@jonasbb
Copy link
Owner

jonasbb commented May 19, 2022

Hi, thanks for the feedback. Could you please expand more on what exactly your needs are? The linked issue only mentions parsing from 0/1. But there are many variants possible. For integers you could also allow all numbers and check for != 0. Both cases can be covered with serde_with::formats::Strictness. It can be modelled as BoolFromInt<S: Strictness>.

Do you also require parsing strings to bools, something like "t" or "f"?

@TonalidadeHidrica
Copy link
Author

For my personal purpose, I expect only the integer 0 to be parsed as false, the integer 1 as true, and any other value as an error. I'm not sure if somebody else or some other purpose call for parsing "0" / "1", 0 / != 0, "true" / "false", "yes" / "no" and so on.

@jonasbb jonasbb added breaking changes These changes require a major version number increase enhancement New feature or request and removed breaking changes These changes require a major version number increase labels May 28, 2022
@bors bors bot closed this as completed in d3140e1 May 28, 2022
@piavgh
Copy link

piavgh commented Jul 30, 2023

@jonasbb : after adding BoolFromInt, I can not use parameter true or false anymore (only 0 or 1 works with bool param now)

What should I do to support both ways?

@jonasbb
Copy link
Owner

jonasbb commented Jul 30, 2023

@piavgh Please open new issues for help requests and don't post on old topics with unrelated problems.
You can make use of PickFirst, e.g., PickFirst<(_, BoolFromInt)>, but since you don't provide any details of which format you target or what you need for serialization I cannot help you more.

@piavgh
Copy link

piavgh commented Aug 1, 2023

@piavgh Please open new issues for help requests and don't post on old topics with unrelated problems. You can make use of PickFirst, e.g., PickFirst<(_, BoolFromInt)>, but since you don't provide any details of which format you target or what you need for serialization I cannot help you more.

Thanks, I created a new issue here #631

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

Successfully merging a pull request may close this issue.

3 participants