jsontime

package module
v0.0.0-...-cf4b280 Latest Latest
Warning

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

Go to latest
Published: Jun 22, 2024 License: MIT Imports: 3 Imported by: 0

README

jsontime

Test

A json iterator extension that support custom time format.

Install

go get github.com/shufflingpixels/jsontime

or

go mod edit -require=github.com/shufflingpixels/jsontime@latest

Usage

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)

Example

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

Documentation

Index

Constants

This section is empty.

Variables

Functions

func New

func New(timeFormat string, timeLocation *time.Location) jsoniter.API

func SetDefaultTimeFormat

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

Types

type CustomTimeExtension

type CustomTimeExtension struct {
	jsoniter.DummyExtension
	Format   string
	Location *time.Location
}

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