From 65c8b28ca1ca182c9125a068c9aaef7492236391 Mon Sep 17 00:00:00 2001 From: Andrey Grazhdankov Date: Mon, 11 Nov 2024 05:10:21 +0300 Subject: [PATCH] Fix encode []*time.Time - check nil (#524) --- encode_test.go | 5 +++++ internal/encoder/encoder.go | 5 +++++ 2 files changed, 10 insertions(+) diff --git a/encode_test.go b/encode_test.go index 1165f6a..ede0568 100644 --- a/encode_test.go +++ b/encode_test.go @@ -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) { diff --git a/internal/encoder/encoder.go b/internal/encoder/encoder.go index 14eb6a0..b436f5b 100644 --- a/internal/encoder/encoder.go +++ b/internal/encoder/encoder.go @@ -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 {