}
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())
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());
var marshalTests = []marshalTest{
// basic string
+ marshalTest{nil, "null"},
marshalTest{true, "true"},
marshalTest{false, "false"},
marshalTest{123, "123"},
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;