aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorShulhan <m.shulhan@gmail.com>2024-01-25 04:18:36 +0700
committerShulhan <m.shulhan@gmail.com>2026-04-14 21:51:40 +0700
commit7ace40ec22f23a9df78fdbee54fc44e6e30fb1dc (patch)
tree31b57ce6b9a575c4f9318e375d4f4ac402ec8e86
parent4f51d4c33231636f46d44f996cf4c6822dd2ee19 (diff)
downloadgo-7ace40ec22f23a9df78fdbee54fc44e6e30fb1dc.tar.xz
encoding/json: realign struct UnmarshalTypeError and decodeState
This reduce the UnmarshalTypeError size from 64 to 56 bytes (-8 bytes), and decodeState from 128 to 96 (-32 bytes).
-rw-r--r--src/encoding/json/decode.go10
-rw-r--r--src/encoding/json/decode_test.go8
2 files changed, 9 insertions, 9 deletions
diff --git a/src/encoding/json/decode.go b/src/encoding/json/decode.go
index 59d4c7e734..c33b0ada8a 100644
--- a/src/encoding/json/decode.go
+++ b/src/encoding/json/decode.go
@@ -124,11 +124,11 @@ type Unmarshaler interface {
// An UnmarshalTypeError describes a JSON value that was
// not appropriate for a value of a specific Go type.
type UnmarshalTypeError struct {
- Value string // description of JSON value - "bool", "array", "number -5"
Type reflect.Type // type of Go value it could not be assigned to
- Offset int64 // error occurred after reading Offset bytes
+ Value string // description of JSON value - "bool", "array", "number -5"
Struct string // name of the struct type containing the field
Field string // the full path from root node to the field, include embedded struct
+ Offset int64 // error occurred after reading Offset bytes
}
func (e *UnmarshalTypeError) Error() string {
@@ -210,12 +210,12 @@ type errorContext struct {
// decodeState represents the state while decoding a JSON value.
type decodeState struct {
+ scan scanner
+ savedError error
+ errorContext *errorContext
data []byte
off int // next read offset in data
opcode int // last read result
- scan scanner
- errorContext *errorContext
- savedError error
useNumber bool
disallowUnknownFields bool
}
diff --git a/src/encoding/json/decode_test.go b/src/encoding/json/decode_test.go
index 0b26b8eb91..2f966a824d 100644
--- a/src/encoding/json/decode_test.go
+++ b/src/encoding/json/decode_test.go
@@ -442,13 +442,13 @@ var unmarshalTests = []struct {
{CaseName: Name(""), in: `"g-clef: \uD834\uDD1E"`, ptr: new(string), out: "g-clef: \U0001D11E"},
{CaseName: Name(""), in: `"invalid: \uD834x\uDD1E"`, ptr: new(string), out: "invalid: \uFFFDx\uFFFD"},
{CaseName: Name(""), in: "null", ptr: new(any), out: nil},
- {CaseName: Name(""), in: `{"X": [1,2,3], "Y": 4}`, ptr: new(T), out: T{Y: 4}, err: &UnmarshalTypeError{"array", reflect.TypeFor[string](), 7, "T", "X"}},
- {CaseName: Name(""), in: `{"X": 23}`, ptr: new(T), out: T{}, err: &UnmarshalTypeError{"number", reflect.TypeFor[string](), 8, "T", "X"}},
+ {CaseName: Name(""), in: `{"X": [1,2,3], "Y": 4}`, ptr: new(T), out: T{Y: 4}, err: &UnmarshalTypeError{reflect.TypeFor[string](), "array", "T", "X", 7}},
+ {CaseName: Name(""), in: `{"X": 23}`, ptr: new(T), out: T{}, err: &UnmarshalTypeError{reflect.TypeFor[string](), "number", "T", "X", 8}},
{CaseName: Name(""), in: `{"x": 1}`, ptr: new(tx), out: tx{}},
{CaseName: Name(""), in: `{"x": 1}`, ptr: new(tx), out: tx{}},
{CaseName: Name(""), in: `{"x": 1}`, ptr: new(tx), err: fmt.Errorf("json: unknown field \"x\""), disallowUnknownFields: true},
- {CaseName: Name(""), in: `{"S": 23}`, ptr: new(W), out: W{}, err: &UnmarshalTypeError{"number", reflect.TypeFor[SS](), 0, "W", "S"}},
- {CaseName: Name(""), in: `{"T": {"X": 23}}`, ptr: new(TOuter), out: TOuter{}, err: &UnmarshalTypeError{"number", reflect.TypeFor[string](), 8, "TOuter", "T.X"}},
+ {CaseName: Name(""), in: `{"S": 23}`, ptr: new(W), out: W{}, err: &UnmarshalTypeError{Value: "number", Type: reflect.TypeFor[SS](), Offset: 0, Struct: "W", Field: "S"}},
+ {CaseName: Name(""), in: `{"T": {"X": 23}}`, ptr: new(TOuter), out: TOuter{}, err: &UnmarshalTypeError{Value: "number", Type: reflect.TypeFor[string](), Offset: 8, Struct: "TOuter", Field: "T.X"}},
{CaseName: Name(""), in: `{"F1":1,"F2":2,"F3":3}`, ptr: new(V), out: V{F1: float64(1), F2: int32(2), F3: Number("3")}},
{CaseName: Name(""), in: `{"F1":1,"F2":2,"F3":3}`, ptr: new(V), out: V{F1: Number("1"), F2: int32(2), F3: Number("3")}, useNumber: true},
{CaseName: Name(""), in: `{"k1":1,"k2":"s","k3":[1,2.0,3e-3],"k4":{"kk1":"s","kk2":2}}`, ptr: new(any), out: ifaceNumAsFloat64},