From a6a59f0762787fd50c7069b77b0addbc2339c8d2 Mon Sep 17 00:00:00 2001 From: Joe Tsai Date: Tue, 21 Oct 2025 10:39:15 -0700 Subject: [PATCH] encoding/json/v2: use slices.Sort directly MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit This is semantically identical and just a cleanup. Prior to #63397, JSON object names were sorted according to UTF-16 to match the semantic of RFC 8785, but there were a number of objections in the discussion to using that as the sorting order. In https://github.com/go-json-experiment/json/pull/121, we switched to sorting by UTF-8, which matches the behavior of v1 and avoids an option to toggle the behavior. However, we should have deleted the stringSlice.Sort method and just directly called slices.Sort. From a principled perspective, both UTF-16 and UTF-8 are reasonable ways to sort JSON object names. RFC 8259 specifies that the entire JSON text is encoded as UTF-8. However, the way JSON strings are encoded requires escaping Unicode codepoints according to UTF-16 surragate halves (a quirk of JavaScript inherited by JSON). Thus, JSON is inconsistently both UTF-8 and UTF-16. Change-Id: Id92b5cc20efe4201827e9d3fccf24ccf894d3e60 Reviewed-on: https://go-review.googlesource.com/c/go/+/713522 Reviewed-by: Johan Brandhorst-Satzkorn Reviewed-by: Dmitri Shuralyov TryBot-Bypass: Damien Neil Auto-Submit: Damien Neil Reviewed-by: Daniel Martí Reviewed-by: Damien Neil --- src/encoding/json/v2/arshal.go | 6 ------ src/encoding/json/v2/arshal_any.go | 3 ++- src/encoding/json/v2/arshal_default.go | 2 +- src/encoding/json/v2/arshal_inlined.go | 3 ++- 4 files changed, 5 insertions(+), 9 deletions(-) diff --git a/src/encoding/json/v2/arshal.go b/src/encoding/json/v2/arshal.go index 573d26567f..e26f3340ee 100644 --- a/src/encoding/json/v2/arshal.go +++ b/src/encoding/json/v2/arshal.go @@ -11,8 +11,6 @@ import ( "encoding" "io" "reflect" - "slices" - "strings" "sync" "time" @@ -575,7 +573,3 @@ func putStrings(s *stringSlice) { } stringsPools.Put(s) } - -func (ss *stringSlice) Sort() { - slices.SortFunc(*ss, func(x, y string) int { return strings.Compare(x, y) }) -} diff --git a/src/encoding/json/v2/arshal_any.go b/src/encoding/json/v2/arshal_any.go index 97a77e9237..8c0c445404 100644 --- a/src/encoding/json/v2/arshal_any.go +++ b/src/encoding/json/v2/arshal_any.go @@ -10,6 +10,7 @@ import ( "cmp" "math" "reflect" + "slices" "strconv" "encoding/json/internal" @@ -153,7 +154,7 @@ func marshalObjectAny(enc *jsontext.Encoder, obj map[string]any, mo *jsonopts.St (*names)[i] = name i++ } - names.Sort() + slices.Sort(*names) for _, name := range *names { if err := enc.WriteToken(jsontext.String(name)); err != nil { return err diff --git a/src/encoding/json/v2/arshal_default.go b/src/encoding/json/v2/arshal_default.go index 078d345e14..33931af17e 100644 --- a/src/encoding/json/v2/arshal_default.go +++ b/src/encoding/json/v2/arshal_default.go @@ -843,7 +843,7 @@ func makeMapArshaler(t reflect.Type) *arshaler { k.SetIterKey(iter) (*names)[i] = k.String() } - names.Sort() + slices.Sort(*names) for _, name := range *names { if err := enc.WriteToken(jsontext.String(name)); err != nil { return err diff --git a/src/encoding/json/v2/arshal_inlined.go b/src/encoding/json/v2/arshal_inlined.go index d911bfa1c0..03e563a0c0 100644 --- a/src/encoding/json/v2/arshal_inlined.go +++ b/src/encoding/json/v2/arshal_inlined.go @@ -11,6 +11,7 @@ import ( "errors" "io" "reflect" + "slices" "encoding/json/internal/jsonflags" "encoding/json/internal/jsonopts" @@ -146,7 +147,7 @@ func marshalInlinedFallbackAll(enc *jsontext.Encoder, va addressableValue, mo *j mk.SetIterKey(iter) (*names)[i] = mk.String() } - names.Sort() + slices.Sort(*names) for _, name := range *names { mk.SetString(name) if err := marshalKey(mk); err != nil { -- 2.52.0