github.com/jxskiss/gopkg/v2@v2.14.9-0.20240514120614-899f3e7952b4/perf/json/encoder.go (about)

     1  package json
     2  
     3  import "io"
     4  
     5  // Encoder is a wrapper of encoding/json.Encoder.
     6  // It provides same methods as encoding/json.Encoder but with method
     7  // chaining capabilities.
     8  //
     9  // See encoding/json.Encoder for detailed document.
    10  type Encoder struct {
    11  	UnderlyingEncoder
    12  }
    13  
    14  // NewEncoder returns a new Encoder that writes to w.
    15  func NewEncoder(w io.Writer) *Encoder {
    16  	return &Encoder{getImpl().NewEncoder(w)}
    17  }
    18  
    19  // SetEscapeHTML specifies whether problematic HTML characters
    20  // should be escaped inside JSON quoted strings.
    21  // The default behavior is to escape &, <, and > to \u0026, \u003c, and \u003e
    22  // to avoid certain safety problems that can arise when embedding JSON in HTML.
    23  //
    24  // In non-HTML settings where the escaping interferes with the readability
    25  // of the output, SetEscapeHTML(false) disables this behavior.
    26  func (enc *Encoder) SetEscapeHTML(on bool) *Encoder {
    27  	enc.UnderlyingEncoder.SetEscapeHTML(on)
    28  	return enc
    29  }
    30  
    31  // SetIndent instructs the encoder to format each subsequent encoded
    32  // value as if indented by the package-level function Indent(dst, src, prefix, indent).
    33  // Calling SetIndent("", "") disables indentation.
    34  func (enc *Encoder) SetIndent(prefix, indent string) *Encoder {
    35  	enc.UnderlyingEncoder.SetIndent(prefix, indent)
    36  	return enc
    37  }