for i := 0; i < val.NumField(); i++ {
fieldValue := val.Field(i)
fmt.Fprintf(w, "%q:", typ.Field(i).Name)
-
if err := writeValue(w, fieldValue); err != nil {
return err
}
-
if i < val.NumField()-1 {
fmt.Fprint(w, ",")
}
case *reflect.StructValue:
err = writeStruct(w, v)
case *reflect.ChanValue,
- *reflect.PtrValue,
*reflect.UnsafePointerValue:
err = &MarshalError{val.Type()}
case *reflect.InterfaceValue:
if v.IsNil() {
fmt.Fprint(w, "null")
} else {
- err = &MarshalError{val.Type()}
+ err = writeValue(w, v.Elem())
+ }
+ case *reflect.PtrValue:
+ if v.IsNil() {
+ fmt.Fprint(w, "null")
+ } else {
+ err = writeValue(w, v.Elem())
}
default:
value := val.(reflect.Value)
out string
}
+type MTE string
+
+type OneField struct {
+ a int
+}
+
var marshalTests = []marshalTest{
// basic string
marshalTest{nil, "null"},
`{"a":1,"b":"hello"}`,
},
marshalTest{map[string][]int{"3": []int{1, 2, 3}}, `{"3":[1,2,3]}`},
+ marshalTest{map[string]*MTE{"hi": nil}, `{"hi":null}`},
+ marshalTest{map[string]interface{}{"hi": 3}, `{"hi":3}`},
+ marshalTest{&OneField{3}, `{"a":3}`},
}
func TestMarshal(t *testing.T) {
error string
}
-type MTE string
+type ChanVal struct {
+ C chan int
+}
var marshalErrorTests = []marshalErrorTest{
marshalErrorTest{map[chan int]string{make(chan int): "one"}, "json cannot encode value of type map[chan int] string"},
- marshalErrorTest{map[string]*MTE{"hi": nil}, "json cannot encode value of type *json.MTE"},
+ marshalErrorTest{make(chan int, 100), "json cannot encode value of type chan int"},
+ marshalErrorTest{new(ChanVal), "json cannot encode value of type chan int"},
}
func TestMarshalError(t *testing.T) {