emirpasic / gods

GoDS (Go Data Structures) - Sets, Lists, Stacks, Maps, Trees, Queues, and much more
Other
16.11k stars 1.75k forks source link

linkedhashmap json.Marshal error #250

Open jinsuojinsuo opened 4 months ago

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