go-ipld-cbor icon indicating copy to clipboard operation
go-ipld-cbor copied to clipboard

integer number in json but dump into cbor the number is float

Open atenjin opened this issue 5 years ago • 1 comments

testcase in the code: https://github.com/ipfs/go-ipld-cbor/blob/e249008b68731703a093b723253be5175d518ed0/node_test.go#L337

func TestResolvedValIsJsonable(t *testing.T) {
        // we can see that the value in map "foo" for key "bar" and "baz" is **interger**
	data := `{
		"foo": {
			"bar": 1,
			"baz": 2
		}
	}`
	n, err := FromJSON(strings.NewReader(data), mh.SHA2_256, -1)
	if err != nil {
		t.Fatal(err)
	}
        // ...
}

but in function FromJSON

func FromJSON(r io.Reader, mhType uint64, mhLen int) (*Node, error) {
	var m interface{}
        // receive the preview str, it would result be like
        // map { "bar" => 1 (float64) ; "baz" => 2 (float64) }
	err := json.NewDecoder(r).Decode(&m)  
	if err != nil {
		return nil, err
	}
        // and then, cbor would use float64 to serialize, but in fact, this value is an integer
	obj, err := convertToCborIshObj(m)
	if err != nil {
		return nil, err
	}

	return WrapObject(obj, mhType, mhLen)
}

I think this is a wrong case. Please check it, thanks.

atenjin avatar Dec 17 '19 04:12 atenjin

The wikipedia says that The format makes no distinction between integer and floating-point (https://en.wikipedia.org/wiki/JSON#Data_types_and_syntax). The Go decoder chooses to default to float64. So I think you can say that even though those look like integers, they are actually floating point numbers.

Does this represent an issue for you?

hsanjuan avatar Mar 06 '20 17:03 hsanjuan