gods icon indicating copy to clipboard operation
gods copied to clipboard

linkedhashmap json.Marshal error

Open jinsuojinsuo opened this issue 9 months ago • 1 comments

m := linkedhashmap.New[int, string]() // empty (keys are of type int)
m.Put(4, "d")                         // 2->b
m.Put(2, "b")                         // 2->b
m.Put(1, "x")                         // 2->b, 1->x (insertion-order)
m.Put(1, "a")                         // 2->b, 1->a (insertion-order)
{
	//marshalJSON, err2 := m.MarshalJSON()
	//fmt.Println("marshalJSON", string(marshalJSON), err2)

	toJSON, err := m.ToJSON()
	fmt.Println("toJSON", string(toJSON), err)
}
{
	marshal, err := json.Marshal(m)
	fmt.Println("json编码", string(marshal), err)  [linkedhashmap](json: error calling MarshalJSON for type *linkedhashmap.Map[int,string]: invalid character '4' looking for beginning of object key string)
}

jinsuojinsuo avatar May 11 '24 03:05 jinsuojinsuo

m := linkedhashmap.New[int, string]() // empty (keys are of type int)
m.Put(4, "d")                         // 2->b
m.Put(2, "b")                         // 2->b
m.Put(1, "x")                         // 2->b, 1->x (insertion-order)
m.Put(1, "a")                         // 2->b, 1->a (insertion-order)
{
	//marshalJSON, err2 := m.MarshalJSON()
	//fmt.Println("marshalJSON", string(marshalJSON), err2)

	toJSON, err := m.ToJSON()
	fmt.Println("toJSON", string(toJSON), err)
}
{
	marshal, err := json.Marshal(m)
	fmt.Println("json编码", string(marshal), err)  [linkedhashmap](json: error calling MarshalJSON for type *linkedhashmap.Map[int,string]: invalid character '4' looking for beginning of object key string)
}

这里的问题是linkedhashmap的MarshalJSON接口的实现是错误的,不满足JSON格式: 你得修改对应的实现

jiangzhongqiang avatar May 17 '24 06:05 jiangzhongqiang