Override the layout used by json.Marshal to format time. Time

In Golang, is there a way to make the general encoding/json marshal use a different layout when marshaling the time.Time fields?

Basically, I have this structure:

 s := {"starttime":time.Now(), "name":"ali"} 

and I want to encode json using the encdoding/json Marshal function, but I want to use my own layout, I think somewhere time.Format(layout) is called, I want to control this layout,

+7
json time go marshalling
source share
3 answers

As Zeebo inspired the answer and hashed in the comments on this answer:

http://play.golang.org/p/pUCBUgrjZC

 package main import "fmt" import "time" import "encoding/json" type jsonTime struct { time.Time f string } func (j jsonTime) format() string { return j.Time.Format(jf) } func (j jsonTime) MarshalText() ([]byte, error) { return []byte(j.format()), nil } func (j jsonTime) MarshalJSON() ([]byte, error) { return []byte(`"` + j.format() + `"`), nil } func main() { jt := jsonTime{time.Now(), time.Kitchen} if jt.Before(time.Now().AddDate(0, 0, 1)) { // 1 x := map[string]interface{}{ "foo": jt, "bar": "baz", } data, err := json.Marshal(x) if err != nil { panic(err) } fmt.Printf("%s", data) } } 

This solution inserts time. Time to jsonTime structure. Embedding supports all the time. Temporary methods for the jsonTime structure that allow you to use them without explicit type conversion (see // 1).

Embedding time. Time also has a way to advance the MarshalJSON method, which encodes the / json encoding code for priorities higher than the MarshalText method for backward compatibility reasons ( MarshalText was added in Go 1.2 , MarshalJSON precedes this). As a result, the default format time.Time is used instead of the custom format provided by MarshalText.

To overcome this problem, we will redefine MarshalJSON for structure jsonTime.

+10
source share

Maybe something like this will work for you?

 package main import "fmt" import "time" import "encoding/json" type jsonTime struct { t time.Time f string } func (j jsonTime) MarshalText() ([]byte, error) { return []byte(jtFormat(jf)), nil } func main() { x := map[string]interface{}{ "foo": jsonTime{t: time.Now(), f: time.Kitchen}, "bar": "baz", } data, err := json.Marshal(x) if err != nil { panic(err) } fmt.Printf("%s", data) } 

also available here: http://play.golang.org/p/D1kq5KrXQZ

Just create a custom type that implements MarshalText the way you want it to display.

+6
source share

First, I highly recommend using a time format other than the default RFC3339. This is a good time format and can be analyzed in any number of languages, so if you do not need a different format because it needs a different API, it is better to use it by default.

But I had to solve this problem by using other people's APIs, so here is one solution that takes the bulk of the work to the Marshal / Unmarshal step and leaves you with the perfect structure: http://play.golang.org/p/DKaTbV2Zvl

+3
source share

All Articles