github.com/bingoohuang/gg@v0.0.0-20240325092523-45da7dee9335/pkg/jsoni/jsoni.go (about)

     1  package jsoni
     2  
     3  import (
     4  	"context"
     5  	"time"
     6  	"unsafe"
     7  )
     8  
     9  // MarshalerContext is the interface implemented by types that
    10  // can marshal themselves into valid JSON with context.Context.
    11  type MarshalerContext interface {
    12  	MarshalJSONContext(context.Context) ([]byte, error)
    13  }
    14  
    15  // UnmarshalerContext is the interface implemented by types
    16  // that can unmarshal with context.Context a JSON description of themselves.
    17  type UnmarshalerContext interface {
    18  	UnmarshalJSONContext(context.Context, []byte) error
    19  }
    20  
    21  type contextKey int
    22  
    23  const (
    24  	ContextCfg contextKey = iota
    25  )
    26  
    27  // CreateTimeEncodeFn creates a time.Time encoding function on the specified layout.
    28  func CreateTimeEncodeFn(layout string) EncoderFunc {
    29  	return func(_ context.Context, p unsafe.Pointer, stream *Stream) {
    30  		stream.WriteString((*((*time.Time)(p))).Format(layout))
    31  	}
    32  }