decoder

package
v0.11.2 Latest Latest
Warning

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

Go to latest
Published: Sep 30, 2026 License: MIT Imports: 23 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	EmptyInterfaceType = emptyInterfaceType
)

Functions

func AssignValue added in v0.10.0

func AssignValue(src, dst reflect.Value) error

func NewInput added in v0.11.0

func NewInput(data []byte) []byte

NewInput returns the buffer which the decoders decode: a copy of the input followed by a nul byte, which ends every scan.

func ReleaseRuntimeContext

func ReleaseRuntimeContext(ctx *RuntimeContext)

func StreamOffsetBase added in v0.11.0

func StreamOffsetBase(_, valueStart int64) int64

StreamOffsetBase returns the position which the offsets of the type errors of a value of a stream are relative to, of the end of the previous value and of the start of the value: encoding/json of Go 1.27 reports them from the start of the value.

func Valid added in v0.11.0

func Valid(buf []byte) bool

Valid reports whether buf, the input followed by the nul byte, is a JSON value, by the grammar which the values which are not decoded are skipped by.

func ValidateEnd added in v0.11.0

func ValidateEnd(buf []byte, cursor int64) error

ValidateEnd returns the syntax error of what follows the value which ends at cursor in buf, the input followed by the nul byte: white spaces only may follow it, up to the end of the input. A nul byte in the input is a byte which the grammar doesn't have there, as encoding/json has it.

Types

type BasePathNode added in v0.10.0

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

type Decoder

type Decoder interface {
	Decode(*RuntimeContext, int64, int64, unsafe.Pointer) (int64, error)
	DecodePath(*RuntimeContext, int64, int64) ([][]byte, int64, error)
}

func CompileToGetDecoder

func CompileToGetDecoder(typ unsafe.Pointer) (Decoder, error)

CompileToGetDecoder returns the decoder of the type. The type is given by the pointer to its type descriptor, which the caller reads from an interface value, because reflect.Type is required only when the decoder is not cached yet.

func NewPathDecoder added in v0.10.0

func NewPathDecoder() Decoder

type Option

type Option struct {
	Flags   OptionFlags
	Context context.Context
	Path    *Path
}

type OptionFlags

type OptionFlags uint8
const (
	FirstWinOption OptionFlags = 1 << iota
	ContextOption
	PathOption
	// UseNumberOption decodes a number into interface{} as json.Number instead of float64.
	UseNumberOption
	// DisallowUnknownFieldsOption makes an object key which matches no field of the struct an error.
	DisallowUnknownFieldsOption
	// NoCopyStringOption makes the decoded strings refer to the input instead of a copy of their bytes.
	NoCopyStringOption
)

type Path added in v0.10.0

type Path struct {
	RootSelectorOnly        bool
	SingleQuotePathSelector bool
	DoubleQuotePathSelector bool
	// contains filtered or unexported fields
}

func (*Path) Field added in v0.10.0

func (p *Path) Field(sel string) (PathNode, bool, error)

func (*Path) Get added in v0.10.0

func (p *Path) Get(src, dst reflect.Value) error

func (*Path) String added in v0.10.0

func (p *Path) String() string

type PathBuilder added in v0.10.0

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

func (*PathBuilder) Build added in v0.10.0

func (b *PathBuilder) Build(buf []rune) (*Path, error)

type PathIndexAllNode added in v0.10.0

type PathIndexAllNode struct {
	*BasePathNode
}

func (*PathIndexAllNode) Field added in v0.10.0

func (n *PathIndexAllNode) Field(fieldName string) (PathNode, bool, error)

func (*PathIndexAllNode) Get added in v0.10.0

func (n *PathIndexAllNode) Get(src, dst reflect.Value) error

func (*PathIndexAllNode) Index added in v0.10.0

func (n *PathIndexAllNode) Index(idx int) (PathNode, bool, error)

func (*PathIndexAllNode) String added in v0.10.0

func (n *PathIndexAllNode) String() string

type PathIndexNode added in v0.10.0

type PathIndexNode struct {
	*BasePathNode
	// contains filtered or unexported fields
}

func (*PathIndexNode) Field added in v0.10.0

func (n *PathIndexNode) Field(fieldName string) (PathNode, bool, error)

func (*PathIndexNode) Get added in v0.10.0

func (n *PathIndexNode) Get(src, dst reflect.Value) error

func (*PathIndexNode) Index added in v0.10.0

func (n *PathIndexNode) Index(idx int) (PathNode, bool, error)

func (*PathIndexNode) String added in v0.10.0

func (n *PathIndexNode) String() string

type PathNode added in v0.10.0

type PathNode interface {
	fmt.Stringer
	Index(idx int) (PathNode, bool, error)
	Field(fieldName string) (PathNode, bool, error)
	Get(src, dst reflect.Value) error
	// contains filtered or unexported methods
}

type PathRecursiveNode added in v0.10.0

type PathRecursiveNode struct {
	*BasePathNode
	// contains filtered or unexported fields
}

func (*PathRecursiveNode) Field added in v0.10.0

func (n *PathRecursiveNode) Field(fieldName string) (PathNode, bool, error)

func (*PathRecursiveNode) Get added in v0.10.0

func (n *PathRecursiveNode) Get(src, dst reflect.Value) error

func (*PathRecursiveNode) Index added in v0.10.0

func (n *PathRecursiveNode) Index(_ int) (PathNode, bool, error)

func (*PathRecursiveNode) String added in v0.10.0

func (n *PathRecursiveNode) String() string

