防止 json.Marshal 移除时间戳后面的零

huangapple go评论74阅读模式
英文:

prevent json.Marshal time.Time removing trailing zeros

问题

我有类似以下代码的代码:

package main

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

type Message struct {
	Time time.Time `json:"timestamp,omitempty"`
}

func main() {
	t, _ := time.Parse("2006-01-02T15:04:05.999Z07:00", "2017-05-01T15:04:05.630Z")
	msg := Message{
		Time: t,
	}
	bs, _ := json.Marshal(msg)
	fmt.Println(string(bs[:]))
}

这段代码输出:

{"timestamp":"2017-05-01T15:04:05.63Z"}

我该如何使JSON编组保留尾部的0?即输出如下所示:

{"timestamp":"2017-05-01T15:04:05.630Z"}

编辑:

这是Playground链接:https://play.golang.org/p/9p3kWeiwu2

英文:

I have code similar to the following

package main

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

type Message struct {
    Time time.Time `json:"timestamp,omitempty"`
}

func main() {
    t, _ := time.Parse("2006-01-02T15:04:05.999Z07:00", "2017-05-01T15:04:05.630Z")
    msg := Message{
	    Time: t,
    }
    bs, _ := json.Marshal(msg)
    fmt.Println(string(bs[:]))
}

This prints

{"timestamp":"2017-05-01T15:04:05.63Z"}

How can I make json marshalling keep the trailing 0? I.e., to print this?

{"timestamp":"2017-05-01T15:04:05.630Z"}

Edit:

Here's the playground https://play.golang.org/p/9p3kWeiwu2

答案1

得分: 6

time.Time始终以RFC 3339格式进行编组,仅在存在时包括亚秒精度:https://golang.org/pkg/time/#Time.MarshalJSON

您可以使用命名的time.Time类型编写自己的自定义版本,或者可以为您的结构定义自定义编组函数,或者可以使您的结构在该位置上保存一个字符串。无论哪种情况,如果您想使用相同的格式,但包括尾随零,则需要使用修改过的RFC3339Nano常量。

其值为:"2006-01-02T15:04:05.999999999Z07:00"。

末尾的9表示“包括到最右边的非零值,之后省略”。如果您将这些9更改为0,它将始终包括它们。例如,如果您始终希望毫秒精度(无论是否为非零值),则可以使用:

"2006-01-02T15:04:05.000Z07:00"

如果将其传递给time.Time值的Format()函数,您将得到所需的字符串,并可以将其包含在JSON中。

功能示例:https://play.golang.org/p/oqwnma6odw

英文:

time.Time always marshals to RFC 3339, only including sub-second precision if present: https://golang.org/pkg/time/#Time.MarshalJSON

You can write your own custom version using a named time.Time type, or you can define a custom marshal function for your structure, or you can make your structure hold a string in that place instead. In any case, if you want to use the same format, but including trailing zeros, you need to use a modified version of the RFC3339Nano constant.

Its value is: "2006-01-02T15:04:05.999999999Z07:00".

The 9's at the end mean "include until the rightmost non-zero value, omit after that". If you change those 9's to 0's, it will always include them. For example, if you always want millisecond precision (and nothing after that, regardless of whether it's non-zero or not), you would use:

"2006-01-02T15:04:05.000Z07:00"

If you feed that to Format() on your time.Time value, you'll get out the string you want, and can thus include it in the JSON.

Functioning example: https://play.golang.org/p/oqwnma6odw

huangapple
  • 本文由 发表于 2017年5月2日 23:04:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/43741286.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定