serializer

package
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Apr 30, 2026 License: MIT Imports: 4 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Converter

type Converter interface {
	Encode(v any) (any, error)
	Decode(v any) error
}

Converter handles custom type serialization.

type ConverterFactory

type ConverterFactory func() Converter

ConverterFactory creates a custom type converter for serialization.

type NamingStrategy

type NamingStrategy int

NamingStrategy controls how struct field names are transformed during serialization.

const (
	// PascalCase leaves names as-is (Go default).
	PascalCase NamingStrategy = iota
	// CamelCase converts to lowerCamelCase.
	CamelCase
	// SnakeCase converts to snake_case.
	SnakeCase
)

type Option

type Option func(*Policy)

Option configures a Policy.

func WithCustomType

func WithCustomType(typ reflect.Type, factory ConverterFactory) Option

WithCustomType registers a custom converter for a specific type.

func WithIgnoreNil

func WithIgnoreNil() Option

WithIgnoreNil skips nil pointers during marshaling.

func WithIgnoreZero

func WithIgnoreZero() Option

WithIgnoreZero skips zero values during marshaling.

func WithNaming

func WithNaming(n NamingStrategy) Option

WithNaming sets the naming strategy.

func WithTagName

func WithTagName(tag string) Option

WithTagName sets the struct tag used for field names (default: "json").

type Policy

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

Policy defines serialization behavior including naming and custom type handling.

func New

func New(opts ...Option) *Policy

New creates a Policy with the given options.

func (*Policy) Marshal

func (p *Policy) Marshal(v any) ([]byte, error)

Marshal serializes v to JSON bytes using the policy.

func (*Policy) MarshalToString

func (p *Policy) MarshalToString(v any) (string, error)

MarshalToString serializes v to a JSON string.

func (*Policy) Unmarshal

func (p *Policy) Unmarshal(data []byte, v any) error

Unmarshal deserializes JSON bytes into v using the policy.

Jump to

Keyboard shortcuts

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