Skip to content

Commit 87b7b3f

Browse files
authored
fix: Treat JSON null as a no-op in Timestamp.UnmarshalJSON (#4732)
1 parent 5c5bbe8 commit 87b7b3f

2 files changed

Lines changed: 5 additions & 0 deletions

File tree

‎github/timestamp.go‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,12 @@ func (t *Timestamp) GetTime() *time.Time {
3232

3333
// UnmarshalJSON implements the json.Unmarshaler interface.
3434
// Time is expected in RFC3339 or Unix format.
35+
// A JSON null is a no-op, following the encoding/json convention.
3536
func (t *Timestamp) UnmarshalJSON(data []byte) (err error) {
3637
str := string(data)
38+
if str == "null" {
39+
return nil
40+
}
3741
i, err := strconv.ParseInt(str, 10, 64)
3842
if err == nil {
3943
t.Time = time.Unix(i, 0)

‎github/timestamp_test.go‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ func TestTimestamp_Unmarshal(t *testing.T) {
6868
{"ReferenceUnixMillisecond", referenceUnixTimeStrMilliSeconds, Timestamp{referenceTime}, false, true},
6969
{"ReferenceFractional", referenceTimeStrFractional, Timestamp{referenceTime}, false, true},
7070
{"Empty", emptyTimeStr, Timestamp{}, false, true},
71+
{"Null", `null`, Timestamp{}, false, true},
7172
{"UnixStart", `0`, Timestamp{unixOrigin}, false, true},
7273
{"Mismatch", referenceTimeStr, Timestamp{}, false, false},
7374
{"MismatchUnix", `0`, Timestamp{}, false, false},

0 commit comments

Comments
 (0)