File tree Expand file tree Collapse file tree 2 files changed +25
-5
lines changed Expand file tree Collapse file tree 2 files changed +25
-5
lines changed Original file line number Diff line number Diff line change @@ -23,8 +23,19 @@ var _ json.Marshaler = Map(nil)
23
23
// MarshalJSON implements json.Marshaler.
24
24
//
25
25
// It is guaranteed to return a nil error.
26
- // Any error marshalling a field will
27
- // become the field's value.
26
+ // Any error marshalling a field will become the field's value.
27
+ //
28
+ // Every field value is encoded with the following process:
29
+ //
30
+ // 1. slog.Value is handled to allow any type to replace its representation for logging.
31
+ //
32
+ // 2. xerrors.Formatter is handled.
33
+ //
34
+ // 3. error and fmt.Stringer are handled.
35
+ //
36
+ // 4. slices and arrays are handled to go through the encode function for every value.
37
+ //
38
+ // 5. json.Marshal is invoked as the default case.
28
39
func (m Map ) MarshalJSON () ([]byte , error ) {
29
40
b := & bytes.Buffer {}
30
41
b .WriteByte ('{' )
@@ -90,8 +101,7 @@ func encode(v interface{}) []byte {
90
101
switch rv .Type ().Kind () {
91
102
case reflect .Slice :
92
103
if rv .IsNil () {
93
- b , _ := json .Marshal (nil )
94
- return b
104
+ break
95
105
}
96
106
fallthrough
97
107
case reflect .Array :
Original file line number Diff line number Diff line change @@ -93,7 +93,7 @@ func TestMap(t *testing.T) {
93
93
{
94
94
"msg": "failed to marshal to JSON",
95
95
"fun": "cdr.dev/slog.encode",
96
- "loc": "` + mapTestFile + `:105 "
96
+ "loc": "` + mapTestFile + `:115 "
97
97
},
98
98
"json: unsupported type: func(*testing.T, string) string"
99
99
],
@@ -164,6 +164,16 @@ func TestMap(t *testing.T) {
164
164
"error": "xdxd"
165
165
}` )
166
166
})
167
+
168
+ t .Run ("nilSlice" , func (t * testing.T ) {
169
+ t .Parallel ()
170
+
171
+ test (t , slog .M (
172
+ slog .F ("slice" , []string (nil )),
173
+ ), `{
174
+ "slice": null
175
+ }` )
176
+ })
167
177
}
168
178
169
179
type meow struct {
You can’t perform that action at this time.
0 commit comments