]> Cypherpunks repositories - gostls13.git/commitdiff
encoding/json: marshal the RawMessage value type the same as its pointer type
authorBrad Fitzpatrick <bradfitz@golang.org>
Sun, 10 Apr 2016 17:16:41 +0000 (10:16 -0700)
committerBrad Fitzpatrick <bradfitz@golang.org>
Wed, 26 Oct 2016 21:03:00 +0000 (21:03 +0000)
Fixes #14493
Updates #6458 (changes its behavior)

Change-Id: I851a8113fd312dae3384e989ec2b70949dc22838
Reviewed-on: https://go-review.googlesource.com/21811
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Quentin Smith <quentin@golang.org>
api/except.txt
src/encoding/json/encode_test.go
src/encoding/json/stream.go

index 4040d1455604e3dd397a6b7fd8ef6ddae6a867a1..93302ec819cb321e0369ceb46aee179d771ac265 100644 (file)
@@ -1,3 +1,4 @@
+pkg encoding/json, method (*RawMessage) MarshalJSON() ([]uint8, error)
 pkg net, func ListenUnixgram(string, *UnixAddr) (*UDPConn, error)
 pkg syscall (darwin-386), func Fchflags(string, int) error
 pkg syscall (darwin-386-cgo), func Fchflags(string, int) error
index 13e765afa31d1ae35956fed02b51ebe3ca01258c..507581feeda3b6bc8c8f47d21d023f87cf332c69 100644 (file)
@@ -440,7 +440,9 @@ func TestIssue6458(t *testing.T) {
                t.Fatal(err)
        }
 
-       if want := `{"M":"ImZvbyI="}`; string(b) != want {
+       // Until Go 1.8, this generated `{"M":"ImZvbyI="}`.
+       // See https://github.com/golang/go/issues/14493#issuecomment-255857318
+       if want := `{"M":"foo"}`; string(b) != want {
                t.Errorf("Marshal(x) = %#q; want %#q", b, want)
        }
 }
@@ -717,3 +719,14 @@ func TestMarshalFloat(t *testing.T) {
        test(0, 32)
        test(math.Copysign(0, -1), 32)
 }
+
+func TestMarshalRawMessageValue(t *testing.T) {
+       const val = "\"some value\""
+       b, err := Marshal(RawMessage(val))
+       if err != nil {
+               t.Fatal(err)
+       }
+       if string(b) != val {
+               t.Errorf("got %q; want %q", b, val)
+       }
+}
index 87f0e57c6cdb5cc810220700b287145d95f858e4..4c350fdd5ea78e1f090c4ade7140f27d4b831465 100644 (file)
@@ -246,9 +246,9 @@ func (enc *Encoder) SetEscapeHTML(on bool) {
 // be used to delay JSON decoding or precompute a JSON encoding.
 type RawMessage []byte
 
-// MarshalJSON returns *m as the JSON encoding of m.
-func (m *RawMessage) MarshalJSON() ([]byte, error) {
-       return *m, nil
+// MarshalJSON returns m as the JSON encoding of m.
+func (m RawMessage) MarshalJSON() ([]byte, error) {
+       return m, nil
 }
 
 // UnmarshalJSON sets *m to a copy of data.