Skip to content

Commit 56d3e84

Browse files
ianlancetaylorgopherbot
authored andcommitted
encoding/json: use reflect.TypeFor for known types
For #60088 Change-Id: I2e471c76de62944b14472966b63f5778124b9b8b Reviewed-on: https://go-review.googlesource.com/c/go/+/514655 TryBot-Result: Gopher Robot <[email protected]> Run-TryBot: Ian Lance Taylor <[email protected]> Run-TryBot: Ian Lance Taylor <[email protected]> Run-TryBot: Joseph Tsai <[email protected]> Auto-Submit: Ian Lance Taylor <[email protected]> Reviewed-by: Ian Lance Taylor <[email protected]> Reviewed-by: Joseph Tsai <[email protected]> Reviewed-by: David Chase <[email protected]> Reviewed-by: Daniel Martí <[email protected]>
1 parent db25bc1 commit 56d3e84

File tree

4 files changed

+26
-26
lines changed

4 files changed

+26
-26
lines changed

src/encoding/json/bench_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -450,7 +450,7 @@ func BenchmarkTypeFieldsCache(b *testing.B) {
450450
// Dynamically generate many new types.
451451
types := make([]reflect.Type, maxTypes)
452452
fs := []reflect.StructField{{
453-
Type: reflect.TypeOf(""),
453+
Type: reflect.TypeFor[string](),
454454
Index: []int{0},
455455
}}
456456
for i := range types {

src/encoding/json/decode.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -591,7 +591,7 @@ func (d *decodeState) array(v reflect.Value) error {
591591
}
592592

593593
var nullLiteral = []byte("null")
594-
var textUnmarshalerType = reflect.TypeOf((*encoding.TextUnmarshaler)(nil)).Elem()
594+
var textUnmarshalerType = reflect.TypeFor[encoding.TextUnmarshaler]()
595595

596596
// object consumes an object from d.data[d.off-1:], decoding into v.
597597
// The first byte ('{') of the object has been read already.
@@ -829,12 +829,12 @@ func (d *decodeState) convertNumber(s string) (any, error) {
829829
}
830830
f, err := strconv.ParseFloat(s, 64)
831831
if err != nil {
832-
return nil, &UnmarshalTypeError{Value: "number " + s, Type: reflect.TypeOf(0.0), Offset: int64(d.off)}
832+
return nil, &UnmarshalTypeError{Value: "number " + s, Type: reflect.TypeFor[float64](), Offset: int64(d.off)}
833833
}
834834
return f, nil
835835
}
836836

837-
var numberType = reflect.TypeOf(Number(""))
837+
var numberType = reflect.TypeFor[Number]()
838838

839839
// literalStore decodes a literal stored in item into v.
840840
//

src/encoding/json/decode_test.go

+20-20
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ type PP struct {
5757
type SS string
5858

5959
func (*SS) UnmarshalJSON(data []byte) error {
60-
return &UnmarshalTypeError{Value: "number", Type: reflect.TypeOf(SS(""))}
60+
return &UnmarshalTypeError{Value: "number", Type: reflect.TypeFor[SS]()}
6161
}
6262

6363
// ifaceNumAsFloat64/ifaceNumAsNumber are used to test unmarshaling with and
@@ -421,11 +421,11 @@ var unmarshalTests = []unmarshalTest{
421421
{in: `"g-clef: \uD834\uDD1E"`, ptr: new(string), out: "g-clef: \U0001D11E"},
422422
{in: `"invalid: \uD834x\uDD1E"`, ptr: new(string), out: "invalid: \uFFFDx\uFFFD"},
423423
{in: "null", ptr: new(any), out: nil},
424-
{in: `{"X": [1,2,3], "Y": 4}`, ptr: new(T), out: T{Y: 4}, err: &UnmarshalTypeError{"array", reflect.TypeOf(""), 7, "T", "X"}},
425-
{in: `{"X": 23}`, ptr: new(T), out: T{}, err: &UnmarshalTypeError{"number", reflect.TypeOf(""), 8, "T", "X"}}, {in: `{"x": 1}`, ptr: new(tx), out: tx{}},
424+
{in: `{"X": [1,2,3], "Y": 4}`, ptr: new(T), out: T{Y: 4}, err: &UnmarshalTypeError{"array", reflect.TypeFor[string](), 7, "T", "X"}},
425+
{in: `{"X": 23}`, ptr: new(T), out: T{}, err: &UnmarshalTypeError{"number", reflect.TypeFor[string](), 8, "T", "X"}}, {in: `{"x": 1}`, ptr: new(tx), out: tx{}},
426426
{in: `{"x": 1}`, ptr: new(tx), out: tx{}},
427427
{in: `{"x": 1}`, ptr: new(tx), err: fmt.Errorf("json: unknown field \"x\""), disallowUnknownFields: true},
428-
{in: `{"S": 23}`, ptr: new(W), out: W{}, err: &UnmarshalTypeError{"number", reflect.TypeOf(SS("")), 0, "W", "S"}},
428+
{in: `{"S": 23}`, ptr: new(W), out: W{}, err: &UnmarshalTypeError{"number", reflect.TypeFor[SS](), 0, "W", "S"}},
429429
{in: `{"F1":1,"F2":2,"F3":3}`, ptr: new(V), out: V{F1: float64(1), F2: int32(2), F3: Number("3")}},
430430
{in: `{"F1":1,"F2":2,"F3":3}`, ptr: new(V), out: V{F1: Number("1"), F2: int32(2), F3: Number("3")}, useNumber: true},
431431
{in: `{"k1":1,"k2":"s","k3":[1,2.0,3e-3],"k4":{"kk1":"s","kk2":2}}`, ptr: new(any), out: ifaceNumAsFloat64},
@@ -545,32 +545,32 @@ var unmarshalTests = []unmarshalTest{
545545
{
546546
in: `{"abc":"abc"}`,
547547
ptr: new(map[int]string),
548-
err: &UnmarshalTypeError{Value: "number abc", Type: reflect.TypeOf(0), Offset: 2},
548+
err: &UnmarshalTypeError{Value: "number abc", Type: reflect.TypeFor[int](), Offset: 2},
549549
},
550550
{
551551
in: `{"256":"abc"}`,
552552
ptr: new(map[uint8]string),
553-
err: &UnmarshalTypeError{Value: "number 256", Type: reflect.TypeOf(uint8(0)), Offset: 2},
553+
err: &UnmarshalTypeError{Value: "number 256", Type: reflect.TypeFor[uint8](), Offset: 2},
554554
},
555555
{
556556
in: `{"128":"abc"}`,
557557
ptr: new(map[int8]string),
558-
err: &UnmarshalTypeError{Value: "number 128", Type: reflect.TypeOf(int8(0)), Offset: 2},
558+
err: &UnmarshalTypeError{Value: "number 128", Type: reflect.TypeFor[int8](), Offset: 2},
559559
},
560560
{
561561
in: `{"-1":"abc"}`,
562562
ptr: new(map[uint8]string),
563-
err: &UnmarshalTypeError{Value: "number -1", Type: reflect.TypeOf(uint8(0)), Offset: 2},
563+
err: &UnmarshalTypeError{Value: "number -1", Type: reflect.TypeFor[uint8](), Offset: 2},
564564
},
565565
{
566566
in: `{"F":{"a":2,"3":4}}`,
567567
ptr: new(map[string]map[int]int),
568-
err: &UnmarshalTypeError{Value: "number a", Type: reflect.TypeOf(int(0)), Offset: 7},
568+
err: &UnmarshalTypeError{Value: "number a", Type: reflect.TypeFor[int](), Offset: 7},
569569
},
570570
{
571571
in: `{"F":{"a":2,"3":4}}`,
572572
ptr: new(map[string]map[uint]int),
573-
err: &UnmarshalTypeError{Value: "number a", Type: reflect.TypeOf(uint(0)), Offset: 7},
573+
err: &UnmarshalTypeError{Value: "number a", Type: reflect.TypeFor[uint](), Offset: 7},
574574
},
575575

576576
// Map keys can be encoding.TextUnmarshalers.
@@ -715,12 +715,12 @@ var unmarshalTests = []unmarshalTest{
715715
{
716716
in: `{"2009-11-10T23:00:00Z": "hello world"}`,
717717
ptr: new(map[Point]string),
718-
err: &UnmarshalTypeError{Value: "object", Type: reflect.TypeOf(map[Point]string{}), Offset: 1},
718+
err: &UnmarshalTypeError{Value: "object", Type: reflect.TypeFor[map[Point]string](), Offset: 1},
719719
},
720720
{
721721
in: `{"asdf": "hello world"}`,
722722
ptr: new(map[unmarshaler]string),
723-
err: &UnmarshalTypeError{Value: "object", Type: reflect.TypeOf(map[unmarshaler]string{}), Offset: 1},
723+
err: &UnmarshalTypeError{Value: "object", Type: reflect.TypeFor[map[unmarshaler]string](), Offset: 1},
724724
},
725725

726726
// related to issue 13783.
@@ -820,7 +820,7 @@ var unmarshalTests = []unmarshalTest{
820820
Value: "string",
821821
Struct: "V",
822822
Field: "V.F2",
823-
Type: reflect.TypeOf(int32(0)),
823+
Type: reflect.TypeFor[int32](),
824824
Offset: 20,
825825
},
826826
},
@@ -831,7 +831,7 @@ var unmarshalTests = []unmarshalTest{
831831
Value: "string",
832832
Struct: "V",
833833
Field: "V.F2",
834-
Type: reflect.TypeOf(int32(0)),
834+
Type: reflect.TypeFor[int32](),
835835
Offset: 30,
836836
},
837837
},
@@ -907,24 +907,24 @@ var unmarshalTests = []unmarshalTest{
907907
{
908908
in: `{"data":{"test1": "bob", "test2": 123}}`,
909909
ptr: new(mapStringToStringData),
910-
err: &UnmarshalTypeError{Value: "number", Type: reflect.TypeOf(""), Offset: 37, Struct: "mapStringToStringData", Field: "data"},
910+
err: &UnmarshalTypeError{Value: "number", Type: reflect.TypeFor[string](), Offset: 37, Struct: "mapStringToStringData", Field: "data"},
911911
},
912912
{
913913
in: `{"data":{"test1": 123, "test2": "bob"}}`,
914914
ptr: new(mapStringToStringData),
915-
err: &UnmarshalTypeError{Value: "number", Type: reflect.TypeOf(""), Offset: 21, Struct: "mapStringToStringData", Field: "data"},
915+
err: &UnmarshalTypeError{Value: "number", Type: reflect.TypeFor[string](), Offset: 21, Struct: "mapStringToStringData", Field: "data"},
916916
},
917917

918918
// trying to decode JSON arrays or objects via TextUnmarshaler
919919
{
920920
in: `[1, 2, 3]`,
921921
ptr: new(MustNotUnmarshalText),
922-
err: &UnmarshalTypeError{Value: "array", Type: reflect.TypeOf(&MustNotUnmarshalText{}), Offset: 1},
922+
err: &UnmarshalTypeError{Value: "array", Type: reflect.TypeFor[*MustNotUnmarshalText](), Offset: 1},
923923
},
924924
{
925925
in: `{"foo": "bar"}`,
926926
ptr: new(MustNotUnmarshalText),
927-
err: &UnmarshalTypeError{Value: "object", Type: reflect.TypeOf(&MustNotUnmarshalText{}), Offset: 1},
927+
err: &UnmarshalTypeError{Value: "object", Type: reflect.TypeFor[*MustNotUnmarshalText](), Offset: 1},
928928
},
929929
// #22369
930930
{
@@ -934,7 +934,7 @@ var unmarshalTests = []unmarshalTest{
934934
Value: "string",
935935
Struct: "T",
936936
Field: "PP.T.Y",
937-
Type: reflect.TypeOf(int(0)),
937+
Type: reflect.TypeFor[int](),
938938
Offset: 29,
939939
},
940940
},
@@ -945,7 +945,7 @@ var unmarshalTests = []unmarshalTest{
945945
Value: "string",
946946
Struct: "T",
947947
Field: "Ts.Y",
948-
Type: reflect.TypeOf(int(0)),
948+
Type: reflect.TypeFor[int](),
949949
Offset: 29,
950950
},
951951
},

src/encoding/json/encode.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -371,8 +371,8 @@ func typeEncoder(t reflect.Type) encoderFunc {
371371
}
372372

373373
var (
374-
marshalerType = reflect.TypeOf((*Marshaler)(nil)).Elem()
375-
textMarshalerType = reflect.TypeOf((*encoding.TextMarshaler)(nil)).Elem()
374+
marshalerType = reflect.TypeFor[Marshaler]()
375+
textMarshalerType = reflect.TypeFor[encoding.TextMarshaler]()
376376
)
377377

378378
// newTypeEncoder constructs an encoderFunc for a type.

0 commit comments

Comments
 (0)