Giter Site home page Giter Site logo

Deserializing large numbers fail about seed HOT 11 CLOSED

kujeger avatar kujeger commented on June 17, 2024
Deserializing large numbers fail

from seed.

Comments (11)

fosskers avatar fosskers commented on June 17, 2024

Which JSON backend are you telling Seed to use - serde_json or serde-wasm-bindgen?

from seed.

kujeger avatar kujeger commented on June 17, 2024

Whichever is the default; looks like serde-wasm-bindgen

from seed.

fosskers avatar fosskers commented on June 17, 2024

Try serde_json and see what happens. It'll probably fix it in the meantime, although swb is supposed to have BigInt support 🤔

from seed.

kujeger avatar kujeger commented on June 17, 2024

Looks like I was wrong; according to https://docs.rs/crate/seed/latest/features serde-json is the default.

Manually specifying serde-json in features gives the same result. Using serde-wasm-bindgen instead completely fails to deserialize the same numbers with this error:

JsonError(Serde(JsValue(Error: invalid type: floating point `12345678901234568`, expected i64

from seed.

fosskers avatar fosskers commented on June 17, 2024

From Rust itself:

In: 9223372036854776000_i64
error: literal out of range for `i64`
 --> src/main.rs:4:1
  |
4 | 9223372036854776000_i64
  | ^^^^^^^^^^^^^^^^^^^^^^^
  |
  = note: `#[deny(overflowing_literals)]` on by default
  = note: the literal `9223372036854776000_i64` does not fit into the type `i64` whose range is `-9223372036854775808..=9223372036854775807`
  = help: consider using the type `u64` instead

but it does fit into u64. Consider changing the number type that you're using, if you know that value can never be negative.

from seed.

flosse avatar flosse commented on June 17, 2024

@kujeger I downloaded your example code and it just works.

from seed.

kujeger avatar kujeger commented on June 17, 2024

From Rust itself:

In: 9223372036854776000_i64
error: literal out of range for `i64`
 --> src/main.rs:4:1
  |
4 | 9223372036854776000_i64
  | ^^^^^^^^^^^^^^^^^^^^^^^
  |
  = note: `#[deny(overflowing_literals)]` on by default
  = note: the literal `9223372036854776000_i64` does not fit into the type `i64` whose range is `-9223372036854775808..=9223372036854775807`
  = help: consider using the type `u64` instead

but it does fit into u64. Consider changing the number type that you're using, if you know that value can never be negative.

To clarify, that was not the actual number I was attempting to deserialize when that error message was generated. The fetched payload was 9223372036854775807, the precise i64 max. It was just an example of "too large" a number resulting a panic, presumably because the buggy behaviour rounded it upwards outside of the i64 limit.

from seed.

kujeger avatar kujeger commented on June 17, 2024

@kujeger I downloaded your example code and it just works.

Could it be a platform issue? I've reproduced it across multiple machines and browsers (firefox and chromium), but they were all on linux. I don't have any windows or mac machines available for testing.

e: Just in case there's confusion, both buttons "work" in my example, but clicking the different buttons gives a different output below:
"Fetch data" -> Data: 1234567890, 12345678901234568, 1234567890123456800
"Fetch data using serde" -> Data: 1234567890, 12345678901234567, 1234567890123456789

The numbers seem to be rounded up due to precision loss in the "Fetch data" case.

from seed.

kujeger avatar kujeger commented on June 17, 2024

I can reproduce the rounding behaviour with this:

let foo = 1234567890123456789_i64 as f64;
println!("{}", foo);

1234567890123456800

So it looks like fetch[..].json() might be casting the numbers as f64 or something like that.

from seed.

fosskers avatar fosskers commented on June 17, 2024

JS numbers are 53-bit, by the way.

from seed.

flosse avatar flosse commented on June 17, 2024

obsolete since v0.10.0

from seed.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.