1
0
mirror of https://github.com/golang/go synced 2024-11-23 15:10:12 -07:00

encoding/json: get rid of the stream_test.go TODOs

TestRawMessage now passes without the need for the RawMessage field to
be a pointer. The TODO dates all the way back to 2010, so I presume the
issue has since been fixed.

TestNullRawMessage tested the decoding of a JSON null into a
*RawMessage. The existing behavior was correct, but for the sake of
completeness a non-pointer RawMessage field has been added too. The
non-pointer field behaves differently, as one can read in the docs:

	To unmarshal JSON into a value implementing the Unmarshaler
	interface, Unmarshal calls that value's UnmarshalJSON method,
	including when the input is a JSON null.

Change-Id: Iabaed75d4ed10ea427d135ee1b80c6e6b83b2e6e
Reviewed-on: https://go-review.googlesource.com/131377
Run-TryBot: Daniel Martí <mvdan@mvdan.cc>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
This commit is contained in:
Daniel Martí 2018-08-26 08:35:24 -06:00
parent eae5fc88c1
commit 21af0c1699

View File

@ -201,10 +201,9 @@ func nlines(s string, n int) string {
} }
func TestRawMessage(t *testing.T) { func TestRawMessage(t *testing.T) {
// TODO(rsc): Should not need the * in *RawMessage
var data struct { var data struct {
X float64 X float64
Id *RawMessage Id RawMessage
Y float32 Y float32
} }
const raw = `["\u0056",null]` const raw = `["\u0056",null]`
@ -213,8 +212,8 @@ func TestRawMessage(t *testing.T) {
if err != nil { if err != nil {
t.Fatalf("Unmarshal: %v", err) t.Fatalf("Unmarshal: %v", err)
} }
if string([]byte(*data.Id)) != raw { if string([]byte(data.Id)) != raw {
t.Fatalf("Raw mismatch: have %#q want %#q", []byte(*data.Id), raw) t.Fatalf("Raw mismatch: have %#q want %#q", []byte(data.Id), raw)
} }
b, err := Marshal(&data) b, err := Marshal(&data)
if err != nil { if err != nil {
@ -226,20 +225,22 @@ func TestRawMessage(t *testing.T) {
} }
func TestNullRawMessage(t *testing.T) { func TestNullRawMessage(t *testing.T) {
// TODO(rsc): Should not need the * in *RawMessage
var data struct { var data struct {
X float64 X float64
Id *RawMessage Id RawMessage
Y float32 IdPtr *RawMessage
Y float32
} }
data.Id = new(RawMessage) const msg = `{"X":0.1,"Id":null,"IdPtr":null,"Y":0.2}`
const msg = `{"X":0.1,"Id":null,"Y":0.2}`
err := Unmarshal([]byte(msg), &data) err := Unmarshal([]byte(msg), &data)
if err != nil { if err != nil {
t.Fatalf("Unmarshal: %v", err) t.Fatalf("Unmarshal: %v", err)
} }
if data.Id != nil { if want, got := "null", string(data.Id); want != got {
t.Fatalf("Raw mismatch: have non-nil, want nil") t.Fatalf("Raw mismatch: have %q, want %q", got, want)
}
if data.IdPtr != nil {
t.Fatalf("Raw pointer mismatch: have non-nil, want nil")
} }
b, err := Marshal(&data) b, err := Marshal(&data)
if err != nil { if err != nil {