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

treeset json.Unmarshal error #251

Open
jinsuojinsuo opened this issue May 11, 2024 · 1 comment
Open

treeset json.Unmarshal error #251

jinsuojinsuo opened this issue May 11, 2024 · 1 comment

Comments

@jinsuojinsuo
Copy link

set := treeset.NewWithIntComparator() // empty (keys are of type int)
set.Add(1)                            // 1
set.Add(2, 2, 3, 4, 5)                // 1, 2, 3, 4, 5 (in order, duplicates ignored)

//转Json字符串
marshal, _ := json.Marshal(set)
fmt.Println("json编码", tool.Bytes2String(marshal))

{
	//此处有bug panic: interface conversion: interface {} is float64, not int
	set2 := treeset.NewWithIntComparator()
	err := json.Unmarshal(marshal, set2)
	fmt.Printf("json解码:%v err=%v\n", set2.Values(), err)
}
@jiangzhongqiang
Copy link

set := treeset.NewWithIntComparator() // empty (keys are of type int)
set.Add(1)                            // 1
set.Add(2, 2, 3, 4, 5)                // 1, 2, 3, 4, 5 (in order, duplicates ignored)

//转Json字符串
marshal, _ := json.Marshal(set)
fmt.Println("json编码", tool.Bytes2String(marshal))

{
	//此处有bug panic: interface conversion: interface {} is float64, not int
	set2 := treeset.NewWithIntComparator()
	err := json.Unmarshal(marshal, set2)
	fmt.Printf("json解码:%v err=%v\n", set2.Values(), err)
}

用最新的2.0版本带有泛型的就没问题,旧版本这个反序列话的时候,因为是使用interface来接收数据,数字类型默认解析为float64, 加到集合里面的时候会进行排序比较,比较的时候断言为int的话就会报错

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