gods icon indicating copy to clipboard operation
gods copied to clipboard

treeset json.Unmarshal error

Open jinsuojinsuo opened this issue 9 months ago • 1 comments

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)
}

jinsuojinsuo avatar May 11 '24 03:05 jinsuojinsuo

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的话就会报错

jiangzhongqiang avatar May 17 '24 07:05 jiangzhongqiang