summaryrefslogtreecommitdiff
path: root/src/pkg/json/struct.go
diff options
context:
space:
mode:
authorRoss Light <rlight2@gmail.com>2009-12-09 10:36:03 -0800
committerRoss Light <rlight2@gmail.com>2009-12-09 10:36:03 -0800
commit0b385d6c286fbd2aa9ea131a42281a82e08a1331 (patch)
tree27c1e6d09bd1f5fdf63e5ecdb8dc6d2502fd66d0 /src/pkg/json/struct.go
parent0df82cfcfd30fe4aab963ae1eb926e494807253e (diff)
downloadgolang-0b385d6c286fbd2aa9ea131a42281a82e08a1331.tar.gz
json package: Fixed handling of nil values
Fixes issue 400. R=golang-dev, rsc http://codereview.appspot.com/167058 Committer: Russ Cox <rsc@golang.org>
Diffstat (limited to 'src/pkg/json/struct.go')
-rw-r--r--src/pkg/json/struct.go12
1 files changed, 11 insertions, 1 deletions
diff --git a/src/pkg/json/struct.go b/src/pkg/json/struct.go
index 8d8663193..d34939cbd 100644
--- a/src/pkg/json/struct.go
+++ b/src/pkg/json/struct.go
@@ -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());