I wrote a custom MarshalJSON function to format the time the way I wanted. But, it gives error.
json: error calling MarshalJSON for type Time: unexpected end of JSON input
If I remove
if y := tt.Year(); y == 1 {
return []byte{}, nil
}
it works, but 0 time is outputted as Mon, 01 Jan 0001 00:00:00 UTC
How can I have an empty string if time is 0 value.
func (t Time) MarshalJSON() ([]byte, error) {
tt := time.Time(t)
if y := tt.Year(); y < 0 || y >= 10000 {
return nil, errors.New("Time.MarshalJSON: year outside of range [0,9999]")
}
if y := tt.Year(); y == 1 {
return []byte{}, nil
}
return []byte(tt.Format(`"` + time.RFC1123 + `"`)), nil
}