package json

import (
	
	
	

	
	
)

// An Encoder writes JSON values to an output stream.
type Encoder struct {
	w                 io.Writer
	enabledIndent     bool
	enabledHTMLEscape bool
	prefix            string
	indentStr         string
}

// NewEncoder returns a new encoder that writes to w.
func ( io.Writer) *Encoder {
	return &Encoder{w: , enabledHTMLEscape: true}
}

// Encode writes the JSON encoding of v to the stream, followed by a newline character.
//
// See the documentation for Marshal for details about the conversion of Go values to JSON.
func ( *Encoder) ( any) error {
	return .EncodeWithOption()
}

// EncodeWithOption call Encode with EncodeOption.
func ( *Encoder) ( any,  ...EncodeOptionFunc) error {
	 := encoder.TakeRuntimeContext()
	.Option.Flag = 0

	 := .encodeWithOption(, , ...)

	encoder.ReleaseRuntimeContext()
	return 
}

// EncodeContext call Encode with context.Context and EncodeOption.
func ( *Encoder) ( context.Context,  any,  ...EncodeOptionFunc) error {
	 := encoder.TakeRuntimeContext()
	.Option.Flag = 0
	.Option.Flag |= encoder.ContextOption
	.Option.Context = 

	 := .encodeWithOption(, , ...)

	encoder.ReleaseRuntimeContext()
	return 
}

func ( *Encoder) ( *encoder.RuntimeContext,  any,  ...EncodeOptionFunc) error {
	if .enabledHTMLEscape {
		.Option.Flag |= encoder.HTMLEscapeOption
	}
	.Option.Flag |= encoder.NormalizeUTF8Option
	.Option.DebugOut = os.Stdout
	for ,  := range  {
		(.Option)
	}
	var (
		 []byte
		 error
	)
	if .enabledIndent {
		,  = run.EncodeIndent(, , .prefix, .indentStr)
	} else {
		,  = run.Encode(, )
	}
	if  != nil {
		return 
	}
	if .enabledIndent {
		 = [:len()-2]
	} else {
		 = [:len()-1]
	}
	 = append(, '\n')
	if ,  := .w.Write();  != nil {
		return 
	}
	return nil
}

// SetEscapeHTML specifies whether problematic HTML characters should be escaped inside JSON quoted strings.
// The default behavior is to escape &, <, and > to \u0026, \u003c, and \u003e to avoid certain safety problems that can arise when embedding JSON in HTML.
//
// In non-HTML settings where the escaping interferes with the readability of the output, SetEscapeHTML(false) disables this behavior.
func ( *Encoder) ( bool) {
	.enabledHTMLEscape = 
}

// SetIndent instructs the encoder to format each subsequent encoded value as if indented by the package-level function Indent(dst, src, prefix, indent).
// Calling SetIndent("", "") disables indentation.
func ( *Encoder) (,  string) {
	if  == "" &&  == "" {
		.enabledIndent = false
		return
	}
	.prefix = 
	.indentStr = 
	.enabledIndent = true
}

func marshalContext( context.Context,  any,  ...EncodeOptionFunc) ([]byte, error) {
	 := encoder.TakeRuntimeContext()
	.Option.Flag = 0
	.Option.Flag = encoder.HTMLEscapeOption | encoder.NormalizeUTF8Option | encoder.ContextOption
	.Option.Context = 
	for ,  := range  {
		(.Option)
	}

	,  := run.Encode(, )
	if  != nil {
		encoder.ReleaseRuntimeContext()
		return nil, 
	}

	// this line exists to escape call of `runtime.makeslicecopy` .
	// if use `make([]byte, len(buf)-1)` and `copy(copied, buf)`,
	// dst buffer size and src buffer size are differrent.
	// in this case, compiler uses `runtime.makeslicecopy`, but it is slow.
	 = [:len()-1]
	 := make([]byte, len())
	copy(, )

	encoder.ReleaseRuntimeContext()
	return , nil
}

func marshal( any,  ...EncodeOptionFunc) ([]byte, error) {
	 := encoder.TakeRuntimeContext()

	.Option.Flag = 0
	.Option.Flag |= (encoder.HTMLEscapeOption | encoder.NormalizeUTF8Option)
	for ,  := range  {
		(.Option)
	}

	,  := run.Encode(, )
	if  != nil {
		encoder.ReleaseRuntimeContext()
		return nil, 
	}

	// this line exists to escape call of `runtime.makeslicecopy` .
	// if use `make([]byte, len(buf)-1)` and `copy(copied, buf)`,
	// dst buffer size and src buffer size are differrent.
	// in this case, compiler uses `runtime.makeslicecopy`, but it is slow.
	 = [:len()-1]
	 := make([]byte, len())
	copy(, )

	encoder.ReleaseRuntimeContext()
	return , nil
}

func marshalIndent( any, ,  string,  ...EncodeOptionFunc) ([]byte, error) {
	 := encoder.TakeRuntimeContext()

	.Option.Flag = 0
	.Option.Flag |= (encoder.HTMLEscapeOption | encoder.NormalizeUTF8Option | encoder.IndentOption)
	for ,  := range  {
		(.Option)
	}

	,  := run.EncodeIndent(, , , )
	if  != nil {
		encoder.ReleaseRuntimeContext()
		return nil, 
	}

	 = [:len()-2]
	 := make([]byte, len())
	copy(, )

	encoder.ReleaseRuntimeContext()
	return , nil
}