json

package
v0.28.0 Latest Latest
Warning

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

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

Documentation

Overview

Package json implements an ifaces.Adapter using github.com/mailru/easyjson.

Index

Constants

This section is empty.

Variables

View Source
var ErrMaxNestingDepth = errors.New("maximum JSON nesting depth exceeded")

ErrMaxNestingDepth is returned when a JSON document or in-memory structure nests deeper than the configured maximum (see WithMaxNestingDepth). It guards against stack-overflow crashes on adversarially deep input.

Functions

func BorrowAdapterIface

func BorrowAdapterIface() ifaces.Adapter

func BorrowLexer

func BorrowLexer(data []byte) (*jlexer.Lexer, func())

BorrowLexer borrows a jlexer.Lexer from the pool, recycling already allocated instances.

func BorrowWriter

func BorrowWriter() (*jwriter.Writer, func())

BorrowWriter borrows a jwriter.Writer from the pool, recycling already allocated instances.

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)

Register the easyjson implementation of a ifaces.Adapter to the an ifaces.Registrar, e.g. the global registry github.com/go-openapi/swag/jsonutils/adapters.Registry.

Register calls ifaces.Registrar.RegisterFor.

Some optional features proposed by the jwriter.Writer and jlexer.Lexer are available. See 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 a JSON adapter for easyjson.

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()

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.

func (MapItem) MarshalEasyJSON

func (s MapItem) MarshalEasyJSON(w *jwriter.Writer)

MarshalEasyJSON renders a MapItem as JSON bytes, using easyJSON

func (*MapItem) UnmarshalEasyJSON

func (s *MapItem) UnmarshalEasyJSON(in *jlexer.Lexer)

UnmarshalEasyJSON builds a MapItem from JSON bytes, using easyJSON

type MapSlice

type MapSlice []MapItem

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

It implements ifaces.Ordered and ifaces.SetOrdered.

func (MapSlice) MarshalEasyJSON

func (s MapSlice) MarshalEasyJSON(w *jwriter.Writer)

MarshalEasyJSON renders a MapSlice as JSON bytes, using easyJSON

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) UnmarshalEasyJSON

func (s *MapSlice) UnmarshalEasyJSON(in *jlexer.Lexer)

UnmarshalEasyJSON builds a MapSlice from JSON bytes, using easyJSON

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

type Option func(o options) options

Option selects options for the easyjson adapter.

func WithLexerUseMultipleErrors

func WithLexerUseMultipleErrors(enabled bool) Option

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.

func WithWriterNilMapAsEmpty

func WithWriterNilMapAsEmpty(enabled bool) Option

func WithWriterNilSliceAsEmpty

func WithWriterNilSliceAsEmpty(enabled bool) Option

func WithWriterNoEscapeHTML

func WithWriterNoEscapeHTML(noescape bool) Option

Jump to

Keyboard shortcuts

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