diff --git a/arshal_test.go b/arshal_test.go index 909e060..cb1c140 100644 --- a/arshal_test.go +++ b/arshal_test.go @@ -132,9 +132,9 @@ type ( // cannot be called on an unexported field. netipAddr `json:"name"` - // structMethodText has a MarshalText method - // that cancels out netipAddr.MarshalText. - structMethodText + // Bogus MarshalText and AppendText methods are declared on + // structUnexportedEmbeddedMethodTag to prevent it from + // implementing those method interfaces. } structUnexportedEmbeddedStruct struct { structOmitZeroAll @@ -573,6 +573,9 @@ type ( } ) +func (structUnexportedEmbeddedMethodTag) MarshalText() {} +func (structUnexportedEmbeddedMethodTag) AppendText() {} + func (p *allMethods) MarshalJSONV2(enc *jsontext.Encoder, opts Options) error { if got, want := "MarshalJSONV2", p.method; got != want { return fmt.Errorf("called wrong method: got %v, want %v", got, want) @@ -9223,7 +9226,9 @@ func TestCoderBufferGrowth(t *testing.T) { } } - bb := &bytesBuffer{new(bytes.Buffer)} + // bb is identical to bytes.Buffer, + // but a different type to avoid any optimizations for bytes.Buffer. + bb := struct{ *bytes.Buffer }{new(bytes.Buffer)} var writeSizes []int if err := MarshalWrite(WriterFunc(func(b []byte) (int, error) { diff --git a/bench_test.go b/bench_test.go index f209dfb..8bc94b7 100644 --- a/bench_test.go +++ b/bench_test.go @@ -2,7 +2,7 @@ // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. -package json +package json_test import ( "bytes" @@ -18,6 +18,7 @@ import ( jsonv1 "encoding/json" + jsonv2 "github.com/go-json-experiment/json" "github.com/go-json-experiment/json/internal/jsontest" "github.com/go-json-experiment/json/jsontext" ) @@ -28,6 +29,14 @@ var benchV1 = os.Getenv("BENCHMARK_V1") != "" // but a different type to avoid any optimizations for bytes.Buffer. type bytesBuffer struct{ *bytes.Buffer } +func addr[T any](v T) *T { + return &v +} + +func len64[Bytes ~[]byte | ~string](in Bytes) int64 { + return int64(len(in)) +} + var arshalTestdata = []struct { name string raw []byte @@ -206,10 +215,10 @@ func (*jsonArshalerV1) UnmarshalJSON(b []byte) error { type jsonArshalerV2 struct{ _ [4]int } -func (jsonArshalerV2) MarshalJSONV2(enc *jsontext.Encoder, opts Options) error { +func (jsonArshalerV2) MarshalJSONV2(enc *jsontext.Encoder, opts jsonv2.Options) error { return enc.WriteToken(jsontext.String("method")) } -func (*jsonArshalerV2) UnmarshalJSONV2(dec *jsontext.Decoder, opts Options) error { +func (*jsonArshalerV2) UnmarshalJSONV2(dec *jsontext.Decoder, opts jsonv2.Options) error { b, err := dec.ReadValue() if string(b) != `"method"` { return fmt.Errorf("UnmarshalJSONV2: got %q, want %q", b, `"method"`) @@ -226,7 +235,7 @@ func runUnmarshal(tb testing.TB) { var val any run := func(tb testing.TB) { val = tt.new() - if err := Unmarshal(tt.raw, val); err != nil { + if err := jsonv2.Unmarshal(tt.raw, val); err != nil { tb.Fatalf("Unmarshal error: %v", err) } } @@ -266,7 +275,7 @@ func runMarshal(tb testing.TB) { var raw []byte run := func(tb testing.TB) { var err error - raw, err = Marshal(tt.val) + raw, err = jsonv2.Marshal(tt.val) if err != nil { tb.Fatalf("Marshal error: %v", err) } @@ -347,7 +356,7 @@ func runAllTestdata(tb testing.TB) { func mustUnmarshalValue(t testing.TB, data []byte, newValue func() any) (value any) { value = newValue() - if err := Unmarshal(data, value); err != nil { + if err := jsonv2.Unmarshal(data, value); err != nil { t.Fatalf("Unmarshal error: %v", err) } return value @@ -370,11 +379,11 @@ func runArshal(t testing.TB, arshalName string, newValue func() any, data []byte switch arshalName { case "Marshal": - if _, err := Marshal(value); err != nil { + if _, err := jsonv2.Marshal(value); err != nil { t.Fatalf("Marshal error: %v", err) } case "Unmarshal": - if err := Unmarshal(data, newValue()); err != nil { + if err := jsonv2.Unmarshal(data, newValue()); err != nil { t.Fatalf("Unmarshal error: %v", err) } } diff --git a/fold_test.go b/fold_test.go index 1be7c13..fc7e421 100644 --- a/fold_test.go +++ b/fold_test.go @@ -9,8 +9,6 @@ import ( "reflect" "testing" "unicode" - - jsonv1 "encoding/json" ) var equalFoldTestdata = []struct { @@ -103,18 +101,10 @@ func TestFoldRune(t *testing.T) { // TestBenchmarkUnmarshalUnknown unmarshals an unknown field into a struct with // varying number of fields. Since the unknown field does not directly match // any known field by name, it must fall back on case-insensitive matching. -func TestBenchmarkUnmarshalUnknown(t *testing.T) { runUnmarshalUnknown(t) } -func BenchmarkUnmarshalUnknown(b *testing.B) { runUnmarshalUnknown(b) } - -func runUnmarshalUnknown(tb testing.TB) { +func TestBenchmarkUnmarshalUnknown(t *testing.T) { in := []byte(`{"NameUnknown":null}`) for _, n := range []int{1, 2, 5, 10, 20, 50, 100} { unmarshal := Unmarshal - if benchV1 { - unmarshal = func(in []byte, out any, opts ...Options) error { - return jsonv1.Unmarshal(in, out) - } - } var fields []reflect.StructField for i := range n { @@ -126,9 +116,9 @@ func runUnmarshalUnknown(tb testing.TB) { } out := reflect.New(reflect.StructOf(fields)).Interface() - runTestOrBench(tb, fmt.Sprintf("N%d", n), len64(in), func(tb testing.TB) { + t.Run(fmt.Sprintf("N%d", n), func(t *testing.T) { if err := unmarshal(in, out); err != nil { - tb.Fatalf("Unmarshal error: %v", err) + t.Fatalf("Unmarshal error: %v", err) } }) }