A json iterator extension that support custom time format.
go get github.com/shufflingpixels/jsontime
or
go mod edit -require=github.com/shufflingpixels/jsontime@latest
100% compatibility with standard lib
Replace
import "encoding/json"
json.Marshal(&data)
json.Unmarshal(input, &data)
with
import "github.com/shufflingpixels/jsontime-go"
var json = jsontime.ConfigWithCustomTimeFormat
json.Marshal(&data)
json.Unmarshal(input, &data)
package main
import (
"fmt"
"time"
"github.com/shufflingpixels/jsontime-go"
)
var json = jsontime.Default
func init() {
jsontime.SetDefaultTimeFormat(time.RFC3339, time.Local)
}
type Book struct {
Id int `json:"id"`
PublishedAt time.Time `json:"published_at"`
UpdatedAt *time.Time `json:"updated_at"`
CreatedAt time.Time `json:"created_at"`
}
func main() {
book := Book{
Id: 1,
PublishedAt: time.Now(),
UpdatedAt: nil,
CreatedAt: time.Now(),
}
bytes, _ := json.Marshal(book)
fmt.Printf("%s", bytes)
}