diff --git a/src/encoding/json/v2/bench_test.go b/src/encoding/json/v2/bench_test.go index 7672acb1d1c93f..4733bbfc6af3eb 100644 --- a/src/encoding/json/v2/bench_test.go +++ b/src/encoding/json/v2/bench_test.go @@ -278,6 +278,12 @@ var arshalTestdata = []struct { raw: []byte(`"2006-01-02T22:04:05Z"`), val: addr(time.Unix(1136239445, 0).UTC()), new: func() any { return new(time.Time) }, +}, { + name: "OmitZeroMethod", + raw: []byte(`{}`), + val: new(omitZeroStruct), + new: func() any { return new(omitZeroStruct) }, + skipV1: true, }} type textArshaler struct{ _ [4]int } @@ -317,6 +323,25 @@ func (*jsonArshalerV2) UnmarshalJSONFrom(dec *jsontext.Decoder) error { return err } +type omitZeroStruct struct { + A zeroAlways `json:"a,omitzero"` + B zeroAlwaysPtr `json:"b,omitzero"` +} + +type zeroAlways struct { + // Just so the struct has some size to allocate + A int +} + +func (zeroAlways) IsZero() bool { return true } + +type zeroAlwaysPtr struct { + // Just so the struct has some size to allocate + A int +} + +func (*zeroAlwaysPtr) IsZero() bool { return true } + func TestBenchmarkUnmarshal(t *testing.T) { runUnmarshal(t) } func BenchmarkUnmarshal(b *testing.B) { runUnmarshal(b) } diff --git a/src/encoding/json/v2/fields.go b/src/encoding/json/v2/fields.go index f67a9ecb08f237..860e34170ccfbc 100644 --- a/src/encoding/json/v2/fields.go +++ b/src/encoding/json/v2/fields.go @@ -229,10 +229,10 @@ func makeStructFields(root reflect.Type) (fs structFields, serr *SemanticError) // Avoid panics calling IsZero on nil pointer. return va.IsNil() || va.Interface().(isZeroer).IsZero() } - case sf.Type.Implements(isZeroerType): - f.isZero = func(va addressableValue) bool { return va.Interface().(isZeroer).IsZero() } case reflect.PointerTo(sf.Type).Implements(isZeroerType): f.isZero = func(va addressableValue) bool { return va.Addr().Interface().(isZeroer).IsZero() } + case sf.Type.Implements(isZeroerType): + f.isZero = func(va addressableValue) bool { return va.Interface().(isZeroer).IsZero() } } // Provide a function that can determine whether the value would