json

package
v0.10.0 Latest Latest
Warning

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

Go to latest
Published: Sep 14, 2026 License: Apache-2.0 Imports: 17 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 MarshalStandard added in v0.10.0

func MarshalStandard(value any, opts ...Option) ([]byte, error)

MarshalStandard applies field exclusion while retaining encoding/json v1 field dominance, tags and value encoding. Custom value encoders own their complete value. Formatting transforms belong to MarshalContext instead.

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 FieldFilter added in v0.10.0

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

func NewFieldFilter added in v0.10.0

func NewFieldFilter(t reflect.Type, selections []FieldSelection) (*FieldFilter, error)

func (*FieldFilter) AffectsIndexes added in v0.10.0

func (f *FieldFilter) AffectsIndexes(index []int) bool

AffectsIndexes bounds presentation compilation to the selected holder graph. Fully included branches retain their original types and custom codec methods.

func (*FieldFilter) ExcludeField added in v0.10.0

func (f *FieldFilter) ExcludeField(path []string, index []int) bool

ExcludeField retains the exact JSON field index when promoted fields share a Go name. Standard encoding consumes this alongside PathFieldExcluder.

func (*FieldFilter) ExcludeIndexes added in v0.10.0

func (f *FieldFilter) ExcludeIndexes(index []int) bool

ExcludeIndexes applies the selection to a canonical structural field index. Container elements do not add indexes. The longest holder scope wins, so two occurrences of one row type remain independent. Anonymous holder prefixes stay present when they contain a selected promoted field.

func (*FieldFilter) ExcludePath added in v0.10.0

func (f *FieldFilter) ExcludePath(path []string, field string) bool

func (*FieldFilter) WithOptions added in v0.10.0

func (f *FieldFilter) WithOptions(opts ...Option) (*FieldFilter, error)

WithOptions resolves parent paths using the transformed encoder's tag and naming authority. The original standard filter remains immutable.

type FieldSelection added in v0.10.0

type FieldSelection struct {
	Path    []string
	Fields  []string
	Indexes [][]int
}

FieldSelection includes canonical Go fields at a canonical Go holder path. An empty Fields list selects no properties at that object. Omitted scopes leave their objects unrestricted. Selection never examines row values.

type IndexedPathFieldExcluder added in v0.10.0

type IndexedPathFieldExcluder interface {
	PathFieldExcluder
	ExcludeField(path []string, index []int) bool
}

IndexedPathFieldExcluder optionally supplies exact promoted-field identity to standard encoding. It uses the same parent wire paths as PathFieldExcluder.

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 Marshaller added in v0.10.0

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

Marshaller retains one configured native engine for encoding and metadata. Options and callbacks must be immutable after construction.

func NewMarshaller added in v0.10.0

func NewMarshaller(typeOf reflect.Type, opts ...Option) (*Marshaller, error)

func (*Marshaller) Marshal added in v0.10.0

func (m *Marshaller) Marshal(value interface{}) ([]byte, error)

func (*Marshaller) Wire added in v0.10.0

func (m *Marshaller) Wire() (*jsonmarshal.WireShape, error)

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 WithExcludedFields added in v0.10.0

func WithExcludedFields(paths ...string) Option

WithExcludedFields resolves canonical Go-field paths using the native marshal plans, including field aliases, casing and inline holders.

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 {
	// ExcludedFields contains canonical Go-field paths for marshal projection.
	ExcludedFields []string
	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