json

package
v0.27.3 Latest Latest
Warning

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

Go to latest
Published: Jul 20, 2026 License: Apache-2.0 Imports: 14 Imported by: 0

Documentation

Overview

Package json implements an ifaces.Adapter using the standard library.

Index

Constants

This section is empty.

Variables

View Source
var ErrStdlib jsonError = "error from the JSON adapter stdlib"

ErrStdlib indicates that an error comes from the stdlib JSON adapter

Functions

func BorrowAdapterIface

func BorrowAdapterIface() ifaces.Adapter

BorrowAdapterIface borrows a stdlib Adapter and converts it directly to ifaces.Adapter.

This is useful to avoid further allocations when translating the concrete type into an interface.

func RedeemAdapter

func RedeemAdapter(a *Adapter)

RedeemAdapter redeems an Adapter to the pool, so it may be recycled.

func RedeemAdapterIface

func RedeemAdapterIface(a ifaces.Adapter)

func Register

func Register(dispatcher ifaces.Registrar, opts ...Option)

Types

type Adapter

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

func BorrowAdapter

func BorrowAdapter() *Adapter

BorrowAdapter borrows an Adapter from the pool, recycling already allocated instances.

func NewAdapter

func NewAdapter(opts ...Option) *Adapter

NewAdapter yields an ifaces.Adapter using the standard library.

func (*Adapter) Marshal

func (a *Adapter) Marshal(value any) ([]byte, error)

func (*Adapter) NewOrderedMap

func (a *Adapter) NewOrderedMap(capacity int) ifaces.OrderedMap

func (*Adapter) OrderedMarshal

func (a *Adapter) OrderedMarshal(value ifaces.Ordered) ([]byte, error)

func (*Adapter) OrderedUnmarshal

func (a *Adapter) OrderedUnmarshal(data []byte, value ifaces.SetOrdered) error

func (*Adapter) Redeem

func (a *Adapter) Redeem()

Redeem the Adapter when it comes from a pool.

The adapter becomes immediately unusable once redeemed.

func (*Adapter) Reset

func (a *Adapter) Reset()

func (*Adapter) Unmarshal

func (a *Adapter) Unmarshal(data []byte, value any) error

type MapItem

type MapItem struct {
	Key   string
	Value any
}

MapItem represents the value of a key in a JSON object held by MapSlice.

Notice that MapItem should not be marshaled to or unmarshaled from JSON directly, use this type as part of a MapSlice when dealing with JSON bytes.

type MapSlice

type MapSlice []MapItem

MapSlice represents a JSON object, with the order of keys maintained.

func (MapSlice) MarshalJSON

func (s MapSlice) MarshalJSON() ([]byte, error)

MarshalJSON renders a MapSlice as JSON bytes, preserving the order of keys.

func (MapSlice) OrderedItems

func (s MapSlice) OrderedItems() iter.Seq2[string, any]

func (MapSlice) OrderedMarshalJSON

func (s MapSlice) OrderedMarshalJSON() ([]byte, error)

func (*MapSlice) OrderedUnmarshalJSON

func (s *MapSlice) OrderedUnmarshalJSON(data []byte) error

func (*MapSlice) SetOrderedItems

func (s *MapSlice) SetOrderedItems(items iter.Seq2[string, any])

func (*MapSlice) UnmarshalJSON

func (s *MapSlice) UnmarshalJSON(data []byte) error

UnmarshalJSON builds a MapSlice from JSON bytes, preserving the order of keys.

Inner objects are unmarshaled as MapSlice slices and not map[string]any.

type Option added in v0.27.1

type Option func(o options) options

Option selects options for the stdlib adapter.

func WithMaxNestingDepth added in v0.27.1

func WithMaxNestingDepth(depth int) Option

WithMaxNestingDepth sets the maximum number of nested JSON containers accepted when marshaling or unmarshaling ordered JSON.

A value <= 0 selects the default (10,000).

This guards against stack-overflow crashes on deeply nested (possibly adversarial) JSON documents or in-memory structures.

Jump to

Keyboard shortcuts

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