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

Error: out of range integral type conversion attempted #85

Closed
NAlexandrov opened this issue Dec 3, 2022 · 1 comment · Fixed by #87
Closed

Error: out of range integral type conversion attempted #85

NAlexandrov opened this issue Dec 3, 2022 · 1 comment · Fixed by #87
Labels
good first issue Good for newcomers help wanted Extra attention is needed

Comments

@NAlexandrov
Copy link
Contributor

When I try deserialize next value, I've got an error "out of range integral type conversion attempted".

    let json = r#"{"num": 5000000000}"#;
    let obj: serde_json::Value = serde_json::from_str(json)?;
    println!("num {}", obj["num"]);

    let vec = serde_json::to_vec(&obj).unwrap();
    let pb = serde_json::from_slice::<pbjson_types::Value>(&vec).unwrap();
    println!("{:#?}", pb);

This happens because next function throw an error:

    fn visit_u64<E>(self, v: u64) -> Result<Self::Value, E>
    where
        E: de::Error,
    {
        self.visit_u32(v.try_into().map_err(de::Error::custom)?)
    }

If look visit_u32 function, so number value wrapped in Kind::NumberValue which accept f64 type:

    fn visit_u32<E>(self, v: u32) -> Result<Self::Value, E>
    where
        E: de::Error,
    {
        Ok(Kind::NumberValue(v.into())) // f64
    }

As a result, the current conversion pipeline looks like u64 -> u32 -> f64. If it were u64 -> f64 (without u32), then we would not lose a significant range of numerical values (u32, f64].

For example like this:

    fn visit_u64<E>(self, v: u64) -> Result<Self::Value, E>
    where
        E: de::Error,
    {
        Ok(Kind::NumberValue(
            v.to_string().parse::<f64>().map_err(de::Error::custom)?,
        ))
    }
@tustvold tustvold added good first issue Good for newcomers help wanted Extra attention is needed labels Dec 3, 2022
@tustvold
Copy link
Contributor

tustvold commented Dec 3, 2022

Hi @NAlexandrov, thank you for the report.

I would be happy to review a PR that makes this change, otherwise I can add it to my list but I am not sure when I will have time to get to it.

FWIW the protobuf JSON representation recommends serializing 64-bt integers as strings, as f64 only has 52-bit precision, but I presume you are aware of this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Good for newcomers help wanted Extra attention is needed
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants