Skip to content

Commit a6a59f0

Browse files
dsnetgopherbot
authored andcommitted
encoding/json/v2: use slices.Sort directly
This is semantically identical and just a cleanup. Prior to golang#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 go-json-experiment/json#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 <[email protected]> Reviewed-by: Dmitri Shuralyov <[email protected]> TryBot-Bypass: Damien Neil <[email protected]> Auto-Submit: Damien Neil <[email protected]> Reviewed-by: Daniel Martí <[email protected]> Reviewed-by: Damien Neil <[email protected]>
1 parent 0d3dab9 commit a6a59f0

File tree

4 files changed

+5
-9
lines changed

4 files changed

+5
-9
lines changed

src/encoding/json/v2/arshal.go

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@ import (
1111
"encoding"
1212
"io"
1313
"reflect"
14-
"slices"
15-
"strings"
1614
"sync"
1715
"time"
1816

@@ -575,7 +573,3 @@ func putStrings(s *stringSlice) {
575573
}
576574
stringsPools.Put(s)
577575
}
578-
579-
func (ss *stringSlice) Sort() {
580-
slices.SortFunc(*ss, func(x, y string) int { return strings.Compare(x, y) })
581-
}

src/encoding/json/v2/arshal_any.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"cmp"
1111
"math"
1212
"reflect"
13+
"slices"
1314
"strconv"
1415

1516
"encoding/json/internal"
@@ -153,7 +154,7 @@ func marshalObjectAny(enc *jsontext.Encoder, obj map[string]any, mo *jsonopts.St
153154
(*names)[i] = name
154155
i++
155156
}
156-
names.Sort()
157+
slices.Sort(*names)
157158
for _, name := range *names {
158159
if err := enc.WriteToken(jsontext.String(name)); err != nil {
159160
return err

src/encoding/json/v2/arshal_default.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -843,7 +843,7 @@ func makeMapArshaler(t reflect.Type) *arshaler {
843843
k.SetIterKey(iter)
844844
(*names)[i] = k.String()
845845
}
846-
names.Sort()
846+
slices.Sort(*names)
847847
for _, name := range *names {
848848
if err := enc.WriteToken(jsontext.String(name)); err != nil {
849849
return err

src/encoding/json/v2/arshal_inlined.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"errors"
1212
"io"
1313
"reflect"
14+
"slices"
1415

1516
"encoding/json/internal/jsonflags"
1617
"encoding/json/internal/jsonopts"
@@ -146,7 +147,7 @@ func marshalInlinedFallbackAll(enc *jsontext.Encoder, va addressableValue, mo *j
146147
mk.SetIterKey(iter)
147148
(*names)[i] = mk.String()
148149
}
149-
names.Sort()
150+
slices.Sort(*names)
150151
for _, name := range *names {
151152
mk.SetString(name)
152153
if err := marshalKey(mk); err != nil {

0 commit comments

Comments
 (0)