Skip to content
This repository has been archived by the owner on Mar 25, 2024. It is now read-only.

Deserialize Error 128 bit hex number #196

Closed
Maeda-Naoki opened this issue Feb 26, 2021 · 2 comments
Closed

Deserialize Error 128 bit hex number #196

Maeda-Naoki opened this issue Feb 26, 2021 · 2 comments

Comments

@Maeda-Naoki
Copy link

Maeda-Naoki commented Feb 26, 2021

I can't deserialize 128 bit hex number.
The error message shows "invalid type: string".
I expect to recognize it as a number.

Error message

thread 'main' panicked at 'called Result::unwrap() on an Err value: Message("invalid type: string "0x1234567890ABCDEF1234567890ABCDEF", expected u128", Some(Pos { marker: Marker { index: 7, line: 2, col: 3 }, path: "x" }))', src/main.rs:15:57

use serde::Deserialize;
use indoc::indoc;

#[derive(Debug, Deserialize)]
struct Data {
    x: u128
}

fn main() {
    let yaml = indoc! {"
        ---
        x: 0x1234567890ABCDEF1234567890ABCDEF
    "};

    // ★Deserialize error
    let deserialized: Data = serde_yaml::from_str(yaml).unwrap();
}
@Akaame
Copy link

Akaame commented Aug 26, 2021

Using yaml_rust

extern crate yaml_rust;
use yaml_rust::{YamlLoader, YamlEmitter};

fn main() {
    let s = r#"
        x: 0x1234567890ABCDEF1234567890ABCDEF
        y: 0x1234567890AB
    "#;
    let docs = YamlLoader::load_from_str(s).unwrap();
    println!("{:?}", docs);
    // prints
    // [Hash({String("x"): String("0x1234567890ABCDEF1234567890ABCDEF"), String("y"): Integer(20015998341291)})]
}

Related:
This PR added i128 and u128 capabilities. You can deserialize stuff like

---
x: 18446744073709551615555555555

The problem only exists for HEX representation (because of underlying library behaviour shown above).

@dtolnay
Copy link
Owner

dtolnay commented Jul 28, 2022

This is fixed in 0.9.

[src/main.rs:17] deserialized = Data {
    x: 24197857200151252728969465429440056815,
}

@dtolnay dtolnay closed this as completed Jul 28, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants