You've passed to json
a pointer to an abstract interface. You should simply pass a pointer to Ping
as an abstract interface:
func bad() {
var ping interface{} = &Ping{} // <<<< this
deserialize([]byte(`{"id":42}`), ping) // << and this
fmt.Println("DONE:", ping) // It's a simple map now, not a Ping. Why?
}
But if as you said you don't have a pointer to cast ton an interface{}
, you can use reflect to create a new pointer, deserialize into it, and copy the value back:
func bad() {
var ping interface{} = Ping{}
nptr := reflect.New(reflect.TypeOf(ping))
deserialize([]byte(`{"id":42}`), nptr.Interface())
ping = nptr.Interface()
fmt.Println("DONE:", ping) // It's a simple map now, not a Ping. Why?
}