Skip to content
Snippets Groups Projects
Commit 076807d7 authored by Jano Hendriks's avatar Jano Hendriks
Browse files

added a json unmarshaling function

parent 7d54e7f7
Branches
Tags v1.4.4
No related merge requests found
package struct_utils
import (
"encoding/json"
"fmt"
)
// UnmarshalJSON performs a JSON unmarshalling, but on type mismatches it returns a more user friendly error.
// Used exactly the same as json.Unmarshal
func UnmarshalJSON(data []byte, val interface{}) error {
err := json.Unmarshal(data, &val)
if err != nil {
typeErr, ok := err.(*json.UnmarshalTypeError)
if ok {
return fmt.Errorf("invalid type received for field '%s': expected a value of type '%s', but received type '%s'", typeErr.Field, typeErr.Type.Name(), typeErr.Value)
}
}
return err
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment