]> Cypherpunks repositories - gostls13.git/commitdiff
encoding/json: respect json.Marshaler when encoding byte kind slices
authorHÃ¥vard Haugen <havard.haugen@gmail.com>
Wed, 3 Feb 2016 22:41:55 +0000 (23:41 +0100)
committerBrad Fitzpatrick <bradfitz@golang.org>
Wed, 6 Apr 2016 20:19:15 +0000 (20:19 +0000)
Fixes #13783.

Change-Id: I0122c1f0cf4075acabf5f58241bded1835699dc1
Reviewed-on: https://go-review.googlesource.com/19725
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>

src/encoding/json/encode.go
src/encoding/json/encode_test.go

index bcae6838cc0574daf6ca0b2e88598004eaf68b4d..927f47b179ea804ef671d976234bfdbe691ef38c 100644 (file)
@@ -679,7 +679,9 @@ func (se *sliceEncoder) encode(e *encodeState, v reflect.Value, _ bool) {
 
 func newSliceEncoder(t reflect.Type) encoderFunc {
        // Byte slices get special treatment; arrays don't.
-       if t.Elem().Kind() == reflect.Uint8 {
+       if t.Elem().Kind() == reflect.Uint8 &&
+               !t.Elem().Implements(marshalerType) &&
+               !t.Elem().Implements(textMarshalerType) {
                return encodeByteSlice
        }
        enc := &sliceEncoder{newArrayEncoder(t)}
index eed40a42723ca994acbb167904f9d80656f1aa76..eee59ccb4959074dddb0446999ecd95721905e68 100644 (file)
@@ -6,6 +6,7 @@ package json
 
 import (
        "bytes"
+       "fmt"
        "math"
        "reflect"
        "testing"
@@ -537,6 +538,60 @@ func TestEncodeString(t *testing.T) {
        }
 }
 
+type jsonbyte byte
+
+func (b jsonbyte) MarshalJSON() ([]byte, error) { return tenc(`{"JB":%d}`, b) }
+
+type textbyte byte
+
+func (b textbyte) MarshalText() ([]byte, error) { return tenc(`TB:%d`, b) }
+
+type jsonint int
+
+func (i jsonint) MarshalJSON() ([]byte, error) { return tenc(`{"JI":%d}`, i) }
+
+type textint int
+
+func (i textint) MarshalText() ([]byte, error) { return tenc(`TI:%d`, i) }
+
+func tenc(format string, a ...interface{}) ([]byte, error) {
+       var buf bytes.Buffer
+       fmt.Fprintf(&buf, format, a...)
+       return buf.Bytes(), nil
+}
+
+// Issue 13783
+func TestEncodeBytekind(t *testing.T) {
+       testdata := []struct {
+               data interface{}
+               want string
+       }{
+               {byte(7), "7"},
+               {jsonbyte(7), `{"JB":7}`},
+               {textbyte(4), `"TB:4"`},
+               {jsonint(5), `{"JI":5}`},
+               {textint(1), `"TI:1"`},
+               {[]byte{0, 1}, `"AAE="`},
+               {[]jsonbyte{0, 1}, `[{"JB":0},{"JB":1}]`},
+               {[][]jsonbyte{{0, 1}, {3}}, `[[{"JB":0},{"JB":1}],[{"JB":3}]]`},
+               {[]textbyte{2, 3}, `["TB:2","TB:3"]`},
+               {[]jsonint{5, 4}, `[{"JI":5},{"JI":4}]`},
+               {[]textint{9, 3}, `["TI:9","TI:3"]`},
+               {[]int{9, 3}, `[9,3]`},
+       }
+       for _, d := range testdata {
+               js, err := Marshal(d.data)
+               if err != nil {
+                       t.Error(err)
+                       continue
+               }
+               got, want := string(js), d.want
+               if got != want {
+                       t.Errorf("got %s, want %s", got, want)
+               }
+       }
+}
+
 func TestTextMarshalerMapKeysAreSorted(t *testing.T) {
        b, err := Marshal(map[unmarshalerText]int{
                {"x", "y"}: 1,