json

package
v0.9.0 Latest Latest
Warning

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

Go to latest
Published: Mar 24, 2026 License: Apache-2.0 Imports: 13 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Marshal added in v0.9.0

func Marshal(value interface{}, opts ...Option) ([]byte, error)

Marshal marshals using context.Background unless overridden by options.

func MarshalContext added in v0.9.0

func MarshalContext(ctx context.Context, value interface{}, opts ...Option) ([]byte, error)

MarshalContext marshals with an explicit context.

func Unmarshal added in v0.9.0

func Unmarshal(data []byte, dest interface{}, opts ...Option) error

Unmarshal unmarshals using context.Background unless overridden by options.

func UnmarshalContext added in v0.9.0

func UnmarshalContext(ctx context.Context, data []byte, dest interface{}, opts ...Option) error

UnmarshalContext unmarshals with an explicit context.

Types

type Codec added in v0.9.0

type Codec interface {
	String(*string) error
	Int(*int) error
	Int8(*int8) error
	Int16(*int16) error
	Int32(*int32) error
	Int64(*int64) error
	Uint(*uint) error
	Uint8(*uint8) error
	Uint16(*uint16) error
	Uint32(*uint32) error
	Uint64(*uint64) error
	Float32(*float32) error
	Float64(*float64) error
	Bool(*bool) error
	Interface(*interface{}) error
	Object(UnmarshalerObject) error
	Array(UnmarshalerArray) error

	AddString(string)
	AddInt(int)
	AddInt8(int8)
	AddInt16(int16)
	AddInt32(int32)
	AddInt64(int64)
	AddUint(uint)
	AddUint8(uint8)
	AddUint16(uint16)
	AddUint32(uint32)
	AddUint64(uint64)
	AddFloat32(float32)
	AddFloat64(float64)
	AddBool(bool)
	AddNull()
	AddInterface(interface{}) error
	AddObject(MarshalerObject)
	AddArray(MarshalerArray)
}

Codec is a unified encoder/decoder contract.

type Decoder added in v0.9.0

type Decoder interface {
	String(*string) error
	Int(*int) error
	Int8(*int8) error
	Int16(*int16) error
	Int32(*int32) error
	Int64(*int64) error
	Uint(*uint) error
	Uint8(*uint8) error
	Uint16(*uint16) error
	Uint32(*uint32) error
	Uint64(*uint64) error
	Float32(*float32) error
	Float64(*float64) error
	Bool(*bool) error
	Interface(*interface{}) error
	Object(UnmarshalerJSONObject) error
	Array(UnmarshalerJSONArray) error
}

Decoder defines decode-side methods used by generic unmarshalers.

type DuplicateKeyPolicy added in v0.9.0

type DuplicateKeyPolicy int

DuplicateKeyPolicy controls duplicate object key behavior.

const (
	LastWins DuplicateKeyPolicy = iota
	ErrorOnDuplicate
)

type FieldExcluder added in v0.9.0

type FieldExcluder interface {
	Exclude(path, fieldName string) bool
}

FieldExcluder decides whether a field should be excluded.

type MalformedPolicy added in v0.9.0

type MalformedPolicy int

MalformedPolicy controls malformed JSON tolerance.

const (
	Tolerant MalformedPolicy = iota
	FailFast
)

type MarshalInterceptor added in v0.9.0

type MarshalInterceptor func() ([]byte, error)

type MarshalInterceptors added in v0.9.0

type MarshalInterceptors map[string]MarshalInterceptor

type MarshalSession added in v0.9.0

type MarshalSession struct {
	Buffer       *bytes.Buffer
	Options      Options
	Interceptors MarshalInterceptors
	// contains filtered or unexported fields
}

func NewMarshalSession added in v0.9.0

func NewMarshalSession(options Options) *MarshalSession

func (*MarshalSession) PathRef added in v0.9.0

func (s *MarshalSession) PathRef() PathRef

func (*MarshalSession) PopPath added in v0.9.0

func (s *MarshalSession) PopPath()

func (*MarshalSession) PushField added in v0.9.0

func (s *MarshalSession) PushField(name string)

func (*MarshalSession) PushIndex added in v0.9.0

func (s *MarshalSession) PushIndex(index int)

func (*MarshalSession) Release added in v0.9.0

func (s *MarshalSession) Release()

type MarshalerArray added in v0.9.0

