diff options
author | Ross Light <rlight2@gmail.com> | 2009-12-09 10:36:03 -0800 |
---|---|---|
committer | Russ Cox <rsc@golang.org> | 2009-12-09 10:36:03 -0800 |
commit | 8a5b76ce5ca65a4606e8c141c1f18a999d484a42 (patch) | |
tree | 8fef67fa80df51ab3978841050ffecfdd1397f79 | |
parent | 7d7d95a160160cf77217886c54d06236532b07ea (diff) | |
download | go-8a5b76ce5ca65a4606e8c141c1f18a999d484a42.tar.gz go-8a5b76ce5ca65a4606e8c141c1f18a999d484a42.zip |
json package: Fixed handling of nil values
Fixes #400.
R=golang-dev, rsc
https://golang.org/cl/167058
-rw-r--r-- | src/pkg/json/struct.go | 12 | ||||
-rw-r--r-- | src/pkg/json/struct_test.go | 4 |
2 files changed, 15 insertions, 1 deletions
diff --git a/src/pkg/json/struct.go b/src/pkg/json/struct.go index 8d86631939..d34939cbd2 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()); diff --git a/src/pkg/json/struct_test.go b/src/pkg/json/struct_test.go index c01f4ddeb9..0c7976eba0 100644 --- a/src/pkg/json/struct_test.go +++ b/src/pkg/json/struct_test.go @@ -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; |