Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
atenjin opened this issue Dec 17, 2019 · 1 comment
Open

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

atenjin opened this issue Dec 17, 2019 · 1 comment

Comments

@atenjin
Copy link

atenjin commented Dec 17, 2019

testcase in the code:

func TestResolvedValIsJsonable(t *testing.T) {

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.

@hsanjuan
Copy link
Contributor

hsanjuan commented Mar 6, 2020

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants