Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
go
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
go
Commits
34c7765f
Commit
34c7765f
authored
Dec 14, 2011
by
Rob Pike
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
json: treat renamed byte slices the same as []byte
Fixes #2163. R=rsc CC=golang-dev
https://golang.org/cl/5488068
parent
465aba66
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
32 additions
and
6 deletions
+32
-6
src/pkg/encoding/json/encode.go
src/pkg/encoding/json/encode.go
+7
-6
src/pkg/encoding/json/encode_test.go
src/pkg/encoding/json/encode_test.go
+25
-0
No files found.
src/pkg/encoding/json/encode.go
View file @
34c7765f
...
...
@@ -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
++
{
...
...
src/pkg/encoding/json/encode_test.go
View file @
34c7765f
...
...
@@ -82,3 +82,28 @@ func TestStringTag(t *testing.T) {
t
.
Fatalf
(
"decode didn't match.
\n
source: %#v
\n
Encoded as:
\n
%s
\n
decode: %#v"
,
s
,
string
(
got
),
s2
)
}
}
// byte slices are special even if they're renamed types.
type
renamedByte
byte
type
renamedByteSlice
[]
byte
type
renamedRenamedByteSlice
[]
renamedByte
func
TestEncodeRenamedByteSlice
(
t
*
testing
.
T
)
{
s
:=
renamedByteSlice
(
"abc"
)
result
,
err
:=
Marshal
(
s
)
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
expect
:=
`"YWJj"`
if
string
(
result
)
!=
expect
{
t
.
Errorf
(
" got %s want %s"
,
result
,
expect
)
}
r
:=
renamedRenamedByteSlice
(
"abc"
)
result
,
err
=
Marshal
(
r
)
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
if
string
(
result
)
!=
expect
{
t
.
Errorf
(
" got %s want %s"
,
result
,
expect
)
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment