package json

import (
	
	
	

	
	
	
)

type Decoder struct {
	s *decoder.Stream
}

type emptyInterface struct {
	typ unsafe.Pointer
	ptr unsafe.Pointer
}

func unmarshal( []byte,  any,  ...DecodeOptionFunc) error {
	 := (*emptyInterface)(unsafe.Pointer(&))

	if  := validateType(.typ, uintptr(.ptr));  != nil {
		return 
	}
	 := decoder.TakeRuntimeContext()
	,  := .DecoderOf(.typ)
	if  != nil {
		decoder.ReleaseRuntimeContext()
		return 
	}
	 := .SetInput()
	.Option.Flags = 0
	for ,  := range  {
		(.Option)
	}
	,  := .Decode(, 0, 0, .ptr)
	if  == nil {
		// src is the buffer of the context: it is validated before the context is released
		 = validateEndBuf(, )
	}
	if  != nil || .HasTypeError() {
		return endWithError(, .typ, )
	}
	decoder.ReleaseRuntimeContext()
	return nil
}

func unmarshalContext( context.Context,  []byte,  any,  ...DecodeOptionFunc) error {
	 := (*emptyInterface)(unsafe.Pointer(&))

	if  := validateType(.typ, uintptr(.ptr));  != nil {
		return 
	}
	 := decoder.TakeRuntimeContext()
	,  := .DecoderOf(.typ)
	if  != nil {
		decoder.ReleaseRuntimeContext()
		return 
	}
	 := .SetInput()
	.Option.Flags = 0
	.Option.Flags |= decoder.ContextOption
	.Option.Context = 
	for ,  := range  {
		(.Option)
	}
	,  := .Decode(, 0, 0, .ptr)
	if  == nil {
		// src is the buffer of the context: it is validated before the context is released
		 = validateEndBuf(, )
	}
	if  != nil || .HasTypeError() {
		return endWithError(, .typ, )
	}
	decoder.ReleaseRuntimeContext()
	return nil
}

var (
	pathDecoder = decoder.NewPathDecoder()
)

// extractFromPath returns the values of data which the path selects: parts of data, or of a copy of it.
func extractFromPath( *Path,  []byte,  ...DecodeOptionFunc) ([][]byte, error) {
	if .path.RootSelectorOnly {
		return [][]byte{}, nil
	}
	 := decoder.NewInput()

	 := decoder.TakeRuntimeContext()
	decoder.SetBuf(, )
	.Option.Flags = 0
	.Option.Flags |= decoder.PathOption
	.SetPath(.path)
	for ,  := range  {
		(.Option)
	}
	, ,  := pathDecoder.DecodePath(, 0, 0)
	if  != nil {
		decoder.ReleaseRuntimeContext()
		return nil, 
	}
	decoder.ReleaseRuntimeContext()
	if  := validateEndBuf(, );  != nil {
		return nil, 
	}
	return , nil
}

// endWithError releases the context of a decoding into a value of the pointer type typ, which failed with err or
// else has a type error, and returns the error: the type error is returned only when the whole input is valid, as
// encoding/json does.
//
//go:noinline
func endWithError( *decoder.RuntimeContext,  unsafe.Pointer,  error) error {
	 = inputSyntaxError(, )
	if  == nil {
		 = .TypeError(runtime.TypeOfPtr(), 0, 0, nil)
	}
	.DiscardTypeError()
	decoder.ReleaseRuntimeContext()
	return 
}

// inputSyntaxError returns err, the error of the decoding of the input of ctx, or, for a syntax error, the first
// syntax error of the input as encoding/json reports it: it checks the whole input before it decodes it.
func inputSyntaxError( *decoder.RuntimeContext,  error) error {
	if ,  := .(*errors.SyntaxError);  {
		if  := .InputSyntaxError();  != nil {
			return 
		}
	}
	return 
}

// validateEndBuf returns the syntax error of what follows the value, which ends at cursor, in src, the input
// followed by the nul byte: only white spaces may follow it. The value is most often at the end of the input, which
// is checked without a call.
func validateEndBuf( []byte,  int64) error {
	if  == int64(len())-1 {
		return nil
	}
	return decoder.ValidateEnd(, )
}

// validateType validates that the value is not nil.
// Whether the type is a pointer is validated when its decoder is compiled, once per type.
func validateType( unsafe.Pointer,  uintptr) error {
	if  == nil ||  == 0 {
		return &InvalidUnmarshalError{Type: runtime.TypeOfPtr()}
	}
	return nil
}

// NewDecoder returns a new decoder that reads from r.
//
// The decoder introduces its own buffering and may
// read data from r beyond the JSON values requested.
func ( io.Reader) *Decoder {
	 := decoder.NewStream()
	return &Decoder{
		s: ,
	}
}

// Buffered returns a reader of the data remaining in the Decoder's
// buffer. The reader is valid until the next call to Decode.
func ( *Decoder) () io.Reader {
	return .s.Buffered()
}

// Decode reads the next JSON-encoded value from its
// input and stores it in the value pointed to by v.
//
// See the documentation for Unmarshal for details about
// the conversion of JSON into a Go value.
func ( *Decoder) ( any) error {
	return .DecodeWithOption()
}

// DecodeContext reads the next JSON-encoded value from its
// input and stores it in the value pointed to by v with context.Context.
func ( *Decoder) ( context.Context,  any) error {
	.s.Option.Flags |= decoder.ContextOption
	.s.Option.Context = 
	return .DecodeWithOption()
}

func ( *Decoder) ( any,  ...DecodeOptionFunc) error {
	 := (*emptyInterface)(unsafe.Pointer(&))
	if  := validateType(.typ, uintptr(.ptr));  != nil {
		return 
	}

	,  := .s.DecoderOf(.typ)
	if  != nil {
		return 
	}
	 := .s
	for ,  := range  {
		(.Option)
	}
	return .Decode(, .typ, .ptr)
}

func ( *Decoder) () bool {
	return .s.More()
}

func ( *Decoder) () (Token, error) {
	return .s.Token()
}

// DisallowUnknownFields causes the Decoder to return an error when the destination
// is a struct and the input contains object keys which do not match any
// non-ignored, exported fields in the destination.
func ( *Decoder) () {
	.s.Option.Flags |= decoder.DisallowUnknownFieldsOption
}

func ( *Decoder) () int64 {
	return .s.InputOffset()
}

// UseNumber causes the Decoder to unmarshal a number into an interface{} as a
// Number instead of as a float64.
func ( *Decoder) () {
	.s.Option.Flags |= decoder.UseNumberOption
}