From 689d6e23319ea84c07b4943341361bd0ea09b780 Mon Sep 17 00:00:00 2001 From: Jinzhu Date: Tue, 13 Oct 2020 14:12:03 +0800 Subject: [PATCH] Fix DeletedAt marshalling, close #3598 --- soft_delete.go | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/soft_delete.go b/soft_delete.go index b13fc63f..b15a8148 100644 --- a/soft_delete.go +++ b/soft_delete.go @@ -3,6 +3,7 @@ package gorm import ( "database/sql" "database/sql/driver" + "encoding/json" "reflect" "gorm.io/gorm/clause" @@ -24,6 +25,18 @@ func (n DeletedAt) Value() (driver.Value, error) { 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 { return []clause.Interface{SoftDeleteQueryClause{Field: f}} }