Golang JSON omitempty随着time.Time Field [英] Golang JSON omitempty With time.Time Field

查看:817
本文介绍了Golang JSON omitempty随着time.Time Field的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

试图对包含2个时间字段的结构进行json元帅。但是,如果它有时间价值,我只希望现场通过。所以我使用 json:,omitempty但它不起作用。

我可以设置日期价值如此json.Marshal会把它当作一个空的(零)价值,并不包括它在json字符串?



Playground: http://play.golang.org/p/QJwh7yBJlo



实际成果:


{Timestamp:2015-09-18T00:00:00Z,Date :0001-01-01T00:00:00Z}


期望的结果:


{Timestamp:2015-09-18T00:00:00Z}

代码:

 包主

import(
encoding / json
fmt
time


类型MyStruct结构{
时间戳记时间.json:,omitempty`
Date time.Time`json:,omitempty`
字段字符串`json:,omitempt y`
}

func main(){
ms:= MyStruct {
Timestamp:time.Date(2015,9,18,0, 0,0,time.UTC),
字段:,
}

bb,err:= json.Marshal(ms)
if err!= nil {
panic(err)
}
fmt.Println(string(bb))
}


解决方案

omitempty 标记选项不适用于时间.Time ,因为它是 struct 。结构有一个零值,但这是一个结构值,其中所有字段都有其零值。这是一个有效的值,所以它不被视为空。



但是通过简单地将它改为一个指针: * time .Time ,它会工作( nil 指针被视为空用于json封送/解组)。因此,无需编写自定义 Marshaler 在这种情况下:

  type MyStruct struct {
Timestamp * time.Time`json:,omitempty `
Date * time.Time`json:,omitempty`
字段字符串`json:,omitempty`
}

使用它:

  ts:= time.Date 2015,9,18,0,0,0,time.UTC)
ms:= MyStruct {
时间戳:& ts,
字段:,
}

输出(根据需要):

  {Timestamp:2015-09-18T00:00:00Z} 

去游乐场试试它。



如果您不能或不想将其更改为指针,您仍然可以通过实现自定义的 Marshaler Unmarshaler 。如果你这样做,你可以使用 Time.IsZero() 方法来决定 time.Time 值是否为零值。


Trying to json Marshal a struct that contains 2 time fields. But I only want the field to come through if it has a time value. So I'm using json:",omitempty" but it's not working.

What can I set the Date value to so json.Marshal will treat it like an empty (zero) value and not include it in the json string?

Playground: http://play.golang.org/p/QJwh7yBJlo

Actual Outcome:

{"Timestamp":"2015-09-18T00:00:00Z","Date":"0001-01-01T00:00:00Z"}

Desired Outcome:

{"Timestamp":"2015-09-18T00:00:00Z"}

Code:

package main

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

type MyStruct struct {
    Timestamp time.Time `json:",omitempty"`
    Date  time.Time `json:",omitempty"`
    Field   string    `json:",omitempty"`
}

func main() {
    ms := MyStruct{
        Timestamp: time.Date(2015, 9, 18, 0, 0, 0, 0, time.UTC),
        Field:   "",
    }

    bb, err := json.Marshal(ms)
    if err != nil {
        panic(err)
    }
    fmt.Println(string(bb))
}

解决方案

The omitempty tag option does not work with time.Time as it is a struct. There is a "zero" value for structs, but that is a struct value where all fields have their zero values. This is a "valid" value, so it is not treated as "empty".

But by simply changing it to a pointer: *time.Time, it will work (nil pointers are treated as "empty" for json marshaling/unmarshaling). So no need to write custom Marshaler in this case:

type MyStruct struct {
    Timestamp *time.Time `json:",omitempty"`
    Date      *time.Time `json:",omitempty"`
    Field     string     `json:",omitempty"`
}

Using it:

ts := time.Date(2015, 9, 18, 0, 0, 0, 0, time.UTC)
ms := MyStruct{
    Timestamp: &ts,
    Field:     "",
}

Output (as desired):

{"Timestamp":"2015-09-18T00:00:00Z"}

Try it on the Go Playground.

If you can't or don't want to change it to a pointer, you can still achieve what you want by implementing a custom Marshaler and Unmarshaler. If you do so, you can use the Time.IsZero() method to decide if a time.Time value is the zero value.

这篇关于Golang JSON omitempty随着time.Time Field的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