Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions src/encoding/json/v2/bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down Expand Up @@ -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) }

Expand Down
4 changes: 2 additions & 2 deletions src/encoding/json/v2/fields.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down