type PathSelectorNode added in v0.10.0

type PathSelectorNode struct {
	*BasePathNode
	// contains filtered or unexported fields
}

func (*PathSelectorNode) Field added in v0.10.0

func (n *PathSelectorNode) Field(fieldName string) (PathNode, bool, error)

func (*PathSelectorNode) Get added in v0.10.0

func (n *PathSelectorNode) Get(src, dst reflect.Value) error

func (*PathSelectorNode) Index added in v0.10.0

func (n *PathSelectorNode) Index(idx int) (PathNode, bool, error)

func (*PathSelectorNode) String added in v0.10.0

func (n *PathSelectorNode) String() string

type PathString added in v0.10.0

type PathString string

func (PathString) Build added in v0.10.0

func (s PathString) Build() (*Path, error)

type QuotePathSelector added in v0.10.0

type QuotePathSelector int
const (
	SingleQuotePathSelector QuotePathSelector = 1
	DoubleQuotePathSelector QuotePathSelector = 2
)

type RuntimeContext

type RuntimeContext struct {
	Buf    []byte
	Option *Option
	// contains filtered or unexported fields
}

func TakeRuntimeContext

func TakeRuntimeContext() *RuntimeContext

func (*RuntimeContext) DecoderOf added in v0.11.0

func (ctx *RuntimeContext) DecoderOf(typ unsafe.Pointer) (Decoder, error)

DecoderOf returns the decoder of the type, compiling it if the type is new.

A runtime context remembers the decoders of the types it decoded last: the same types are decoded again and again in most of the programs, and this is cheaper than a lookup of the table shared by every goroutine. The first entry of the set is looked at here, which is inlined into the callers; the rest in lookupDecoder.

func (*RuntimeContext) DiscardTypeError added in v0.11.0

func (ctx *RuntimeContext) DiscardTypeError()

DiscardTypeError drops the type error of a decoding which failed with another error.

func (*RuntimeContext) HasTypeError added in v0.11.0

func (ctx *RuntimeContext) HasTypeError() bool

HasTypeError reports whether the decoding has a type error, which TypeError returns: the type of the value is made for it only.

func (*RuntimeContext) InputSyntaxError added in v0.11.0

func (ctx *RuntimeContext) InputSyntaxError() error

InputSyntaxError returns the first syntax error of the input of the decoding, as encoding/json reports it, or nil if the input is valid: the decoders report a syntax error as they meet it, which this reports again as encoding/json does, which checks the whole input first. The input is copied again, since the decoding writes its buffer, as the escapes of the keys are decoded in it.

func (*RuntimeContext) SetInput added in v0.11.0

func (ctx *RuntimeContext) SetInput(data []byte) []byte

SetInput copies data to the buffer of the context, followed by a nul byte which ends every scan, and makes it the buffer to decode.

func (*RuntimeContext) TakeValue added in v0.11.0

func (ctx *RuntimeContext) TakeValue(typ reflect.Type, typeptr unsafe.Pointer) unsafe.Pointer

TakeValue returns the address of a zero value of typ in the heap, which the context keeps for the next call with the same type. typ is given as its type descriptor too, which is how it is looked up.

func (*RuntimeContext) TypeError added in v0.11.0

func (ctx *RuntimeContext) TypeError(dec Decoder, typ reflect.Type, base, offsetBase int64) error

TypeError returns the first type error of the decoding of the value which dec decoded from base into a value of typ, as encoding/json of the Go version reports it, or nil if the decoding had none. Its offset is relative to offsetBase.

type Stream

type Stream struct {
	Option *Option
	// contains filtered or unexported fields
}

Stream reads the values of a JSON text from an io.Reader one by one.

A value is decoded by the same decoders as a byte slice is: the stream reads until its buffer holds the whole value ( scanValue ), puts a nul byte after the value and lets the decoder work on the buffer. The decoded strings refer to the buffer, so a byte which has been read is never overwritten: when the buffer is full, the bytes which are not consumed yet are copied to a new one.

func NewStream

func NewStream(r io.Reader) *Stream

func (*Stream) Buffered

func (s *Stream) Buffered() io.Reader

Buffered returns a reader of the data read from the reader but not consumed yet.

func (*Stream) Decode added in v0.11.0

func (s *Stream) Decode(dec Decoder, typ unsafe.Pointer, p unsafe.Pointer) error

Decode decodes the next value of the stream into p, of the pointer type typ, by dec. If the value has a type error, the stream goes on after the value, as the one of encoding/json does.

func (*Stream) DecoderOf added in v0.11.0

func (s *Stream) DecoderOf(typ unsafe.Pointer) (Decoder, error)

DecoderOf returns the decoder of the type, from the recent decoders of the context of the stream.

func (*Stream) InputOffset added in v0.11.0

func (s *Stream) InputOffset() int64

InputOffset returns the offset of the stream which Decoder.InputOffset reports: the end of the last token or value read, or where a read which failed started.

func (*Stream) More

func (s *Stream) More() bool

More reports whether the current array or object has another element: at the end of the input in one, it reports true, and the error of the end is returned by the next read, as encoding/json of Go 1.27 does.

func (*Stream) Token

func (s *Stream) Token() (any, error)

Token returns the next token of the stream: a delimiter, a string, a number, a bool or nil. The commas and the colons are consumed where the grammar has them.

func (*Stream) TotalOffset

func (s *Stream) TotalOffset() int64

TotalOffset returns the number of the bytes consumed from the reader.

Jump to

Keyboard shortcuts

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