// Copyright 2020 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

//go:build goexperiment.jsonv2

package json

import (
	
	
	

	
	
	
	
	
)

var errNonStringValue = errors.New("JSON value must be string type")

// Interfaces for custom serialization.
var (
	jsonMarshalerType       = reflect.TypeFor[Marshaler]()
	jsonMarshalerToType     = reflect.TypeFor[MarshalerTo]()
	jsonUnmarshalerType     = reflect.TypeFor[Unmarshaler]()
	jsonUnmarshalerFromType = reflect.TypeFor[UnmarshalerFrom]()
	textAppenderType        = reflect.TypeFor[encoding.TextAppender]()
	textMarshalerType       = reflect.TypeFor[encoding.TextMarshaler]()
	textUnmarshalerType     = reflect.TypeFor[encoding.TextUnmarshaler]()

	allMarshalerTypes   = []reflect.Type{jsonMarshalerToType, jsonMarshalerType, textAppenderType, textMarshalerType}
	allUnmarshalerTypes = []reflect.Type{jsonUnmarshalerFromType, jsonUnmarshalerType, textUnmarshalerType}
	allMethodTypes      = append(allMarshalerTypes, allUnmarshalerTypes...)
)

// Marshaler is implemented by types that can marshal themselves.
// It is recommended that types implement [MarshalerTo] unless the implementation
// is trying to avoid a hard dependency on the "jsontext" package.
//
// It is recommended that implementations return a buffer that is safe
// for the caller to retain and potentially mutate.
type Marshaler interface {
	MarshalJSON() ([]byte, error)
}

// MarshalerTo is implemented by types that can marshal themselves.
// It is recommended that types implement MarshalerTo instead of [Marshaler]
// since this is both more performant and flexible.
// If a type implements both Marshaler and MarshalerTo,
// then MarshalerTo takes precedence. In such a case, both implementations
// should aim to have equivalent behavior for the default marshal options.
//
// The implementation must write only one JSON value to the Encoder and
// must not retain the pointer to [jsontext.Encoder].
type MarshalerTo interface {
	MarshalJSONTo(*jsontext.Encoder) error

	// TODO: Should users call the MarshalEncode function or
	// should/can they call this method directly? Does it matter?
}

// Unmarshaler is implemented by types that can unmarshal themselves.
// It is recommended that types implement [UnmarshalerFrom] unless the implementation
// is trying to avoid a hard dependency on the "jsontext" package.
//
// The input can be assumed to be a valid encoding of a JSON value
// if called from unmarshal functionality in this package.
// UnmarshalJSON must copy the JSON data if it is retained after returning.
// It is recommended that UnmarshalJSON implement merge semantics when
// unmarshaling into a pre-populated value.
//
// Implementations must not retain or mutate the input []byte.
type Unmarshaler interface {
	UnmarshalJSON([]byte) error
}

// UnmarshalerFrom is implemented by types that can unmarshal themselves.
// It is recommended that types implement UnmarshalerFrom instead of [Unmarshaler]
// since this is both more performant and flexible.
// If a type implements both Unmarshaler and UnmarshalerFrom,
// then UnmarshalerFrom takes precedence. In such a case, both implementations
// should aim to have equivalent behavior for the default unmarshal options.
//
// The implementation must read only one JSON value from the Decoder.
// It is recommended that UnmarshalJSONFrom implement merge semantics when
// unmarshaling into a pre-populated value.
//
// Implementations must not retain the pointer to [jsontext.Decoder].
type UnmarshalerFrom interface {
	UnmarshalJSONFrom(*jsontext.Decoder) error

	// TODO: Should users call the UnmarshalDecode function or
	// should/can they call this method directly? Does it matter?
}

