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
8a5b76ce
Commit
8a5b76ce
authored
Dec 09, 2009
by
Ross Light
Committed by
Russ Cox
Dec 09, 2009
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
json package: Fixed handling of nil values
Fixes #400. R=golang-dev, rsc
https://golang.org/cl/167058
parent
7d7d95a1
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
15 additions
and
1 deletion
+15
-1
struct.go
src/pkg/json/struct.go
+11
-1
struct_test.go
src/pkg/json/struct_test.go
+4
-0
No files found.
src/pkg/json/struct.go
View file @
8a5b76ce
...
...
@@ -377,6 +377,11 @@ func writeStruct(w io.Writer, val *reflect.StructValue) os.Error {
}
func
writeValue
(
w
io
.
Writer
,
val
reflect
.
Value
)
(
err
os
.
Error
)
{
if
val
==
nil
{
fmt
.
Fprint
(
w
,
"null"
);
return
;
}
switch
v
:=
val
.
(
type
)
{
case
*
reflect
.
StringValue
:
fmt
.
Fprintf
(
w
,
"%q"
,
v
.
Get
())
...
...
@@ -389,10 +394,15 @@ func writeValue(w io.Writer, val reflect.Value) (err os.Error) {
case
*
reflect
.
StructValue
:
err
=
writeStruct
(
w
,
v
)
case
*
reflect
.
ChanValue
,
*
reflect
.
InterfaceValue
,
*
reflect
.
PtrValue
,
*
reflect
.
UnsafePointerValue
:
err
=
&
MarshalError
{
val
.
Type
()}
case
*
reflect
.
InterfaceValue
:
if
v
.
IsNil
()
{
fmt
.
Fprint
(
w
,
"null"
)
}
else
{
err
=
&
MarshalError
{
val
.
Type
()}
}
default
:
value
:=
val
.
(
reflect
.
Value
);
fmt
.
Fprint
(
w
,
value
.
Interface
());
...
...
src/pkg/json/struct_test.go
View file @
8a5b76ce
...
...
@@ -177,6 +177,7 @@ type marshalTest struct {
var
marshalTests
=
[]
marshalTest
{
// basic string
marshalTest
{
nil
,
"null"
},
marshalTest
{
true
,
"true"
},
marshalTest
{
false
,
"false"
},
marshalTest
{
123
,
"123"
},
...
...
@@ -185,11 +186,14 @@ var marshalTests = []marshalTest{
marshalTest
{
"teststring"
,
`"teststring"`
},
marshalTest
{[
4
]
int
{
1
,
2
,
3
,
4
},
"[1,2,3,4]"
},
marshalTest
{[]
int
{
1
,
2
,
3
,
4
},
"[1,2,3,4]"
},
marshalTest
{[]
interface
{}{
nil
},
"[null]"
},
marshalTest
{[][]
int
{[]
int
{
1
,
2
},
[]
int
{
3
,
4
}},
"[[1,2],[3,4]]"
},
marshalTest
{
map
[
string
]
string
{
"one"
:
"one"
},
`{"one":"one"}`
},
marshalTest
{
map
[
string
]
int
{
"one"
:
1
},
`{"one":1}`
},
marshalTest
{
map
[
string
]
interface
{}{
"null"
:
nil
},
`{"null":null}`
},
marshalTest
{
struct
{}{},
"{}"
},
marshalTest
{
struct
{
a
int
}{
1
},
`{"a":1}`
},
marshalTest
{
struct
{
a
interface
{}
}{
nil
},
`{"a":null}`
},
marshalTest
{
struct
{
a
int
;
b
string
;
...
...
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