type MarshalerArray interface {
	MarshalArray(context.Context, Codec)
	IsNil() bool
}

MarshalerArray emits array elements.

type MarshalerObject added in v0.9.0

type MarshalerObject interface {
	MarshalObject(context.Context, Codec)
	IsNil() bool
}

MarshalerObject emits object fields.

type Mode added in v0.9.0

type Mode int

Mode controls compatibility vs strict behavior.

const (
	ModeCompat Mode = iota
	ModeStrict
)

type NameTransformer added in v0.9.0

type NameTransformer interface {
	Transform(path, fieldName string) string
}

NameTransformer transforms field names for output and path display.

type NilSlicePolicy added in v0.9.0

type NilSlicePolicy int

NilSlicePolicy controls marshal output for nil slices.

const (
	NilSliceAsNull NilSlicePolicy = iota
	NilSliceAsEmptyArray
)

type NullPolicy added in v0.9.0

type NullPolicy int

NullPolicy controls null assignment behavior.

const (
	CompatNulls NullPolicy = iota
	StrictNulls
)

type NumberPolicy added in v0.9.0

type NumberPolicy int

NumberPolicy controls numeric coercion behavior.

const (
	CoerceNumbers NumberPolicy = iota
	ExactNumbers
)

type Option added in v0.9.0

type Option interface {
	// contains filtered or unexported methods
}

Option mutates runtime options.

func WithCaseFormat added in v0.9.0

func WithCaseFormat(caseFormat text.CaseFormat) Option

func WithContext added in v0.9.0

func WithContext(ctx context.Context) Option

func WithDebugPathSink added in v0.9.0

func WithDebugPathSink(sink func(PathRef)) Option

func WithDuplicateKeyPolicy added in v0.9.0

func WithDuplicateKeyPolicy(policy DuplicateKeyPolicy) Option

func WithFieldExcluder added in v0.9.0

func WithFieldExcluder(excluder FieldExcluder) Option

func WithFieldUnmarshalHook added in v0.9.0

func WithFieldUnmarshalHook(hook func(ctx context.Context, holder unsafe.Pointer, field string, value any) (any, error)) Option

func WithFormatTag added in v0.9.0

func WithFormatTag(tag *format.Tag) Option

func WithMalformedPolicy added in v0.9.0

func WithMalformedPolicy(policy MalformedPolicy) Option

func WithMode added in v0.9.0

func WithMode(mode Mode) Option

func WithNameTransformer added in v0.9.0

func WithNameTransformer(transformer NameTransformer) Option

func WithNilSlicePolicy added in v0.9.0

func WithNilSlicePolicy(policy NilSlicePolicy) Option

func WithNullPolicy added in v0.9.0

func WithNullPolicy(policy NullPolicy) Option

func WithNumberPolicy added in v0.9.0

func WithNumberPolicy(policy NumberPolicy) Option

func WithOmitEmpty added in v0.9.0

func WithOmitEmpty(enabled bool) Option

func WithPathFieldExcluder added in v0.9.0

func WithPathFieldExcluder(excluder PathFieldExcluder) Option

func WithPathNameTransformer added in v0.9.0

func WithPathNameTransformer(transformer PathNameTransformer) Option

func WithPathTracking added in v0.9.0

func WithPathTracking(mode PathTrackingMode) Option

func WithPathUnmarshalHook added in v0.9.0

func WithPathUnmarshalHook(hook func(ctx context.Context, holder unsafe.Pointer, path []string, field string, value any) (any, error)) Option

func WithScannerHooks added in v0.9.0

func WithScannerHooks(hooks ScannerHooks) Option

func WithUnknownFieldPolicy added in v0.9.0

func WithUnknownFieldPolicy(policy UnknownFieldPolicy) Option

type Options added in v0.9.0

type Options struct {
	Ctx context.Context

	Mode               Mode
	UnknownFieldPolicy UnknownFieldPolicy
	NumberPolicy       NumberPolicy
	NullPolicy         NullPolicy
	DuplicateKeyPolicy DuplicateKeyPolicy
	MalformedPolicy    MalformedPolicy
	PathTracking       PathTrackingMode

	CaseFormat         text.CaseFormat
	FormatTag          *format.Tag
	TimeLayout         string
	NameTransformer    NameTransformer
	FieldExcluder      FieldExcluder
	PathName           PathNameTransformer
	PathExcluder       PathFieldExcluder
	DebugPathSink      func(PathRef)
	FieldUnmarshalHook func(ctx context.Context, holder unsafe.Pointer, field string, value any) (any, error)
	PathUnmarshalHook  func(ctx context.Context, holder unsafe.Pointer, path []string, field string, value any) (any, error)

	OmitEmpty      bool
	NilSlicePolicy NilSlicePolicy
	// contains filtered or unexported fields
}

