// It supports string, object (e.g. pq.NullTime and friends)
// and null input.
func(t*Time)UnmarshalJSON(data[]byte)error{
varerrerror
varvinterface{}
iferr=json.Unmarshal(data,&v);err!=nil{
returnerr
}
switchx:=v.(type){
casestring:
err=t.Time.UnmarshalJSON(data)
casemap[string]interface{}:
ti,tiOK:=x["Time"].(string)
valid,validOK:=x["Valid"].(bool)
if!tiOK||!validOK{
returnfmt.Errorf(`json: unmarshalling object into Go value of type null.Time requires key "Time" to be of type string and key "Valid" to be of type bool; found %T and %T, respectively`,x["Time"],x["Valid"])
}
err=t.Time.UnmarshalText([]byte(ti))
t.Valid=valid
returnerr
casenil:
t.Valid=false
returnnil
default:
err=fmt.Errorf("json: cannot unmarshal %v into Go value of type null.Time",reflect.TypeOf(v).Name())
}
t.Valid=err==nil
returnerr
}
func(tTime)MarshalText()([]byte,error){
if!t.Valid{
return[]byte("null"),nil
}
returnt.Time.MarshalText()
}
func(t*Time)UnmarshalText(text[]byte)error{
str:=string(text)
ifstr==""||str=="null"{
t.Valid=false
returnnil
}
iferr:=t.Time.UnmarshalText(text);err!=nil{
returnerr
}
t.Valid=true
returnnil
}
// SetValid changes this Time's value and sets it to be non-null.
func(t*Time)SetValid(vtime.Time){
t.Time=v
t.Valid=true
}
// Ptr returns a pointer to this Time's value, or a nil pointer if this Time is null.