func makeMethodArshaler( *arshaler,  reflect.Type) *arshaler {
	// Avoid injecting method arshaler on the pointer or interface version
	// to avoid ever calling the method on a nil pointer or interface receiver.
	// Let it be injected on the value receiver (which is always addressable).
	if .Kind() == reflect.Pointer || .Kind() == reflect.Interface {
		return 
	}

	if ,  := implements(, textMarshalerType);  {
		.nonDefault = true
		 := .marshal
		.marshal = func( *jsontext.Encoder,  addressableValue,  *jsonopts.Struct) error {
			if .Flags.Get(jsonflags.CallMethodsWithLegacySemantics) &&
				( && .forcedAddr) {
				return (, , )
			}
			 := .Addr().Interface().(encoding.TextMarshaler)
			if  := export.Encoder().AppendRaw('"', false, func( []byte) ([]byte, error) {
				,  := .MarshalText()
				return append(, ...), 
			});  != nil {
				 = wrapSkipFunc(, "marshal method")
				if .Flags.Get(jsonflags.ReportErrorsWithLegacySemantics) {
					return internal.NewMarshalerError(.Addr().Interface(), , "MarshalText") // unlike unmarshal, always wrapped
				}
				if !isSemanticError() && !export.IsIOError() {
					 = newMarshalErrorBefore(, , )
				}
				return 
			}
			return nil
		}
	}

	if ,  := implements(, textAppenderType);  {
		.nonDefault = true
		 := .marshal
		.marshal = func( *jsontext.Encoder,  addressableValue,  *jsonopts.Struct) ( error) {
			if .Flags.Get(jsonflags.CallMethodsWithLegacySemantics) &&
				( && .forcedAddr) {
				return (, , )
			}
			 := .Addr().Interface().(encoding.TextAppender)
			if  := export.Encoder().AppendRaw('"', false, .AppendText);  != nil {
				 = wrapSkipFunc(, "append method")
				if .Flags.Get(jsonflags.ReportErrorsWithLegacySemantics) {
					return internal.NewMarshalerError(.Addr().Interface(), , "AppendText") // unlike unmarshal, always wrapped
				}
				if !isSemanticError() && !export.IsIOError() {
					 = newMarshalErrorBefore(, , )
				}
				return 
			}
			return nil
		}
	}

	if ,  := implements(, jsonMarshalerType);  {
		.nonDefault = true
		 := .marshal
		.marshal = func( *jsontext.Encoder,  addressableValue,  *jsonopts.Struct) error {
			if .Flags.Get(jsonflags.CallMethodsWithLegacySemantics) &&
				(( && .forcedAddr) || export.Encoder().Tokens.Last.NeedObjectName()) {
				return (, , )
			}
			 := .Addr().Interface().(Marshaler)
			,  := .MarshalJSON()
			if  != nil {
				 = wrapSkipFunc(, "marshal method")
				if .Flags.Get(jsonflags.ReportErrorsWithLegacySemantics) {
					return internal.NewMarshalerError(.Addr().Interface(), , "MarshalJSON") // unlike unmarshal, always wrapped
				}
				 = newMarshalErrorBefore(, , )
				return collapseSemanticErrors()
			}
			if  := .WriteValue();  != nil {
				if .Flags.Get(jsonflags.ReportErrorsWithLegacySemantics) {
					return internal.NewMarshalerError(.Addr().Interface(), , "MarshalJSON") // unlike unmarshal, always wrapped
				}
				if isSyntacticError() {
					 = newMarshalErrorBefore(, , )
				}
				return 
			}
			return nil
		}
	}

	if ,  := implements(, jsonMarshalerToType);  {
		.nonDefault = true
		 := .marshal
		.marshal = func( *jsontext.Encoder,  addressableValue,  *jsonopts.Struct) error {
			if .Flags.Get(jsonflags.CallMethodsWithLegacySemantics) &&
				(( && .forcedAddr) || export.Encoder().Tokens.Last.NeedObjectName()) {
				return (, , )
			}
			 := export.Encoder()
			,  := .Tokens.DepthLength()
			.Flags.Set(jsonflags.WithinArshalCall | 1)
			 := .Addr().Interface().(MarshalerTo).MarshalJSONTo()
			.Flags.Set(jsonflags.WithinArshalCall | 0)
			,  := .Tokens.DepthLength()
			if ( !=  || +1 != ) &&  == nil {
				 = errNonSingularValue
			}
			if  != nil {
				 = wrapSkipFunc(, "marshal method")
				if .Flags.Get(jsonflags.ReportErrorsWithLegacySemantics) {
					return internal.NewMarshalerError(.Addr().Interface(), , "MarshalJSONTo") // unlike unmarshal, always wrapped
				}
				if !export.IsIOError() {
					 = newSemanticErrorWithPosition(, , , , )
				}
				return 
			}
			return nil
		}
	}

	if ,  := implements(, textUnmarshalerType);  {
		.nonDefault = true
		.unmarshal = func( *jsontext.Decoder,  addressableValue,  *jsonopts.Struct) error {
			 := export.Decoder()
			var  jsonwire.ValueFlags
			,  := .ReadValue(&)
			if  != nil {
				return  // must be a syntactic or I/O error
			}
			if .Kind() == 'n' {
				if !.Flags.Get(jsonflags.MergeWithLegacySemantics) {
					.SetZero()
				}
				return nil
			}
			if .Kind() != '"' {
				return newUnmarshalErrorAfter(, , errNonStringValue)
			}
			 := jsonwire.UnquoteMayCopy(, .IsVerbatim())
			 := .Addr().Interface().(encoding.TextUnmarshaler)
			if  := .UnmarshalText();  != nil {
				 = wrapSkipFunc(, "unmarshal method")
				if .Flags.Get(jsonflags.ReportErrorsWithLegacySemantics) {
					return  // unlike marshal, never wrapped
				}
				if !isSemanticError() && !isSyntacticError() && !export.IsIOError() {
					 = newUnmarshalErrorAfter(, , )
				}
				return 
			}
			return nil
		}
	}

	if ,  := implements(, jsonUnmarshalerType);  {
		.nonDefault = true
		 := .unmarshal
		.unmarshal = func( *jsontext.Decoder,  addressableValue,  *jsonopts.Struct) error {
			if .Flags.Get(jsonflags.CallMethodsWithLegacySemantics) &&
				export.Decoder().Tokens.Last.NeedObjectName() {
				return (, , )
			}
			,  := .ReadValue()
			if  != nil {
				return  // must be a syntactic or I/O error
			}
			 := .Addr().Interface().(Unmarshaler)
			if  := .UnmarshalJSON();  != nil {
				 = wrapSkipFunc(, "unmarshal method")
				if .Flags.Get(jsonflags.ReportErrorsWithLegacySemantics) {
					return  // unlike marshal, never wrapped
				}
				 = newUnmarshalErrorAfter(, , )
				return collapseSemanticErrors()
			}
			return nil
		}
	}

	if ,  := implements(, jsonUnmarshalerFromType);  {
		.nonDefault = true
		 := .unmarshal
		.unmarshal = func( *jsontext.Decoder,  addressableValue,  *jsonopts.Struct) error {
			if .Flags.Get(jsonflags.CallMethodsWithLegacySemantics) &&
				export.Decoder().Tokens.Last.NeedObjectName() {
				return (, , )
			}
			 := export.Decoder()
			,  := .Tokens.DepthLength()
			.Flags.Set(jsonflags.WithinArshalCall | 1)
			 := .Addr().Interface().(UnmarshalerFrom).UnmarshalJSONFrom()
			.Flags.Set(jsonflags.WithinArshalCall | 0)
			,  := .Tokens.DepthLength()
			if ( !=  || +1 != ) &&  == nil {
				 = errNonSingularValue
			}
			if  != nil {
				 = wrapSkipFunc(, "unmarshal method")
				if .Flags.Get(jsonflags.ReportErrorsWithLegacySemantics) {
					if  := .SkipUntil(, +1);  != nil {
						return 
					}
					return  // unlike marshal, never wrapped
				}
				if !isSyntacticError() && !export.IsIOError() {
					 = newSemanticErrorWithPosition(, , , , )
				}
				return 
			}
			return nil
		}
	}

	return 
}

// implementsAny is like t.Implements(ifaceType) for a list of interfaces,
// but checks whether either t or reflect.PointerTo(t) implements the interface.
func implementsAny( reflect.Type,  ...reflect.Type) bool {
	for ,  := range  {
		if ,  := implements(, );  {
			return true
		}
	}
	return false
}

// implements is like t.Implements(ifaceType) but checks whether
// either t or reflect.PointerTo(t) implements the interface.
// It also reports whether the value needs to be addressed
// in order to satisfy the interface.
func implements(,  reflect.Type) (,  bool) {
	switch {
	case .Implements():
		return false, true
	case reflect.PointerTo().Implements():
		return true, true
	default:
		return false, false
	}
}