1
0
mirror of https://github.com/golang/go synced 2024-11-24 04:30:14 -07:00

encoding/json: use reflect.SetBytes when decoding bytes

This allows slices of custom types with byte as underlying type to be
decoded, fixing a regression introduced in CL 9371.

Fixes #12921.

Change-Id: I62a715eaeaaa912b6bc599e94f9981a9ba5cb242
Reviewed-on: https://go-review.googlesource.com/16303
Reviewed-by: Ian Lance Taylor <iant@golang.org>
Run-TryBot: Ian Lance Taylor <iant@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
This commit is contained in:
Håvard Haugen 2015-10-25 22:42:41 +01:00 committed by Ian Lance Taylor
parent 0624fd3f14
commit c60707b14d
2 changed files with 22 additions and 1 deletions

View File

@ -757,7 +757,7 @@ func (d *decodeState) literalStore(item []byte, v reflect.Value, fromQuoted bool
d.saveError(err) d.saveError(err)
break break
} }
v.Set(reflect.ValueOf(b[0:n])) v.SetBytes(b[:n])
case reflect.String: case reflect.String:
v.SetString(string(s)) v.SetString(string(s))
case reflect.Interface: case reflect.Interface:

View File

@ -1253,6 +1253,27 @@ func TestByteKind(t *testing.T) {
} }
} }
// The fix for issue 8962 introduced a regression.
// Issue 12921.
func TestSliceOfCustomByte(t *testing.T) {
type Uint8 uint8
a := []Uint8("hello")
data, err := Marshal(a)
if err != nil {
t.Fatal(err)
}
var b []Uint8
err = Unmarshal(data, &b)
if err != nil {
t.Fatal(err)
}
if !reflect.DeepEqual(a, b) {
t.Fatal("expected %v == %v", a, b)
}
}
var decodeTypeErrorTests = []struct { var decodeTypeErrorTests = []struct {
dest interface{} dest interface{}
src string src string