zoukankan      html  css  js  c++  java
  • golang 自定义time.Time json输出格式

    工作中使用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
    ", 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输出格式与打印方式。



  • 相关阅读:
    技术的那些事儿_2_产品与工艺
    for与foreach再探讨
    技术的那些事儿_3_西方技术管理的精髓
    搭建免费的.Net开发环境
    CDN
    servu 系统服务看门狗,自动脱机补丁,自动启动
    .NET Remoting程序开发入门篇
    网管必知远程终端3389端口合理修改秘藉
    反射方法调用时的一个错误:参数计数不匹配( parameter count mismatch )
    VPS性能测试第一步:CPU测试
  • 原文地址:https://www.cnblogs.com/xiaofengshuyu/p/5664654.html
Copyright © 2011-2022 走看看