]> Cypherpunks repositories - gostls13.git/commitdiff
encoding/json: use reflect.SetBytes when decoding bytes
authorHÃ¥vard Haugen <havard.haugen@gmail.com>
Sun, 25 Oct 2015 21:42:41 +0000 (22:42 +0100)
committerIan Lance Taylor <iant@golang.org>
Sat, 14 Nov 2015 23:41:46 +0000 (23:41 +0000)
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>

src/encoding/json/decode.go
src/encoding/json/decode_test.go

index e7e8d0b9979f41766f8aedabeb77cddc8ec916d2..bd939b4258194d423b279cae29486de99e2f60c2 100644 (file)
@@ -757,7 +757,7 @@ func (d *decodeState) literalStore(item []byte, v reflect.Value, fromQuoted bool
                                d.saveError(err)
                                break
                        }
-                       v.Set(reflect.ValueOf(b[0:n]))
+                       v.SetBytes(b[:n])
                case reflect.String:
                        v.SetString(string(s))
                case reflect.Interface:
index 0ed3b51628fba255745fdbbe3bf7905a75706feb..9546ae459cd1581edd7a87e112edb7e937fe3a46 100644 (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 {
        dest interface{}
        src  string