Fix encode []*time.Time - check nil (#524)

This commit is contained in:
Andrey Grazhdankov 2024-11-11 05:10:21 +03:00 committed by GitHub
parent 5e2ae3f23c
commit 65c8b28ca1
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 10 additions and 0 deletions

View File

@ -426,6 +426,11 @@ func Test_Marshal(t *testing.T) {
assertErr(t, err)
assertEq(t, "[]interface{}", `[1,2.1,"hello"]`, string(bytes))
})
t.Run("[]*time.Time", func(t *testing.T) {
bytes, err := json.Marshal([]*time.Time{nil})
assertErr(t, err)
assertEq(t, "[]*time.Time", `[null]`, string(bytes))
})
})
t.Run("array", func(t *testing.T) {

View File

@ -406,6 +406,11 @@ func AppendMarshalJSON(ctx *RuntimeContext, code *Opcode, b []byte, v interface{
rv = newV
}
}
if rv.Kind() == reflect.Ptr && rv.IsNil() {
return AppendNull(ctx, b), nil
}
v = rv.Interface()
var bb []byte
if (code.Flags & MarshalerContextFlags) != 0 {