您的位置:首页 > Web前端 > JavaScript

golang 自定义time.Time json输出格式

2016-07-12 19:44 746 查看
工作中使用golang时,遇到了一个问题。声明的struct含time.Time类型。使用json格式化struct时,time.Time被格式化成”2006-01-02T15:04:05.999999999Z07:00“格式。

代码如下

package jsontest

import (
"encoding/json"
"testing"
"time"
)

type Person struct {
Id       int64     `json:"id"`
Name     string    `json:"name"`
Birthday time.Time `json:"birthday"`
}

func TestTimeJson(t *testing.T) {
now := time.Now()
t.Log(now)
src := `{"id":5,"name":"xiaoming","birthday":"2016-06-30T16:09:51.692226358+08:00"}`
p := new(Person)
err := json.Unmarshal([]byte(src), p)
if err != nil {
t.Fatal(err)
}
t.Log(p)
t.Log(p.Birthday)
js, _ := json.Marshal(p)
t.Log(string(js))
}


golang的time.Time的默认json格式化格式叫做RFC3339。好像是一种国际标准,被推荐用作json时间的标准格式。但是android端不需要这种,而且不容易解析。

经过google,golang文档等途径,写了一个既能解决问题,又不会对源代码产生影响的解决方案。

先看一下google的解决方案

type Time struct {
time.Time
}

// returns time.Now() no matter what!
func (t *Time) UnmarshalJSON(b []byte) error {
// you can now parse b as thoroughly as you want

*t = Time{time.Now()}
return nil
}

type Config struct {
T Time
}

func main() {
c := Config{}

json.Unmarshal([]byte(`{"T": "bad-time"}`), &c)

fmt.Printf("%+v\n", c)
}


原文:http://stackoverflow.com/questions/25087960/json-unmarshal-time-that-isnt-in-rfc-3339-format

但是这样写会对原有的struct产生影响。在映射数据库时,就不行了。此时,心里一片乌云。。。

后来在看url包时,发现了系统包的一种声明数据类型的方式

type Values map[string][]string


根据这种声明方式,受到了启发,便写了一个自己的方法,如下

type Time time.Time

const (
timeFormart = "2006-01-02 15:04:05"
)

func (t *Time) UnmarshalJSON(data []byte) (err error) {
now, err := time.ParseInLocation(`"`+timeFormart+`"`, string(data), time.Local)
*t = Time(now)
return
}

func (t Time) MarshalJSON() ([]byte, error) {
b := make([]byte, 0, len(timeFormart)+2)
b = append(b, '"')
b = time.Time(t).AppendFormat(b, timeFormart)
b = append(b, '"')
return b, nil
}


同时,将Person的Birthday的类型改为Time,成功的实现的json格式化与json解析。应用到自己的项目中,不会对原有的数据库映射产生影响。需要转换类型的时候,只需Time.(xx)便可,很方便。

以为到这里便结束了。后面还有一个小坑在等我。

struct默认打印结果是将其成员完全打印出来。如把Person打印出来,便是

&{5 xiaoming {63602870991 0 0x6854a0}}


我想要的是时间,{63602870991 0 0x6854a0} 是个什么。后来发现,自己的Time类型相当于继承了time.TIme的成员。没有像java一样继承方法。调用了struct默认打印方式。golang有没有类似于java的toString方法呢。

当然有,而且很简单

func (t Time) String() string {
return time.Time(t).Format(timeFormart)
}


这样,就实现了更改打印输出方式

&{5 xiaoming 2016-06-30 16:09:51}


最后,把全部代码贴出

package jsontest

import (
"encoding/json"
"testing"
"time"
)

type Time time.Time const ( timeFormart = "2006-01-02 15:04:05" ) func (t *Time) UnmarshalJSON(data []byte) (err error) { now, err := time.ParseInLocation(`"`+timeFormart+`"`, string(data), time.Local) *t = Time(now) return } func (t Time) MarshalJSON() ([]byte, error) { b := make([]byte, 0, len(timeFormart)+2) b = append(b, '"') b = time.Time(t).AppendFormat(b, timeFormart) b = append(b, '"') return b, nil }

func (t Time) String() string { return time.Time(t).Format(timeFormart) }
type Person struct {
Id int64 `json:"id"`
Name string `json:"name"`
Birthday Time `json:"birthday"`
}

func TestTimeJson(t *testing.T) {
now := Time(time.Now())
t.Log(now)
src := `{"id":5,"name":"xiaoming","birthday":"2016-06-30 16:09:51"}`
p := new(Person)
err := json.Unmarshal([]byte(src), p)
if err != nil {
t.Fatal(err)
}
t.Log(p)
t.Log(time.Time(p.Birthday))
js, _ := json.Marshal(p)
t.Log(string(js))
}


由此,可以对任意struct增加 UnmarshalJSON , MarshalJSON , String 方法,实现自定义json输出格式与打印方式。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: