Marshalling zero valued Deleted at to nullhttps://github.com/go-gorm/gorm/issues/3693

This commit is contained in:
Amit Basuri 2020-10-30 15:56:29 +05:30
parent a8141b6cc9
commit 36bc5ef897

View File

@ -26,12 +26,19 @@ func (n DeletedAt) Value() (driver.Value, error) {
} }
func (n DeletedAt) MarshalJSON() ([]byte, error) { func (n DeletedAt) MarshalJSON() ([]byte, error) {
if !n.Valid {
return []byte(`null`), nil
}
return json.Marshal(n.Time) return json.Marshal(n.Time)
} }
func (n *DeletedAt) UnmarshalJSON(b []byte) error { func (n *DeletedAt) UnmarshalJSON(b []byte) error {
if string(b) == "null" {
n.Valid = false
return nil
}
err := json.Unmarshal(b, &n.Time) err := json.Unmarshal(b, &n.Time)
if err == nil && !n.Time.IsZero() { if err == nil {
n.Valid = true n.Valid = true
} }
return err return err