Skip to content

Error: out of range integral type conversion attempted #85

@NAlexandrov

Description

@NAlexandrov

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)?,
        ))
    }

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions