Fix DeletedAt marshalling, close #3598

This commit is contained in:
Jinzhu 2020-10-13 14:12:03 +08:00
parent 063b1ca0c4
commit 689d6e2331
1 changed files with 13 additions and 0 deletions

View File

@ -3,6 +3,7 @@ package gorm
import ( import (
"database/sql" "database/sql"
"database/sql/driver" "database/sql/driver"
"encoding/json"
"reflect" "reflect"
"gorm.io/gorm/clause" "gorm.io/gorm/clause"
@ -24,6 +25,18 @@ func (n DeletedAt) Value() (driver.Value, error) {
return n.Time, nil return n.Time, nil
} }
func (n DeletedAt) MarshalJSON() ([]byte, error) {
return json.Marshal(n.Time)
}
func (n *DeletedAt) UnmarshalJSON(b []byte) error {
err := json.Unmarshal(b, &n.Time)
if err == nil {
n.Valid = true
}
return err
}
func (DeletedAt) QueryClauses(f *schema.Field) []clause.Interface { func (DeletedAt) QueryClauses(f *schema.Field) []clause.Interface {
return []clause.Interface{SoftDeleteQueryClause{Field: f}} return []clause.Interface{SoftDeleteQueryClause{Field: f}}
} }