From 18c172d69ae6303d049ec223ccb9153b8a3b1b58 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mike=20Schw=C3=B6rer?= Date: Sun, 26 Apr 2026 14:29:28 +0200 Subject: [PATCH] Revert gojson changes --- gojson/decode_test.go | 16 ++++++++-------- gojson/encode.go | 18 +++++++++++++----- gojson/encode_test.go | 29 +++++++++++++++-------------- gojson/fuzz_test.go | 8 ++++---- gojson/indent.go | 2 +- gojson/scanner_test.go | 15 ++++++++++++--- 6 files changed, 53 insertions(+), 35 deletions(-) diff --git a/gojson/decode_test.go b/gojson/decode_test.go index 0ef912b..f5b4467 100644 --- a/gojson/decode_test.go +++ b/gojson/decode_test.go @@ -108,11 +108,11 @@ func (u unmarshalerText) MarshalText() ([]byte, error) { } func (u *unmarshalerText) UnmarshalText(b []byte) error { - before, after, ok := bytes.Cut(b, []byte{':'}) - if !ok { + pos := bytes.IndexByte(b, ':') + if pos == -1 { return errors.New("missing separator") } - u.A, u.B = string(before), string(after) + u.A, u.B = string(b[:pos]), string(b[pos+1:]) return nil } @@ -126,7 +126,7 @@ type ustructText struct { type u8marshal uint8 func (u8 u8marshal) MarshalText() ([]byte, error) { - return fmt.Appendf(nil, "u%d", u8), nil + return []byte(fmt.Sprintf("u%d", u8)), nil } var errMissingU8Prefix = errors.New("missing 'u' prefix") @@ -275,7 +275,7 @@ func (unexportedWithMethods) F() {} type byteWithMarshalJSON byte func (b byteWithMarshalJSON) MarshalJSON() ([]byte, error) { - return fmt.Appendf(nil, `"Z%.2x"`, byte(b)), nil + return []byte(fmt.Sprintf(`"Z%.2x"`, byte(b))), nil } func (b *byteWithMarshalJSON) UnmarshalJSON(data []byte) error { @@ -303,7 +303,7 @@ func (b *byteWithPtrMarshalJSON) UnmarshalJSON(data []byte) error { type byteWithMarshalText byte func (b byteWithMarshalText) MarshalText() ([]byte, error) { - return fmt.Appendf(nil, `Z%.2x`, byte(b)), nil + return []byte(fmt.Sprintf(`Z%.2x`, byte(b))), nil } func (b *byteWithMarshalText) UnmarshalText(data []byte) error { @@ -331,7 +331,7 @@ func (b *byteWithPtrMarshalText) UnmarshalText(data []byte) error { type intWithMarshalJSON int func (b intWithMarshalJSON) MarshalJSON() ([]byte, error) { - return fmt.Appendf(nil, `"Z%.2x"`, int(b)), nil + return []byte(fmt.Sprintf(`"Z%.2x"`, int(b))), nil } func (b *intWithMarshalJSON) UnmarshalJSON(data []byte) error { @@ -359,7 +359,7 @@ func (b *intWithPtrMarshalJSON) UnmarshalJSON(data []byte) error { type intWithMarshalText int func (b intWithMarshalText) MarshalText() ([]byte, error) { - return fmt.Appendf(nil, `Z%.2x`, int(b)), nil + return []byte(fmt.Sprintf(`Z%.2x`, int(b))), nil } func (b *intWithMarshalText) UnmarshalText(data []byte) error { diff --git a/gojson/encode.go b/gojson/encode.go index ad166d0..4dbda60 100644 --- a/gojson/encode.go +++ b/gojson/encode.go @@ -177,7 +177,7 @@ type IndentOpt struct { // MarshalSafeCollections is like Marshal except it will marshal nil maps and // slices as '{}' and '[]' respectfully instead of 'null' -func MarshalSafeCollections(v any, nilSafeSlices bool, nilSafeMaps bool, indent *IndentOpt, filter *string) ([]byte, error) { +func MarshalSafeCollections(v interface{}, nilSafeSlices bool, nilSafeMaps bool, indent *IndentOpt, filter *string) ([]byte, error) { e := &encodeState{} err := e.marshal(v, encOpts{escapeHTML: true, nilSafeSlices: nilSafeSlices, nilSafeMaps: nilSafeMaps, filter: filter}) if err != nil { @@ -891,7 +891,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 // and its length. ptr := struct { - ptr any // always an unsafe.Pointer, but avoids a dependency on package unsafe + ptr interface{} // always an unsafe.Pointer, but avoids a dependency on package unsafe len int }{v.UnsafePointer(), v.Len()} if _, ok := e.ptrSeen[ptr]; ok { @@ -923,7 +923,7 @@ type arrayEncoder struct { func (ae arrayEncoder) encode(e *encodeState, v reflect.Value, opts encOpts) { e.WriteByte('[') n := v.Len() - for i := range n { + for i := 0; i < n; i++ { if i > 0 { e.WriteByte(',') } @@ -1075,7 +1075,10 @@ func appendString[Bytes []byte | string](dst []byte, src Bytes, escapeHTML bool) // For now, cast only a small portion of byte slices to a string // so that it can be stack allocated. This slows down []byte slightly // due to the extra copy, but keeps string performance roughly the same. - n := min(len(src)-i, utf8.UTFMax) + n := len(src) - i + if n > utf8.UTFMax { + n = utf8.UTFMax + } c, size := utf8.DecodeRuneInString(string(src[i : i+n])) if c == utf8.RuneError && size == 1 { dst = append(dst, src[start:i]...) @@ -1127,7 +1130,12 @@ type field struct { type jsonfilter []string func (j jsonfilter) Contains(t string) bool { - return slices.Contains(j, t) + for _, tag := range j { + if t == tag { + return true + } + } + return false } // typeFields returns a list of fields that JSON should recognize for the given type. diff --git a/gojson/encode_test.go b/gojson/encode_test.go index dd71b65..68fb7a9 100644 --- a/gojson/encode_test.go +++ b/gojson/encode_test.go @@ -41,7 +41,7 @@ type Optionals struct { Uo uint `json:"uo,omitempty"` Str struct{} `json:"str"` - Sto struct{} `json:"sto"` + Sto struct{} `json:"sto,omitempty"` } func TestOmitEmpty(t *testing.T) { @@ -1166,7 +1166,8 @@ func TestMarshalUncommonFieldNames(t *testing.T) { } func TestMarshalerError(t *testing.T) { - st := reflect.TypeFor[string]() + s := "test variable" + st := reflect.TypeOf(s) const errText = "json: test error" tests := []struct { @@ -1221,18 +1222,18 @@ func TestIssue63379(t *testing.T) { func TestMarshalSafeCollections(t *testing.T) { var ( - nilSlice []any - pNilSlice *[]any - nilMap map[string]any - pNilMap *map[string]any + nilSlice []interface{} + pNilSlice *[]interface{} + nilMap map[string]interface{} + pNilMap *map[string]interface{} ) type ( nilSliceStruct struct { - NilSlice []any `json:"nil_slice"` + NilSlice []interface{} `json:"nil_slice"` } nilMapStruct struct { - NilMap map[string]any `json:"nil_map"` + NilMap map[string]interface{} `json:"nil_map"` } testWithFilter struct { Test1 string `json:"test1" jsonfilter:"FILTERONE"` @@ -1241,19 +1242,19 @@ func TestMarshalSafeCollections(t *testing.T) { ) tests := []struct { - in any + in interface{} want string }{ {nilSlice, "[]"}, - {[]any{}, "[]"}, - {make([]any, 0), "[]"}, + {[]interface{}{}, "[]"}, + {make([]interface{}, 0), "[]"}, {[]int{1, 2, 3}, "[1,2,3]"}, {pNilSlice, "null"}, {nilSliceStruct{}, "{\"nil_slice\":[]}"}, {nilMap, "{}"}, - {map[string]any{}, "{}"}, - {make(map[string]any, 0), "{}"}, - {map[string]any{"1": 1, "2": 2, "3": 3}, "{\"1\":1,\"2\":2,\"3\":3}"}, + {map[string]interface{}{}, "{}"}, + {make(map[string]interface{}, 0), "{}"}, + {map[string]interface{}{"1": 1, "2": 2, "3": 3}, "{\"1\":1,\"2\":2,\"3\":3}"}, {pNilMap, "null"}, {nilMapStruct{}, "{\"nil_map\":{}}"}, {testWithFilter{}, "{\"test1\":\"\"}"}, diff --git a/gojson/fuzz_test.go b/gojson/fuzz_test.go index f019603..778664c 100644 --- a/gojson/fuzz_test.go +++ b/gojson/fuzz_test.go @@ -28,10 +28,10 @@ func FuzzUnmarshalJSON(f *testing.F) { }`)) f.Fuzz(func(t *testing.T, b []byte) { - for _, typ := range []func() any{ - func() any { return new(any) }, - func() any { return new(map[string]any) }, - func() any { return new([]any) }, + for _, typ := range []func() interface{}{ + func() interface{} { return new(interface{}) }, + func() interface{} { return new(map[string]interface{}) }, + func() interface{} { return new([]interface{}) }, } { i := typ() if err := Unmarshal(b, i); err != nil { diff --git a/gojson/indent.go b/gojson/indent.go index 2a5d490..01bfdf6 100644 --- a/gojson/indent.go +++ b/gojson/indent.go @@ -90,7 +90,7 @@ func appendCompact(dst, src []byte, escape bool) ([]byte, error) { func appendNewline(dst []byte, prefix, indent string, depth int) []byte { dst = append(dst, '\n') dst = append(dst, prefix...) - for range depth { + for i := 0; i < depth; i++ { dst = append(dst, indent...) } return dst diff --git a/gojson/scanner_test.go b/gojson/scanner_test.go index bbd3d73..068439d 100644 --- a/gojson/scanner_test.go +++ b/gojson/scanner_test.go @@ -210,7 +210,10 @@ func diff(t *testing.T, a, b []byte) { t.Helper() for i := 0; ; i++ { if i >= len(a) || i >= len(b) || a[i] != b[i] { - j := max(i-10, 0) + j := i - 10 + if j < 0 { + j = 0 + } t.Errorf("diverge at %d: «%s» vs «%s»", i, trim(a[j:]), trim(b[j:])) return } @@ -271,7 +274,10 @@ func genString(stddev float64) string { } func genArray(n int) []any { - f := min(int(math.Abs(rand.NormFloat64())*math.Min(10, float64(n/2))), n) + f := int(math.Abs(rand.NormFloat64()) * math.Min(10, float64(n/2))) + if f > n { + f = n + } if f < 1 { f = 1 } @@ -283,7 +289,10 @@ func genArray(n int) []any { } func genMap(n int) map[string]any { - f := min(int(math.Abs(rand.NormFloat64())*math.Min(10, float64(n/2))), n) + f := int(math.Abs(rand.NormFloat64()) * math.Min(10, float64(n/2))) + if f > n { + f = n + } if n > 0 && f == 0 { f = 1 }