mirror of
https://github.com/golang/go.git
synced 2025-11-08 20:51:02 +00:00
json: treat renamed byte slices the same as []byte
Fixes #2163. R=rsc CC=golang-dev https://golang.org/cl/5488068
This commit is contained in:
parent
465aba66c1
commit
34c7765fe5
2 changed files with 32 additions and 6 deletions
|
|
@ -339,13 +339,10 @@ func (e *encodeState) reflectValueQuoted(v reflect.Value, quoted bool) {
|
|||
e.WriteString("null")
|
||||
break
|
||||
}
|
||||
// Slices can be marshalled as nil, but otherwise are handled
|
||||
// as arrays.
|
||||
fallthrough
|
||||
case reflect.Array:
|
||||
if v.Type() == byteSliceType {
|
||||
if v.Type().Elem().Kind() == reflect.Uint8 {
|
||||
// Byte slices get special treatment; arrays don't.
|
||||
s := v.Bytes()
|
||||
e.WriteByte('"')
|
||||
s := v.Interface().([]byte)
|
||||
if len(s) < 1024 {
|
||||
// for small buffers, using Encode directly is much faster.
|
||||
dst := make([]byte, base64.StdEncoding.EncodedLen(len(s)))
|
||||
|
|
@ -361,6 +358,10 @@ func (e *encodeState) reflectValueQuoted(v reflect.Value, quoted bool) {
|
|||
e.WriteByte('"')
|
||||
break
|
||||
}
|
||||
// Slices can be marshalled as nil, but otherwise are handled
|
||||
// as arrays.
|
||||
fallthrough
|
||||
case reflect.Array:
|
||||
e.WriteByte('[')
|
||||
n := v.Len()
|
||||
for i := 0; i < n; i++ {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue