package model import ( "encoding/json" "time" ) // Equipment 装备模型 type Equipment struct { ID string `json:"id" db:"id"` Code string `json:"code" db:"code"` Ct time.Time `json:"ct" db:"created_time"` E int `json:"e" db:"experience"` G int `json:"g" db:"grade"` L bool `json:"l" db:"locked"` Mg int `json:"mg" db:"magic"` Op []Operation `json:"op" db:"operations"` P int `json:"p" db:"power"` S string `json:"s" db:"string_value"` Sk int `json:"sk" db:"skill"` CreatedAt time.Time `json:"created_at" db:"created_at"` UpdatedAt time.Time `json:"updated_at" db:"updated_at"` } // Operation 操作属性 type Operation struct { Type string `json:"type"` Value interface{} `json:"value"` } // MarshalJSON 自定义JSON序列化 func (e *Equipment) MarshalJSON() ([]byte, error) { type Alias Equipment return json.Marshal(&struct { *Alias Ct int64 `json:"ct"` CreatedAt int64 `json:"created_at"` UpdatedAt int64 `json:"updated_at"` }{ Alias: (*Alias)(e), Ct: e.Ct.Unix(), CreatedAt: e.CreatedAt.Unix(), UpdatedAt: e.UpdatedAt.Unix(), }) } // UnmarshalJSON 自定义JSON反序列化 func (e *Equipment) UnmarshalJSON(data []byte) error { type Alias Equipment aux := &struct { *Alias Ct int64 `json:"ct"` CreatedAt int64 `json:"created_at"` UpdatedAt int64 `json:"updated_at"` }{ Alias: (*Alias)(e), } if err := json.Unmarshal(data, &aux); err != nil { return err } e.Ct = time.Unix(aux.Ct, 0) e.CreatedAt = time.Unix(aux.CreatedAt, 0) e.UpdatedAt = time.Unix(aux.UpdatedAt, 0) return nil }