I have a function that takes a json decoder and an interface as its arguements, and i am decoding to a struct that is passed on the interface. Like so:
func DecodeJSON(decoder *json.Decoder, i interface{}) bool {
if c, ok := i.(User); ok {
err := decoder.Decode(c)
if err != nil {
fmt.Println(err)
return false //err is not nil
}
}
return false
}
Usage of the function:
// Register test
func HandleRequest(w rest.ResponseWriter, r *rest.Request) {
decoder := json.NewDecoder(r.Body)
user := User{}
if DecodeJSON(decoder, user) {
fmt.Println("success")
}
Error i am getting:
json: Unmarshal(non-pointer main.User)
Bit confused by this message since my DecodeJSON is not taking a pointer for user
. So am not sure what i have done wrong with my code. Hope some one can explain so i can understand my mistake.
You need to use a pointer to a user to decode the data in, otherwise the decoded data gets decoded in a copy of the object that gets deleted when the function returns.
func DecodeJSON(decoder *json.Decoder, i interface{}) bool {
if c, ok := i.(*User); ok {
err := decoder.Decode(c)
if err != nil {
fmt.Println(err)
return false //err is not nil
}
}
return false
}
// Register test
func HandleRequest(w rest.ResponseWriter, r *rest.Request) {
decoder := json.NewDecoder(r.Body)
user := &User{}
if DecodeJSON(decoder, user) {
fmt.Println("success")
}
Leave the interface parameter as is, just use a pointer when you pass the User and when you get the user from the interface.
Based on your code, you'd better change your function signature to func DecodeJSON(decoder *json.Decoder, user *User) bool
This will (1) eliminate explicit runtime cast, and (2) make your code less ambiguous and safe and compile-time checked.