From 032d150bd4a812eee9c3ea017f0d3acc8c484325 Mon Sep 17 00:00:00 2001 From: Joe Tsai Date: Mon, 31 Oct 2016 17:34:42 -0700 Subject: encoding/json: marshal with null when RawMessage is nil This CL expands upon a change made in (http://golang.org/cl/21811) to ensure that a nil RawMessage gets serialized as "null" instead of being a nil slice. The added check only triggers when the RawMessage is nil. We do not handle the case when the RawMessage is non-nil, but empty. Fixes #17704 Updates #14493 Change-Id: I0fbebcdd81f7466c5b78c94953afc897f162ceb4 Reviewed-on: https://go-review.googlesource.com/32472 Reviewed-by: Brad Fitzpatrick Run-TryBot: Brad Fitzpatrick TryBot-Result: Gobot Gobot --- src/encoding/json/stream.go | 3 +++ 1 file changed, 3 insertions(+) (limited to 'src/encoding/json/stream.go') diff --git a/src/encoding/json/stream.go b/src/encoding/json/stream.go index 4c350fdd5e..95e30ce36d 100644 --- a/src/encoding/json/stream.go +++ b/src/encoding/json/stream.go @@ -248,6 +248,9 @@ type RawMessage []byte // MarshalJSON returns m as the JSON encoding of m. func (m RawMessage) MarshalJSON() ([]byte, error) { + if m == nil { + return []byte("null"), nil + } return m, nil } -- cgit v1.3-5-g9baa