json

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: May 3, 2026 License: MIT Imports: 11 Imported by: 0

Documentation

Overview

Package json loads JSON configuration from bytes, files, or URLs into maps and structs, with optional key normalization and multi-source merging.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Codec

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

Codec loads JSON from multiple sources.

func New

func New(opts ...Option) *Codec

New creates a JSON codec.

func (*Codec) Map

func (c *Codec) Map(ctx context.Context) (map[string]any, error)

Map merges all JSON sources into one map.

func (*Codec) Marshal

func (c *Codec) Marshal(src any) ([]byte, error)

Marshal encodes src to JSON with indentation (struct or map[string]any).

func (*Codec) Unmarshal

func (c *Codec) Unmarshal(dst any) error

Unmarshal decodes using context.Background.

func (*Codec) UnmarshalContext

func (c *Codec) UnmarshalContext(ctx context.Context, dst any) error

UnmarshalContext decodes merged JSON into dst.

func (*Codec) WriteTo

func (c *Codec) WriteTo(w io.Writer, src any) (int64, error)

WriteTo writes JSON to w.

type Option

type Option func(*Codec)

Option configures a Codec.

func WithBytes

func WithBytes(b []byte) Option

WithBytes appends JSON from bytes.

func WithDecodeHook

func WithDecodeHook(h mapstructure.DecodeHookFunc) Option

WithDecodeHook appends a decode hook.

func WithFile

func WithFile(path string) Option

WithFile appends a JSON file path.

func WithHTTPClient

func WithHTTPClient(client *http.Client) Option

WithHTTPClient sets the client for subsequent WithURL sources.

func WithHTTPHeader

func WithHTTPHeader(k, v string) Option

WithHTTPHeader adds a header for subsequent WithURL sources.

func WithKeyNormalizer

func WithKeyNormalizer(n keymap.Normalizer) Option

WithKeyNormalizer sets key normalizer after merge (nil disables).

func WithReader

func WithReader(r io.Reader) Option

WithReader appends JSON from r.

func WithSliceMerge

func WithSliceMerge(s merge.SliceStrategy) Option

WithSliceMerge sets slice merge strategy across sources.

func WithTagName

func WithTagName(name string) Option

WithTagName sets mapstructure tag name.

func WithTrim

func WithTrim(enable bool) Option

WithTrim toggles string trim on struct decode.

func WithURL

func WithURL(raw string) Option

WithURL appends JSON from an HTTP(S) URL.

func WithWeaklyTyped

func WithWeaklyTyped(enable bool) Option

WithWeaklyTyped toggles weak typing for struct decode.

Jump to

Keyboard shortcuts

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