aboutsummaryrefslogtreecommitdiff
path: root/src/encoding/json/encode.go
diff options
context:
space:
mode:
authorEmmanuel Odeke <emm.odeke@gmail.com>2016-10-24 21:20:04 -0700
committerRuss Cox <rsc@golang.org>2016-11-11 14:50:51 +0000
commitadd721ef91ed533cf578ff7a604124e377329ae4 (patch)
tree25a6ef1a701978890f9ee87a9fdf4cc2578f2a66 /src/encoding/json/encode.go
parentc439a5d8b77a3b87d94bc49714faeeb2a04112f4 (diff)
downloadgo-add721ef91ed533cf578ff7a604124e377329ae4.tar.xz
encoding/json: encode nil Marshaler as "null"
Fixes #16042. Change-Id: I0a28aa004246b7b0ffaaab457e077ad9035363c2 Reviewed-on: https://go-review.googlesource.com/31932 Reviewed-by: Russ Cox <rsc@golang.org> Run-TryBot: Russ Cox <rsc@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org>
Diffstat (limited to 'src/encoding/json/encode.go')
-rw-r--r--src/encoding/json/encode.go6
1 files changed, 5 insertions, 1 deletions
diff --git a/src/encoding/json/encode.go b/src/encoding/json/encode.go
index 667df31ce3..98a9899502 100644
--- a/src/encoding/json/encode.go
+++ b/src/encoding/json/encode.go
@@ -443,7 +443,11 @@ func marshalerEncoder(e *encodeState, v reflect.Value, opts encOpts) {
e.WriteString("null")
return
}
- m := v.Interface().(Marshaler)
+ m, ok := v.Interface().(Marshaler)
+ if !ok {
+ e.WriteString("null")
+ return
+ }
b, err := m.MarshalJSON()
if err == nil {
// copy JSON into buffer, checking validity.