Skip to content

Commit

Permalink
Fix encode []*time.Time - check nil (#524)
Browse files Browse the repository at this point in the history
  • Loading branch information
mirecl authored Nov 11, 2024
1 parent 5e2ae3f commit 65c8b28
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 0 deletions.
5 changes: 5 additions & 0 deletions encode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
5 changes: 5 additions & 0 deletions internal/encoder/encoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 65c8b28

Please sign in to comment.