Skip to content

Commit

Permalink
Null dates should unmarshal as zero dates
Browse files Browse the repository at this point in the history
  • Loading branch information
aodin committed Dec 21, 2015
1 parent 27ee7c2 commit f3e4e65
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 1 deletion.
3 changes: 3 additions & 0 deletions date.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ func (date Date) Equals(other Date) bool {
// UnmarshalJSON converts a byte array into a Date
func (d *Date) UnmarshalJSON(text []byte) error {
if string(text) == "null" {
// Nulls are converted to zero times
var zero Date
*d = zero
return nil
}
b := bytes.NewBuffer(text)
Expand Down
4 changes: 3 additions & 1 deletion range_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,10 @@ func TestRange_Union(t *testing.T) {
}

func TestRange_Unmarshal(t *testing.T) {
// Unmarshaling should overwrite values
open := EntireMonth(2015, 2)

raw := `{"start":"2015-03-01","end":null}`
var open Range
assert.Nil(t, json.Unmarshal([]byte(raw), &open))
assert.Equal(t, New(2015, 3, 1), open.Start)
assert.True(t, open.End.IsZero())
Expand Down

0 comments on commit f3e4e65

Please sign in to comment.