libipld icon indicating copy to clipboard operation
libipld copied to clipboard

DAG-JSON: decodes large negative integers as floats

Open vmx opened this issue 2 years ago • 0 comments

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:

#[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.

vmx avatar Sep 07 '23 09:09 vmx