jsontime

package module
v2.0.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 25, 2019 License: MIT Imports: 3 Imported by: 0

README

jsontime

A json iterator extension that support custom time format.

Usage

100% compatibility with standard lib

Replace

import "encoding/json"

json.Marshal(&data)
json.Unmarshal(input, &data)

with

import "github.com/liamylian/jsontime"

var json = jsontime.ConfigWithCustomTimeFormat

json.Marshal(&data)
json.Unmarshal(input, &data)

Example

package main

import (
	"fmt"
	"time"

	"github.com/liamylian/jsontime"
)

var json = jsontime.ConfigWithCustomTimeFormat

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)
}

Advanced Usage

var json = jsontime.ConfigWithCustomTimeFormat
timeZoneShanghai, _ := time.LoadLocation("Asia/Shanghai")
jsontime.AddTimeFormatAlias("sql_datetime", "2006-01-02 15:04:05")
jsontime.AddLocaleAlias("shanghai", timeZoneShanghai)

type Book struct {
	Id          int        `json:"id"`
	PublishedAt time.Time  `json:"published_at" time_format:"sql_datetime" time_location:"shanghai"`
	UpdatedAt   *time.Time `json:"updated_at" time_format:"sql_datetime" time_location:"shanghai"`
	CreatedAt   time.Time  `json:"created_at" time_format:"sql_datetime" time_location:"shanghai"`
}

bytes, _ := json.Marshal(book)

Notice

Because there are two maps in the package, that is not thread safe. So it is suggested to call AddTimeFormatAlias, AddLocaleAlias only once, right before calling Marshal and UnMarshal. Or you should make sure not to call them at the same time (writing and reading map at the same time will panic).

Documentation

Index

Constants

View Source
const (
	ANSIC       = "ANSIC"
	UnixDate    = "UnixDate"
	RubyDate    = "RubyDate"
	RFC822      = "RFC822"
	RFC822Z     = "RFC822Z"
	RFC850      = "RFC850"
	RFC1123     = "RFC1123"
	RFC1123Z    = "RFC1123Z"
	RFC3339     = "RFC3339"
	RFC3339Nano = "RFC3339Nano"
	Kitchen     = "Kitchen"
	Stamp       = "Stamp"
	StampMilli  = "StampMilli"
	StampMicro  = "StampMicro"
	StampNano   = "StampNano"
)

time format alias

View Source
const (
	Local = "Local"
	UTC   = "UTC"
)

time zone alias

Variables

View Source
var ConfigWithCustomTimeFormat = jsoniter.ConfigCompatibleWithStandardLibrary

Functions

func AddLocaleAlias

func AddLocaleAlias(alias string, locale *time.Location)

func AddTimeFormatAlias

func AddTimeFormatAlias(alias, format string)

func SetDefaultTimeFormat

func SetDefaultTimeFormat(timeFormat string, timeLocation *time.Location)

Types

type CustomTimeExtension

type CustomTimeExtension struct {
	jsoniter.DummyExtension
}

func (*CustomTimeExtension) UpdateStructDescriptor

func (extension *CustomTimeExtension) UpdateStructDescriptor(structDescriptor *jsoniter.StructDescriptor)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL