This repository has been archived on 2025-03-30. You can view files and clone it, but cannot push or open issues or pull requests.
bactdb/types/null_time.go
Matthew Dillon efb0cc13fa Golint
2015-10-05 10:34:21 -07:00

41 lines
812 B
Go

package types
import (
"bytes"
"encoding/json"
"time"
"github.com/thermokarst/bactdb/Godeps/_workspace/src/github.com/lib/pq"
)
// NullTime wraps pq.NullTime so that the JSON serialization can be overridden.
type NullTime struct {
pq.NullTime
}
// MarshalJSON makes NullTime a json.Marshaller.
func (t *NullTime) MarshalJSON() ([]byte, error) {
if !t.Valid {
return []byte("null"), nil
}
return json.Marshal(t.Time)
}
// UnmarshalJSON makes NullTime a json.Unmarshaller.
func (t *NullTime) UnmarshalJSON(b []byte) error {
if bytes.Equal(b, []byte("null")) {
var nt time.Time
t.Time = nt.In(time.UTC)
t.Valid = false
return nil
}
var x interface{}
var err error
json.Unmarshal(b, &x)
switch x.(type) {
case string:
err = json.Unmarshal(b, &t.Time)
}
t.Valid = true
return err
}