Skip to content

Commit 1625da2

Browse files
committed
encoding/json: marshal the RawMessage value type the same as its pointer type
Fixes #14493 Updates #6458 (changes its behavior) Change-Id: I851a8113fd312dae3384e989ec2b70949dc22838 Reviewed-on: https://go-review.googlesource.com/21811 Run-TryBot: Brad Fitzpatrick <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Quentin Smith <[email protected]>
1 parent 587b803 commit 1625da2

File tree

3 files changed

+18
-4
lines changed

3 files changed

+18
-4
lines changed

api/except.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
pkg encoding/json, method (*RawMessage) MarshalJSON() ([]uint8, error)
12
pkg net, func ListenUnixgram(string, *UnixAddr) (*UDPConn, error)
23
pkg syscall (darwin-386), func Fchflags(string, int) error
34
pkg syscall (darwin-386-cgo), func Fchflags(string, int) error

src/encoding/json/encode_test.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -440,7 +440,9 @@ func TestIssue6458(t *testing.T) {
440440
t.Fatal(err)
441441
}
442442

443-
if want := `{"M":"ImZvbyI="}`; string(b) != want {
443+
// Until Go 1.8, this generated `{"M":"ImZvbyI="}`.
444+
// See https://github.com/golang/go/issues/14493#issuecomment-255857318
445+
if want := `{"M":"foo"}`; string(b) != want {
444446
t.Errorf("Marshal(x) = %#q; want %#q", b, want)
445447
}
446448
}
@@ -717,3 +719,14 @@ func TestMarshalFloat(t *testing.T) {
717719
test(0, 32)
718720
test(math.Copysign(0, -1), 32)
719721
}
722+
723+
func TestMarshalRawMessageValue(t *testing.T) {
724+
const val = "\"some value\""
725+
b, err := Marshal(RawMessage(val))
726+
if err != nil {
727+
t.Fatal(err)
728+
}
729+
if string(b) != val {
730+
t.Errorf("got %q; want %q", b, val)
731+
}
732+
}

src/encoding/json/stream.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -246,9 +246,9 @@ func (enc *Encoder) SetEscapeHTML(on bool) {
246246
// be used to delay JSON decoding or precompute a JSON encoding.
247247
type RawMessage []byte
248248

249-
// MarshalJSON returns *m as the JSON encoding of m.
250-
func (m *RawMessage) MarshalJSON() ([]byte, error) {
251-
return *m, nil
249+
// MarshalJSON returns m as the JSON encoding of m.
250+
func (m RawMessage) MarshalJSON() ([]byte, error) {
251+
return m, nil
252252
}
253253

254254
// UnmarshalJSON sets *m to a copy of data.

0 commit comments

Comments
 (0)