Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
G
golang
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Open sidebar
go
golang
Commits
4e5bc6a8
Commit
4e5bc6a8
authored
May 11, 2010
by
Russ Cox
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
json: fix array -> non-array decoding
Fixes #773. R=adg CC=golang-dev
https://golang.org/cl/1120042
parent
916426ea
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
30 additions
and
19 deletions
+30
-19
decode.go
src/pkg/json/decode.go
+3
-0
decode_test.go
src/pkg/json/decode_test.go
+27
-19
No files found.
src/pkg/json/decode.go
View file @
4e5bc6a8
...
...
@@ -304,6 +304,9 @@ func (d *decodeState) array(v reflect.Value) {
av
,
ok
:=
v
.
(
reflect
.
ArrayOrSliceValue
)
if
!
ok
{
d
.
saveError
(
&
UnmarshalTypeError
{
"array"
,
v
.
Type
()})
d
.
off
--
d
.
next
()
return
}
sv
,
_
:=
v
.
(
*
reflect
.
SliceValue
)
...
...
src/pkg/json/decode_test.go
View file @
4e5bc6a8
...
...
@@ -6,38 +6,46 @@ package json
import
(
"bytes"
"os"
"reflect"
"strings"
"testing"
)
type
T
struct
{
X
string
Y
int
}
type
unmarshalTest
struct
{
in
string
ptr
interface
{}
out
interface
{}
err
os
.
Error
}
var
unmarshalTests
=
[]
unmarshalTest
{
// basic types
unmarshalTest
{
`true`
,
new
(
bool
),
true
},
unmarshalTest
{
`1`
,
new
(
int
),
1
},
unmarshalTest
{
`1.2`
,
new
(
float
),
1.2
},
unmarshalTest
{
`-5`
,
new
(
int16
),
int16
(
-
5
)},
unmarshalTest
{
`"a\u1234"`
,
new
(
string
),
"a
\u1234
"
},
unmarshalTest
{
`"http:\/\/"`
,
new
(
string
),
"http://"
},
unmarshalTest
{
`"g-clef: \uD834\uDD1E"`
,
new
(
string
),
"g-clef:
\U0001D11E
"
},
unmarshalTest
{
`"invalid: \uD834x\uDD1E"`
,
new
(
string
),
"invalid:
\uFFFD
x
\uFFFD
"
},
unmarshalTest
{
"null"
,
new
(
interface
{}),
nil
},
unmarshalTest
{
`true`
,
new
(
bool
),
true
,
nil
},
unmarshalTest
{
`1`
,
new
(
int
),
1
,
nil
},
unmarshalTest
{
`1.2`
,
new
(
float
),
1.2
,
nil
},
unmarshalTest
{
`-5`
,
new
(
int16
),
int16
(
-
5
),
nil
},
unmarshalTest
{
`"a\u1234"`
,
new
(
string
),
"a
\u1234
"
,
nil
},
unmarshalTest
{
`"http:\/\/"`
,
new
(
string
),
"http://"
,
nil
},
unmarshalTest
{
`"g-clef: \uD834\uDD1E"`
,
new
(
string
),
"g-clef:
\U0001D11E
"
,
nil
},
unmarshalTest
{
`"invalid: \uD834x\uDD1E"`
,
new
(
string
),
"invalid:
\uFFFD
x
\uFFFD
"
,
nil
},
unmarshalTest
{
"null"
,
new
(
interface
{}),
nil
,
nil
},
unmarshalTest
{
`{"X": [1,2,3], "Y": 4}`
,
new
(
T
),
T
{
Y
:
4
},
&
UnmarshalTypeError
{
"array"
,
reflect
.
Typeof
(
""
)}},
// composite tests
unmarshalTest
{
allValueIndent
,
new
(
All
),
allValue
},
unmarshalTest
{
allValueCompact
,
new
(
All
),
allValue
},
unmarshalTest
{
allValueIndent
,
new
(
*
All
),
&
allValue
},
unmarshalTest
{
allValueCompact
,
new
(
*
All
),
&
allValue
},
unmarshalTest
{
pallValueIndent
,
new
(
All
),
pallValue
},
unmarshalTest
{
pallValueCompact
,
new
(
All
),
pallValue
},
unmarshalTest
{
pallValueIndent
,
new
(
*
All
),
&
pallValue
},
unmarshalTest
{
pallValueCompact
,
new
(
*
All
),
&
pallValue
},
unmarshalTest
{
allValueIndent
,
new
(
All
),
allValue
,
nil
},
unmarshalTest
{
allValueCompact
,
new
(
All
),
allValue
,
nil
},
unmarshalTest
{
allValueIndent
,
new
(
*
All
),
&
allValue
,
nil
},
unmarshalTest
{
allValueCompact
,
new
(
*
All
),
&
allValue
,
nil
},
unmarshalTest
{
pallValueIndent
,
new
(
All
),
pallValue
,
nil
},
unmarshalTest
{
pallValueCompact
,
new
(
All
),
pallValue
,
nil
},
unmarshalTest
{
pallValueIndent
,
new
(
*
All
),
&
pallValue
,
nil
},
unmarshalTest
{
pallValueCompact
,
new
(
*
All
),
&
pallValue
,
nil
},
}
func
TestMarshal
(
t
*
testing
.
T
)
{
...
...
@@ -73,8 +81,8 @@ func TestUnmarshal(t *testing.T) {
// v = new(right-type)
v
:=
reflect
.
NewValue
(
tt
.
ptr
)
.
(
*
reflect
.
PtrValue
)
v
.
PointTo
(
reflect
.
MakeZero
(
v
.
Type
()
.
(
*
reflect
.
PtrType
)
.
Elem
()))
if
err
:=
Unmarshal
([]
byte
(
in
),
v
.
Interface
());
err
!=
nil
{
t
.
Errorf
(
"#%d: %v
"
,
i
,
err
)
if
err
:=
Unmarshal
([]
byte
(
in
),
v
.
Interface
());
!
reflect
.
DeepEqual
(
err
,
tt
.
err
)
{
t
.
Errorf
(
"#%d: %v
want %v"
,
i
,
err
,
tt
.
err
)
continue
}
if
!
reflect
.
DeepEqual
(
v
.
Elem
()
.
Interface
(),
tt
.
out
)
{
...
...
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