summaryrefslogtreecommitdiff
path: root/src/pkg/json
diff options
context:
space:
mode:
authorIvan Krasin <krasin@golang.org>2010-01-06 07:25:17 -0800
committerIvan Krasin <krasin@golang.org>2010-01-06 07:25:17 -0800
commitd7f9d03638a68e2632400b75ecfaa71f35a33d88 (patch)
tree377cf41efd1bdb9793c6d2670420420d2d048a86 /src/pkg/json
parent218c405e70449ae29c7293e7e5bbe4343f41e725 (diff)
downloadgolang-d7f9d03638a68e2632400b75ecfaa71f35a33d88.tar.gz
Propagate error to the caller in json.Marshal. Fixes issue 445.
R=rsc, imkrasin CC=golang-dev http://codereview.appspot.com/179125 Committer: Russ Cox <rsc@golang.org>
Diffstat (limited to 'src/pkg/json')
-rw-r--r--src/pkg/json/struct.go11
-rw-r--r--src/pkg/json/struct_test.go16
2 files changed, 21 insertions, 6 deletions
diff --git a/src/pkg/json/struct.go b/src/pkg/json/struct.go
index 7b50301e4..955ec7c87 100644
--- a/src/pkg/json/struct.go
+++ b/src/pkg/json/struct.go
@@ -366,11 +366,9 @@ func writeStruct(w io.Writer, val *reflect.StructValue) os.Error {
for i := 0; i < val.NumField(); i++ {
fieldValue := val.Field(i)
fmt.Fprintf(w, "%q:", typ.Field(i).Name)
-
if err := writeValue(w, fieldValue); err != nil {
return err
}
-
if i < val.NumField()-1 {
fmt.Fprint(w, ",")
}
@@ -398,14 +396,19 @@ func writeValue(w io.Writer, val reflect.Value) (err os.Error) {
case *reflect.StructValue:
err = writeStruct(w, v)
case *reflect.ChanValue,
- *reflect.PtrValue,
*reflect.UnsafePointerValue:
err = &MarshalError{val.Type()}
case *reflect.InterfaceValue:
if v.IsNil() {
fmt.Fprint(w, "null")
} else {
- err = &MarshalError{val.Type()}
+ err = writeValue(w, v.Elem())
+ }
+ case *reflect.PtrValue:
+ if v.IsNil() {
+ fmt.Fprint(w, "null")
+ } else {
+ err = writeValue(w, v.Elem())
}
default:
value := val.(reflect.Value)
diff --git a/src/pkg/json/struct_test.go b/src/pkg/json/struct_test.go
index 9a928f7d0..f1440c413 100644
--- a/src/pkg/json/struct_test.go
+++ b/src/pkg/json/struct_test.go
@@ -175,6 +175,12 @@ type marshalTest struct {
out string
}
+type MTE string
+
+type OneField struct {
+ a int
+}
+
var marshalTests = []marshalTest{
// basic string
marshalTest{nil, "null"},
@@ -201,6 +207,9 @@ var marshalTests = []marshalTest{
`{"a":1,"b":"hello"}`,
},
marshalTest{map[string][]int{"3": []int{1, 2, 3}}, `{"3":[1,2,3]}`},
+ marshalTest{map[string]*MTE{"hi": nil}, `{"hi":null}`},
+ marshalTest{map[string]interface{}{"hi": 3}, `{"hi":3}`},
+ marshalTest{&OneField{3}, `{"a":3}`},
}
func TestMarshal(t *testing.T) {
@@ -224,11 +233,14 @@ type marshalErrorTest struct {
error string
}
-type MTE string
+type ChanVal struct {
+ C chan int
+}
var marshalErrorTests = []marshalErrorTest{
marshalErrorTest{map[chan int]string{make(chan int): "one"}, "json cannot encode value of type map[chan int] string"},
- marshalErrorTest{map[string]*MTE{"hi": nil}, "json cannot encode value of type *json.MTE"},
+ marshalErrorTest{make(chan int, 100), "json cannot encode value of type chan int"},
+ marshalErrorTest{new(ChanVal), "json cannot encode value of type chan int"},
}
func TestMarshalError(t *testing.T) {