encoding/json: improve fidelity of TestUnmarshal for Numbers

In particular, cover the behavior of unmarshaling a JSON string
into a Number type regardless of whether the `string` option
is specified or not.

Change-Id: Ibc55f16860442240bcfeea1fd51aaa76f7e50f67
Reviewed-on: https://go-review.googlesource.com/c/go/+/641416
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Auto-Submit: Joseph Tsai <joetsai@digital-static.net>
Reviewed-by: Damien Neil <dneil@google.com>
Reviewed-by: Dmitri Shuralyov <dmitshur@google.com>
This commit is contained in:
Joe Tsai 2025-01-08 12:30:01 -08:00 committed by Joseph Tsai
parent c87a6f932e
commit 4225c6cb37

View file

@ -1068,6 +1068,49 @@ var unmarshalTests = []struct {
ptr: new(map[string]Number), ptr: new(map[string]Number),
err: fmt.Errorf("json: invalid number literal, trying to unmarshal %q into Number", `"invalid"`), err: fmt.Errorf("json: invalid number literal, trying to unmarshal %q into Number", `"invalid"`),
}, },
{
CaseName: Name(""),
in: `5`,
ptr: new(Number),
out: Number("5"),
},
{
CaseName: Name(""),
in: `"5"`,
ptr: new(Number),
out: Number("5"),
},
{
CaseName: Name(""),
in: `{"N":5}`,
ptr: new(struct{ N Number }),
out: struct{ N Number }{"5"},
},
{
CaseName: Name(""),
in: `{"N":"5"}`,
ptr: new(struct{ N Number }),
out: struct{ N Number }{"5"},
},
{
CaseName: Name(""),
in: `{"N":5}`,
ptr: new(struct {
N Number `json:",string"`
}),
err: fmt.Errorf("json: invalid use of ,string struct tag, trying to unmarshal unquoted value into json.Number"),
},
{
CaseName: Name(""),
in: `{"N":"5"}`,
ptr: new(struct {
N Number `json:",string"`
}),
out: struct {
N Number `json:",string"`
}{"5"},
},
} }
func TestMarshal(t *testing.T) { func TestMarshal(t *testing.T) {