Skip to content

Commit 833bf32

Browse files
committed
cm: encode empty Lists as []
1 parent 2e6a4b4 commit 833bf32

File tree

2 files changed

+4
-5
lines changed

2 files changed

+4
-5
lines changed

cm/list.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,8 @@ func (l list[T]) Len() uintptr {
6565

6666
// MarshalJSON implements json.Marshaler.
6767
func (l list[T]) MarshalJSON() ([]byte, error) {
68-
if l.data == nil {
69-
// This cannot return nullLiteral because the caller can mutate the slice.
70-
return []byte("null"), nil
68+
if l.len == 0 {
69+
return []byte("[]"), nil
7170
}
7271

7372
s := l.Slice()

cm/list_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ func TestListMarshalJSON(t *testing.T) {
3939
w: newListEncoder(``, []float64{float64(math.NaN())}, true),
4040
},
4141
{
42-
name: "null",
43-
w: newListEncoder[string](`null`, nil, false),
42+
name: "nil",
43+
w: newListEncoder[string](`[]`, nil, false),
4444
},
4545
{
4646
name: "empty",

0 commit comments

Comments
 (0)