package test_json import ( "json" "os" "reflect" "testing" ) func isNil(x interface{}) bool { if x == nil { return true } switch value := reflect.ValueOf(x); value.Kind() { case reflect.Chan, reflect.Func, reflect.Interface, reflect.Map, reflect.Ptr, reflect.Slice: return value.IsNil() } return false } func TestJsonRoundtrip(t *testing.T) { type MyType struct { Buf []byte Ptr *int Map map[string]string } var testMyType MyType = MyType{nil, nil, nil} var myType MyType var bytes []byte var err os.Error if bytes, err = json.Marshal(&testMyType); err != nil { t.Fatal("Error ", err, " marshalling type ", testMyType) } t.Log("Marshaled type ", &testMyType, " to ", string(bytes)) if err = json.Unmarshal(bytes, &myType); err != nil { t.Fatal("Error ", err, " unmarshalling bytes ", string(bytes)) } if !isNil(myType.Buf) { t.Error("Buf not nil: ",myType.Buf) } if !isNil(myType.Ptr) { t.Error("Ptr not nil: ",myType.Ptr) } if !isNil(myType.Map) { t.Error("Map not nil: ", myType.Map) } }