gloo icon indicating copy to clipboard operation
gloo copied to clipboard

Conversion BigInt/u64 doesn't work

Open adimit opened this issue 3 years ago • 0 comments

Describe the Bug

With serde-wasm-bindgen conversion between bigint and u64 is unproblematic. When I have a struct with a u64 in Rust, I get a bigint (or BigInt) in JS-land. With gloo-utils u64 is translated to number, losing precision.

Steps to Reproduce

Rust:

#[derive(serde::Serialize, serde::Deserialize)]
struct Foo {
    field: u64,
}

#[wasm_bindgen]
pub fn make_a_mistake() -> JsValue {
    <JsValue as gloo_utils::format::JsValueSerdeExt>::from_serde(&Foo {
        field: 12815844532926915242u64,
    })
    .unwrap()
}

Then in JS:

console.log(make_a_mistake());

Expected Behavior

Object { field: 12815844532926915242 }

Actual Behavior

Result:

Object { field: 12815844532926915000 }

And 12815844532926915000 ≠ 12815844532926915242.

Additional Context

Without knowing how the library really works, I'm guessing the u64 gets stringified Rust-side into a neat { field: 12815844532926915242 } and then JSON.parse() mangles that value. I'm not sure this scenario can be handled with this approach at all. In that case, using serde-wasm-bindgen works well, and should be used instead. The only way to make this work, I guess, is to treat the number as a string and then transparently post-process it in JS. I'm not sure this within the scope of your project. If it isn't, feel free to close this issue!

adimit avatar Feb 07 '23 23:02 adimit