1
0
mirror of https://github.com/golang/go synced 2024-11-22 20:14:40 -07:00

encoding/json: rewrite interface{} to any

For #49884

Change-Id: I1623201c47c820a152773d2f43d0072a1466d3bf
Reviewed-on: https://go-review.googlesource.com/c/go/+/588118
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Emmanuel Odeke <emmanuel@orijtech.com>
Reviewed-by: Ian Lance Taylor <iant@google.com>
Commit-Queue: Ian Lance Taylor <iant@google.com>
Reviewed-by: Dmitri Shuralyov <dmitshur@google.com>
Reviewed-by: Mauri de Souza Meneguzzo <mauri870@gmail.com>
Reviewed-by: Joseph Tsai <joetsai@digital-static.net>
Auto-Submit: Ian Lance Taylor <iant@google.com>
Commit-Queue: Ian Lance Taylor <iant@golang.org>
Auto-Submit: Ian Lance Taylor <iant@golang.org>
This commit is contained in:
Ian Lance Taylor 2024-05-23 20:50:25 -07:00 committed by Gopher Robot
parent 72cc7699f8
commit 8659ad972f
4 changed files with 13 additions and 13 deletions

View File

@ -53,8 +53,8 @@ import (
// - bool, for JSON booleans // - bool, for JSON booleans
// - float64, for JSON numbers // - float64, for JSON numbers
// - string, for JSON strings // - string, for JSON strings
// - []interface{}, for JSON arrays // - []any, for JSON arrays
// - map[string]interface{}, for JSON objects // - map[string]any, for JSON objects
// - nil for JSON null // - nil for JSON null
// //
// To unmarshal a JSON array into a slice, Unmarshal resets the slice length // To unmarshal a JSON array into a slice, Unmarshal resets the slice length
@ -466,7 +466,7 @@ func indirect(v reflect.Value, decodingNull bool) (Unmarshaler, encoding.TextUnm
} }
// Prevent infinite loop if v is an interface pointing to its own address: // Prevent infinite loop if v is an interface pointing to its own address:
// var v interface{} // var v any
// v = &v // v = &v
if v.Elem().Kind() == reflect.Interface && v.Elem().Elem() == v { if v.Elem().Kind() == reflect.Interface && v.Elem().Elem() == v {
v = v.Elem() v = v.Elem()
@ -1019,7 +1019,7 @@ func (d *decodeState) literalStore(item []byte, v reflect.Value, fromQuoted bool
// in an empty interface. They are not strictly necessary, // in an empty interface. They are not strictly necessary,
// but they avoid the weight of reflection in this common case. // but they avoid the weight of reflection in this common case.
// valueInterface is like value but returns interface{} // valueInterface is like value but returns any.
func (d *decodeState) valueInterface() (val any) { func (d *decodeState) valueInterface() (val any) {
switch d.opcode { switch d.opcode {
default: default:
@ -1036,7 +1036,7 @@ func (d *decodeState) valueInterface() (val any) {
return return
} }
// arrayInterface is like array but returns []interface{}. // arrayInterface is like array but returns []any.
func (d *decodeState) arrayInterface() []any { func (d *decodeState) arrayInterface() []any {
var v = make([]any, 0) var v = make([]any, 0)
for { for {
@ -1062,7 +1062,7 @@ func (d *decodeState) arrayInterface() []any {
return v return v
} }
// objectInterface is like object but returns map[string]interface{}. // objectInterface is like object but returns map[string]any.
func (d *decodeState) objectInterface() map[string]any { func (d *decodeState) objectInterface() map[string]any {
m := make(map[string]any) m := make(map[string]any)
for { for {

View File

@ -819,7 +819,7 @@ func (se sliceEncoder) encode(e *encodeState, v reflect.Value, opts encOpts) {
// Here we use a struct to memorize the pointer to the first element of the slice // Here we use a struct to memorize the pointer to the first element of the slice
// and its length. // and its length.
ptr := struct { ptr := struct {
ptr interface{} // always an unsafe.Pointer, but avoids a dependency on package unsafe ptr any // always an unsafe.Pointer, but avoids a dependency on package unsafe
len int len int
}{v.UnsafePointer(), v.Len()} }{v.UnsafePointer(), v.Len()}
if _, ok := e.ptrSeen[ptr]; ok { if _, ok := e.ptrSeen[ptr]; ok {

View File

@ -28,10 +28,10 @@ func FuzzUnmarshalJSON(f *testing.F) {
}`)) }`))
f.Fuzz(func(t *testing.T, b []byte) { f.Fuzz(func(t *testing.T, b []byte) {
for _, typ := range []func() interface{}{ for _, typ := range []func() any{
func() interface{} { return new(interface{}) }, func() any { return new(any) },
func() interface{} { return new(map[string]interface{}) }, func() any { return new(map[string]any) },
func() interface{} { return new([]interface{}) }, func() any { return new([]any) },
} { } {
i := typ() i := typ()
if err := Unmarshal(b, i); err != nil { if err := Unmarshal(b, i); err != nil {

View File

@ -32,8 +32,8 @@ func NewDecoder(r io.Reader) *Decoder {
return &Decoder{r: r} return &Decoder{r: r}
} }
// UseNumber causes the Decoder to unmarshal a number into an interface{} as a // UseNumber causes the Decoder to unmarshal a number into an
// [Number] instead of as a float64. // interface value as a [Number] instead of as a float64.
func (dec *Decoder) UseNumber() { dec.d.useNumber = true } func (dec *Decoder) UseNumber() { dec.d.useNumber = true }
// DisallowUnknownFields causes the Decoder to return an error when the destination // DisallowUnknownFields causes the Decoder to return an error when the destination