aboutsummaryrefslogtreecommitdiff
path: root/src/encoding/json/encode.go
diff options
context:
space:
mode:
authorWilliam Poussier <william.poussier@gmail.com>2019-09-11 15:33:25 +0000
committerDaniel Martí <mvdan@mvdan.cc>2019-09-11 18:37:43 +0000
commitcc39d8087b20fce14e60b7e5f287593da2c72749 (patch)
treedea3d59084ccb90a1f5c7d4fc581631d1c4636d1 /src/encoding/json/encode.go
parent85c60bd0f3b375448dd38579acbaafdddc20b42f (diff)
downloadgo-cc39d8087b20fce14e60b7e5f287593da2c72749.tar.xz
encoding/json: encode nil encoding.TextMarshaler instance as "null"
Fixes #34235. Change-Id: Ia3795fd18860530fa6a4b171545f525e784ffdcb GitHub-Last-Rev: 1a319c452857818f7aaf22ef46823b43ca9b2276 GitHub-Pull-Request: golang/go#34238 Reviewed-on: https://go-review.googlesource.com/c/go/+/194642 Reviewed-by: Daniel Martí <mvdan@mvdan.cc> Run-TryBot: Daniel Martí <mvdan@mvdan.cc> 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 0758b2fc9e..e5dd1b7799 100644
--- a/src/encoding/json/encode.go
+++ b/src/encoding/json/encode.go
@@ -481,7 +481,11 @@ func textMarshalerEncoder(e *encodeState, v reflect.Value, opts encOpts) {
e.WriteString("null")
return
}
- m := v.Interface().(encoding.TextMarshaler)
+ m, ok := v.Interface().(encoding.TextMarshaler)
+ if !ok {
+ e.WriteString("null")
+ return
+ }
b, err := m.MarshalText()
if err != nil {
e.error(&MarshalerError{v.Type(), err})