Commit 8a5b76ce authored by Ross Light's avatar Ross Light Committed by Russ Cox

json package: Fixed handling of nil values

Fixes #400.

R=golang-dev, rsc
https://golang.org/cl/167058
parent 7d7d95a1
...@@ -377,6 +377,11 @@ func writeStruct(w io.Writer, val *reflect.StructValue) os.Error { ...@@ -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) { func writeValue(w io.Writer, val reflect.Value) (err os.Error) {
if val == nil {
fmt.Fprint(w, "null");
return;
}
switch v := val.(type) { switch v := val.(type) {
case *reflect.StringValue: case *reflect.StringValue:
fmt.Fprintf(w, "%q", v.Get()) fmt.Fprintf(w, "%q", v.Get())
...@@ -389,10 +394,15 @@ func writeValue(w io.Writer, val reflect.Value) (err os.Error) { ...@@ -389,10 +394,15 @@ func writeValue(w io.Writer, val reflect.Value) (err os.Error) {
case *reflect.StructValue: case *reflect.StructValue:
err = writeStruct(w, v) err = writeStruct(w, v)
case *reflect.ChanValue, case *reflect.ChanValue,
*reflect.InterfaceValue,
*reflect.PtrValue, *reflect.PtrValue,
*reflect.UnsafePointerValue: *reflect.UnsafePointerValue:
err = &MarshalError{val.Type()} err = &MarshalError{val.Type()}
case *reflect.InterfaceValue:
if v.IsNil() {
fmt.Fprint(w, "null")
} else {
err = &MarshalError{val.Type()}
}
default: default:
value := val.(reflect.Value); value := val.(reflect.Value);
fmt.Fprint(w, value.Interface()); fmt.Fprint(w, value.Interface());
......
...@@ -177,6 +177,7 @@ type marshalTest struct { ...@@ -177,6 +177,7 @@ type marshalTest struct {
var marshalTests = []marshalTest{ var marshalTests = []marshalTest{
// basic string // basic string
marshalTest{nil, "null"},
marshalTest{true, "true"}, marshalTest{true, "true"},
marshalTest{false, "false"}, marshalTest{false, "false"},
marshalTest{123, "123"}, marshalTest{123, "123"},
...@@ -185,11 +186,14 @@ var marshalTests = []marshalTest{ ...@@ -185,11 +186,14 @@ var marshalTests = []marshalTest{
marshalTest{"teststring", `"teststring"`}, marshalTest{"teststring", `"teststring"`},
marshalTest{[4]int{1, 2, 3, 4}, "[1,2,3,4]"}, marshalTest{[4]int{1, 2, 3, 4}, "[1,2,3,4]"},
marshalTest{[]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{[][]int{[]int{1, 2}, []int{3, 4}}, "[[1,2],[3,4]]"},
marshalTest{map[string]string{"one": "one"}, `{"one":"one"}`}, marshalTest{map[string]string{"one": "one"}, `{"one":"one"}`},
marshalTest{map[string]int{"one": 1}, `{"one":1}`}, marshalTest{map[string]int{"one": 1}, `{"one":1}`},
marshalTest{map[string]interface{}{"null": nil}, `{"null":null}`},
marshalTest{struct{}{}, "{}"}, marshalTest{struct{}{}, "{}"},
marshalTest{struct{ a int }{1}, `{"a":1}`}, marshalTest{struct{ a int }{1}, `{"a":1}`},
marshalTest{struct{ a interface{} }{nil}, `{"a":null}`},
marshalTest{struct { marshalTest{struct {
a int; a int;
b string; b string;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment