2006-01-02 15:04:05
这应该是go出生的日期。
假如我要反序列化的话:
var a models.Identification
if err = json.Unmarshal(body, &a); err != nil {
log.Printf("Unmarshal err, %v\n", err)
return
}
我的model是这样子设计的:
type Identification struct {
IdDocumentType *IdDocumentType `json:"idDocumentType" validate:"required"`
IdDocumentNumber string `json:"idDocumentNumber" validate:"required"`
IssueDate time.Time `json:"issueDate" validate:"required,datetime"`
ExpiryDate time.Time `json:"expiryDate" validate:"required,datetime"`
IssuingAuthority string `json:"issuingAuthority" validate:"required"`
}
此时会报错,无法反序列化这个时间。
Unmarshal IssueDate、ExpiryDate
...as ""2006-01-02T15:04:05Z07:00"": cannot parse """ as "T"
2006-01-02 15:04:052015-03-28
解决办法:
使用时间模板,时间
// time format template(UnmarshalJSON and MarshalJSON)
const (
YYYYMMDD = "2006-01-02"
DefaultTimeFormat = "2006-01-02 15:04:05"
)
// JSONTime is time
type JSONTime time.Time
// UnmarshalJSON for JSON Time
func (t *JSONTime) UnmarshalJSON(data []byte) (err error) {
now, err := time.ParseInLocation(`"`+YYYYMMDD+`"`, string(data), time.Local)
*t = JSONTime(now)
return
}
// MarshalJSON for JSON Time
func (t JSONTime) MarshalJSON() ([]byte, error) {
b := make([]byte, 0, len(YYYYMMDD)+2)
b = append(b, '"')
b = time.Time(t).AppendFormat(b, YYYYMMDD)
b = append(b, '"')
return b, nil
}
// String for JSON Time
func (t JSONTime) String() string {
return time.Time(t).Format(YYYYMMDD)
}
model要改一下:
// Identification structure of the Customer RequestBody struct
type Identification struct {
IDDocumentType IDDocumentType `json:"idDocumentType" validate:"required"`
IDDocumentNumber string `json:"idDocumentNumber" validate:"required"`
IssueDate JSONTime `json:"issueDate" validate:"required,datetime"`
ExpiryDate JSONTime `json:"expiryDate" validate:"required,datetime"`
IssuingAuthority string `json:"issuingAuthority" validate:"required"`
}
使用Test:
package models
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestJSONTime_UnmarshalJSON(t *testing.T) {
var timeUnmarshalJSON JSONTime
err := timeUnmarshalJSON.UnmarshalJSON([]byte(`"1970-01-26"`))
assert.Nil(t, err)
}
func TestJSONTime_MarshalJSON(t *testing.T) {
var timeUnmarshalJSON JSONTime
marshalJSON, err := timeUnmarshalJSON.MarshalJSON()
assert.Nil(t, err)
assert.NotNil(t, marshalJSON)
}
func TestJSONTime_String(t *testing.T) {
var timeUnmarshalJSON JSONTime
s := timeUnmarshalJSON.String()
assert.NotNil(t, s)
}