Options defines runtime behavior.

type PathFieldExcluder added in v0.9.0

type PathFieldExcluder interface {
	ExcludePath(path []string, fieldName string) bool
}

PathFieldExcluder excludes fields using path segments without forcing path string joins.

type PathNameTransformer added in v0.9.0

type PathNameTransformer interface {
	TransformPath(path []string, fieldName string) string
}

PathNameTransformer transforms names using path segments without forcing path string joins.

type PathRef added in v0.9.0

type PathRef struct {
	// contains filtered or unexported fields
}

PathRef references path segments without eager string allocation.

func (PathRef) At added in v0.9.0

func (p PathRef) At(i int) (PathSegment, bool)

func (PathRef) Len added in v0.9.0

func (p PathRef) Len() int

func (PathRef) Segments added in v0.9.0

func (p PathRef) Segments() []PathSegment

type PathSegment added in v0.9.0

type PathSegment struct {
	Field string
	Index int
	Kind  SegmentKind
}

PathSegment describes one path component.

type PathTrackingMode added in v0.9.0

type PathTrackingMode int

PathTrackingMode controls path tracking overhead.

const (
	PathTrackingOff PathTrackingMode = iota
	PathTrackingErrorsOnly
	PathTrackingFull
)

type ScannerHooks added in v0.9.0

type ScannerHooks interface {
	SkipWhitespace(data []byte, pos int) int
	FindQuoteOrEscape(data []byte, pos int) (quotePos int, escapePos int)
	FindStructural(data []byte, pos int) int
}

ScannerHooks contains block-scan hooks for decoder whitespace and token scans.

type SegmentKind added in v0.9.0

type SegmentKind int

SegmentKind identifies path segment type.

const (
	SegmentField SegmentKind = iota
	SegmentIndex
)

type UnknownFieldPolicy added in v0.9.0

type UnknownFieldPolicy int

UnknownFieldPolicy controls unknown key handling.

const (
	IgnoreUnknown UnknownFieldPolicy = iota
	ErrorOnUnknown
)

type UnmarshalInterceptor added in v0.9.0

type UnmarshalInterceptor func(dst interface{}, codec Codec, options ...interface{}) error

type UnmarshalInterceptors added in v0.9.0

type UnmarshalInterceptors map[string]UnmarshalInterceptor

type UnmarshalSession added in v0.9.0

type UnmarshalSession struct {
	Options      Options
	Interceptors UnmarshalInterceptors
	// contains filtered or unexported fields
}

func NewUnmarshalSession added in v0.9.0

func NewUnmarshalSession(options Options) *UnmarshalSession

func (*UnmarshalSession) PathRef added in v0.9.0

func (s *UnmarshalSession) PathRef() PathRef

func (*UnmarshalSession) PopPath added in v0.9.0

func (s *UnmarshalSession) PopPath()

func (*UnmarshalSession) PushField added in v0.9.0

func (s *UnmarshalSession) PushField(name string)

func (*UnmarshalSession) PushIndex added in v0.9.0

func (s *UnmarshalSession) PushIndex(index int)

type UnmarshalerArray added in v0.9.0

type UnmarshalerArray interface {
	UnmarshalArray(context.Context, Codec) error
}

UnmarshalerArray receives array elements.

type UnmarshalerJSONArray added in v0.9.0

type UnmarshalerJSONArray interface {
	UnmarshalJSONArray(context.Context, Decoder) error
}

UnmarshalerJSONArray receives array elements.

type UnmarshalerJSONObject added in v0.9.0

type UnmarshalerJSONObject interface {
	UnmarshalJSONObject(context.Context, Decoder, string) error
	NKeys() int
}

UnmarshalerObject receives object fields.

type UnmarshalerObject added in v0.9.0

type UnmarshalerObject interface {
	UnmarshalObject(context.Context, Codec, string) error
	NKeys() int
}

UnmarshalerObject is backward-compatible alias style interface.

Directories

Path Synopsis
internal
lru

Jump to

Keyboard shortcuts

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