You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
type Test struct{
A string
B string
}
func main(){
a := Test{"12","23"}
b := reflect.New(reflect.TypeOf(a)).Elem().Interface()
c,err := json.Marshal(a)
if err != nil {
log.Fatal(err)
}
json.Unmarshal(c,&b)
fmt.Printf("%#v", b)
}
result :
map[string]interface {}{"A":"12", "B":"23"}
So we can see json.Unmarshal will change the type of 'b' from Test to map[string]interface{}
Reason :
encoding/json/decode.go function indirect (line 439)
if v.Kind() == reflect.Interface && !v.IsNil() {
e := v.Elem()
if e.Kind() == reflect.Ptr && !e.IsNil() && (!decodingNull || e.Elem().Kind() == reflect.Ptr) {
v = e
continue
}
//maybe you can check the e.Kind() here
}
Thanks
The text was updated successfully, but these errors were encountered:
This is happening because the type of b is interface{}, so the type of &b, which you pass to json.Unmarshal, is type *interface{}. Unmarshal isn't changing the type, it's using the type you give it.
Closing because I don't think there is anything to fix here. Please comment if you disagree.
go version
go1.8.3 linux/amd64
go env
GOARCH="amd64"
GOBIN=""
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH="/home/xing/go"
GORACE=""
GOROOT="/usr/local/go"
GOTOOLDIR="/usr/local/go/pkg/tool/linux_amd64"
GCCGO="gccgo"
CC="gcc"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build248645349=/tmp/go-build -gno-record-gcc-switches"
CXX="g++"
CGO_ENABLED="1"
PKG_CONFIG="pkg-config"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
So we can see json.Unmarshal will change the type of 'b' from Test to map[string]interface{}
Reason :
encoding/json/decode.go function indirect (line 439)
Thanks
The text was updated successfully, but these errors were encountered: