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

linkedhashmap json.Marshal error #250

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

linkedhashmap json.Marshal error #250

jinsuojinsuo opened this issue May 11, 2024 · 1 comment

Comments

@jinsuojinsuo
Copy link

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)
}
@jiangzhongqiang
Copy link

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格式: 你得修改对应的实现

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