3

go中的时间序列化/反序列化的问题

 3 years ago
source link: http://www.lzhpo.com/article/141
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

go中的时间序列化/反序列化的问题

标准时间模板:2006-01-02 15:04:05

这应该是go出生的日期。

假如我要反序列化的话:

var a models.Identificationif 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:05去反序列化,我传入的时间是2015-03-28这种格式,所以会转换出错。

解决办法:

使用时间模板,时间

// time format template(UnmarshalJSON and MarshalJSON)const (    YYYYMMDD          = "2006-01-02"    DefaultTimeFormat = "2006-01-02 15:04:05")// JSONTime is timetype JSONTime time.Time// UnmarshalJSON for JSON Timefunc (t *JSONTime) UnmarshalJSON(data []byte) (err error) {    now, err := time.ParseInLocation(`"`+YYYYMMDD+`"`, string(data), time.Local)    *t = JSONTime(now)    return}// MarshalJSON for JSON Timefunc (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 Timefunc (t JSONTime) String() string {    return time.Time(t).Format(YYYYMMDD)}

model要改一下:

// Identification structure of the Customer RequestBody structtype 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 modelsimport (    "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)}
正文到此结束
所属分类:Go开发

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK