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

Is there any way to make a parameter "either bool or any other String" when deserializing? #117

Open
czy-29 opened this issue Jun 2, 2024 · 0 comments

Comments

@czy-29
Copy link

czy-29 commented Jun 2, 2024

I tried either, but it didn't work as expected:

use either::Either;
use serde::Deserialize;
use serde_urlencoded::from_str;

#[derive(Deserialize)]
struct ParamEither {
    #[serde(with = "either::serde_untagged")]
    param: Either<bool, String>,
}

impl ParamEither {
    fn print_de(input: &str) {
        match from_str::<Self>(input) {
            Ok(Self { param }) => match param {
                Either::Left(param) => println!("param: bool({})", param),
                Either::Right(text) => println!("param: String(\"{}\")", text),
            },
            Err(err) => eprintln!("error: {}", err),
        }
    }
}

fn main() {
    // output: param: String("text")
    // this is expected!
    ParamEither::print_de("param=text");

    // output: param: String("true")
    // expect: param: bool(true)
    ParamEither::print_de("param=true");
}

The deserialization process did not resolve true to bool.
Is this a bug? If so, is it our bug, either's bug, or serde's bug?
If not, how should I correctly implement this feature?

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

No branches or pull requests

1 participant