gods
gods copied to clipboard
treeset json.Unmarshal error
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)
}
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的话就会报错