ipld / ipld/libipld

DAG-JSON: decodes large negative integers as floats

Open
#187 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
Rust
Stars
150
Forks
41
PR merge metrics
No merged PRs in 30d

Description

Current DAG-JSON cannot decode large negative integers as floats. If they are outside the i64 range, they get automatically converted into a float. This means that decoding a number like -11959030306112471732 becomes `Float(-1.1959030306112471e19)` instead of the expected `Integer(-11959030306112471732)`.

Here's an example test case:

```rust
#[test]
fn decode_large_negative_integer() {
let integer: i128 = -11959030306112471732;
let decoded: Ipld = DagJsonCodec.decode(integer.to_string().as_bytes()).unwrap();
assert_eq!(decoded, Ipld::Integer(integer));
}
```

One way is to indeed decode it as integer, another way (which is likely easier) is to have an error if it's outside the i64 range. I think such an error would be better then implicit conversion.

Though I should add that if someone spends time on the current DAG-JSON implementation, that it might be better spend on a new DAG-JSON implementation similar to https://github.com/ipld/serde_ipld_dagcbor.

This means that I'm not sure if this issue ever gets fixed in the current implementation. I just wanted to make sure this issue is tracked somewhere in case someone encounters it.

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.