package decoder

import (
	
	
	
	
	
	

	
)

type unmarshalJSONDecoder struct {
	typ reflect.Type
	// retainsNothing is set for a type whose UnmarshalJSON keeps nothing of the bytes it is given, which are then
	// the ones of the buffer: a type of the standard library ( see runtime.IsStdMarshalerType ), a type which has
	// the UnmarshalJSON of one only by embedding it, and json.RawMessage, which copies them. The bytes of any other type are copied: its UnmarshalJSON may keep them,
	// and the buffer is written again by the next call.
	retainsNothing bool
}

// timePtrType is the type of *time.Time, which is decoded from a string only. The decoder is made for the pointer
// type, whose method set has UnmarshalJSON.
var timePtrType = reflect.TypeOf(&time.Time{})

// rawMessagePtrType is the type of *json.RawMessage, whose UnmarshalJSON copies the bytes it is given: a copy of
// them made here would be copied again.
var rawMessagePtrType = reflect.TypeOf(&json.RawMessage{})

// timeType is the type of time.Time.
var timeType = timePtrType.Elem()

func newUnmarshalJSONDecoder( reflect.Type) *unmarshalJSONDecoder {
	return &unmarshalJSONDecoder{
		typ:            ,
		retainsNothing:  == rawMessagePtrType || runtime.IsStdMarshalerType() || promotesStdUnmarshalJSON(),
	}
}

// promotesStdUnmarshalJSON is whether the UnmarshalJSON of the type is the one of an embedded type of the standard
// library, which the type doesn't declare itself ( see runtime.PromotedStdMethod ).
func promotesStdUnmarshalJSON( reflect.Type) bool {
	, , ,  := runtime.PromotedStdMethod(, runtime.UnmarshalJSONMethod)
	return 
}

func ( *unmarshalJSONDecoder) ( *RuntimeContext, ,  int64,  unsafe.Pointer) (int64, error) {
	 := .Buf
	 = skipWhiteSpace(, )
	if .typ == timePtrType {
		if  := [];  != '"' &&  != 'n' {
			// a time.Time is decoded from a string only: any other value is a type error
			return .skipTypeError(, , timeType)
		}
	}
	 := 
	,  := skipValue(, , )
	if  != nil {
		return 0, 
	}
	 := [::]
	if !.retainsNothing {
		// a copy of the exact length: append would round its capacity up, which costs more
		 := make([]byte, len())
		copy(, )
		 = 
	}

	 := *(*any)(unsafe.Pointer(&emptyInterface{
		typ: runtime.TypePtr(.typ),
		ptr: ,
	}))
	// The method is chosen by what the type implements, not by the entry point:
	// json.Unmarshal may meet a type with the context method and UnmarshalContext one without it.
	switch v := .(type) {
	case unmarshalerContext:
		 := .Option.Context
		if (.Option.Flags&ContextOption) == 0 ||  == nil {
			 = context.Background()
		}
		if  := .UnmarshalJSON(, );  != nil {
			.methodError()
		}
	case json.Unmarshaler:
		if  := .UnmarshalJSON();  != nil {
			.methodError()
		}
	}
	return , nil
}

func ( *unmarshalJSONDecoder) ( *RuntimeContext, ,  int64) ([][]byte, int64, error) {
	return nil, 0, fmt.Errorf("json: unmarshal json decoder does not support decode path")
}