default:
d.saveError(&UnmarshalTypeError{"string", v.Type()})
case reflect.Slice:
- if v.Type() != byteSliceType {
+ if v.Type().Elem().Kind() != reflect.Uint8 {
d.saveError(&UnmarshalTypeError{"string", v.Type()})
break
}
t.Errorf("got error %q, want nil", err)
}
}
+
+// Test that types of byte slices (such as net.IP) both
+// marshal and unmarshal.
+func TestByteSliceType(t *testing.T) {
+ type A []byte
+ type S struct {
+ A A
+ }
+
+ for x, in := range []S{
+ S{},
+ S{A: []byte{'1'}},
+ S{A: []byte{'1', '2', '3', '4', '5'}},
+ } {
+ data, err := Marshal(&in)
+ if err != nil {
+ t.Errorf("#%d: got Marshal error %q, want nil", x, err)
+ continue
+ }
+ var out S
+ err = Unmarshal(data, &out)
+ if err != nil {
+ t.Fatalf("#%d: got Unmarshal error %q, want nil", x, err)
+ }
+ if !reflect.DeepEqual(&out, &in) {
+ t.Fatalf("#%d: got %v, want %v", x, &out, &in)
+ }
+ }
+}
// The angle brackets "<" and ">" are escaped to "\u003c" and "\u003e"
// to keep some browsers from misinterpreting JSON output as HTML.
//
-// Array and slice values encode as JSON arrays, except that
-// []byte encodes as a base64-encoded string, and a nil slice
+// Array and slice values encode as JSON arrays, except that a slice of
+// bytes encodes as a base64-encoded string, and a nil slice
// encodes as the null JSON object.
//
// Struct values encode as JSON objects. Each exported struct field