encoder
v1.15.5
Documentation
pkg.go.dev ↗Functions
Encode
func Encode(val interface{}, opts Options) ([]byte, error)
Encode returns the JSON encoding of val, encoded with opts.
EncodeIndented
func EncodeIndented(val interface{}, prefix string, indent string, opts Options) ([]byte, error)
EncodeIndented is like Encode but applies Indent to format the output. Each JSON element in the output will begin on a new line beginning with prefix followed by one or more copies of indent according to the indentation nesting.
EncodeInto
func EncodeInto(buf *[]byte, val interface{}, opts Options) error
EncodeInto is like Encode but uses a user-supplied buffer instead of allocating a new one.
ForceUseJit
func ForceUseJit()
ForceUseVM
func ForceUseVM()
HTMLEscape
func HTMLEscape(dst []byte, src []byte) []byte
HTMLEscape appends to dst the JSON-encoded src with <, >, &, U+2028 and U+2029 characters inside string literals changed to \u003c, \u003e, \u0026, \u2028, \u2029 so that the JSON will be safe to embed inside HTML <script> tags. For historical reasons, web browsers don't honor standard HTML escaping within <script> tags, so an alternative JSON encoding must be used.
Pretouch
func Pretouch(vt reflect.Type, opts ...option.CompileOption) error
Pretouch compiles vt ahead-of-time to avoid JIT compilation on-the-fly, in order to reduce the first-hit latency.
Opts are the compile options, for example, "option.WithCompileRecursiveDepth" is a compile option to set the depth of recursive compile for the nested struct type.
PretouchMany
func PretouchMany(vts []reflect.Type, opts ...option.CompileOption) error
PretouchMany compiles all vts ahead-of-time to avoid JIT compilation on-the-fly, in order to reduce the first-hit latency.
Opts are the compile options, for example, "option.WithCompileRecursiveDepth" is a compile option to set the depth of recursive compile for the nested struct type.
Quote
func Quote(s string) string
Quote returns the JSON-quoted version of s.
Valid
func Valid(data []byte) (ok bool, start int)
Valid validates json and returns first non-blank character position, if it is only one valid json value. Otherwise returns invalid character position using start.
Note: it does not check for the invalid UTF-8 characters.
Types
Compiler
type Compiler struct {
// contains filtered or unexported fields
}
NewCompiler
func NewCompiler() *Compiler
Compiler.Compile
func (self *Compiler) Compile(vt reflect.Type, pv bool) (ret ir.Program, err error)
Encoder
type Encoder struct {
Opts Options
// contains filtered or unexported fields
}
Encoder represents a specific set of encoder configurations.
Encoder.Encode
func (self *Encoder) Encode(v interface{}) ([]byte, error)
Encode returns the JSON encoding of v.
Encoder.SetCompactMarshaler
func (self *Encoder) SetCompactMarshaler(f bool)
SetCompactMarshaler specifies if option CompactMarshaler opens
Encoder.SetEscapeHTML
func (self *Encoder) SetEscapeHTML(f bool)
SetEscapeHTML specifies if option EscapeHTML opens
Encoder.SetIndent
func (enc *Encoder) SetIndent(prefix, indent string)
SetIndent instructs the encoder to format each subsequent encoded value as if indented by the package-level function EncodeIndent(). Calling SetIndent("", "") disables indentation.
Encoder.SetNoEncoderNewline
func (self *Encoder) SetNoEncoderNewline(f bool)
SetNoEncoderNewline specifies if option NoEncoderNewline opens
Encoder.SetNoQuoteTextMarshaler
func (self *Encoder) SetNoQuoteTextMarshaler(f bool)
SetNoQuoteTextMarshaler specifies if option NoQuoteTextMarshaler opens
Encoder.SetNoValidateJSONMarshaler
func (self *Encoder) SetNoValidateJSONMarshaler(f bool)
SetNoValidateJSONMarshaler specifies if option NoValidateJSONMarshaler opens
Encoder.SetValidateString
func (self *Encoder) SetValidateString(f bool)
SetValidateString specifies if option ValidateString opens
Encoder.SortKeys
func (self *Encoder) SortKeys() *Encoder
SortKeys enables the SortMapKeys option.
Options
type Options uint64
Options is a set of encoding options.
SortMapKeys (10 declarations)
const (
// SortMapKeys indicates that the keys of a map needs to be sorted
// before serializing into JSON.
// WARNING: This hurts performance A LOT, USE WITH CARE.
SortMapKeys Options = 1 << alg.BitSortMapKeys
// EscapeHTML indicates encoder to escape all HTML characters
// after serializing into JSON (see https://pkg.go.dev/encoding/json#HTMLEscape).
// WARNING: This hurts performance A LOT, USE WITH CARE.
EscapeHTML Options = 1 << alg.BitEscapeHTML
// CompactMarshaler indicates that the output JSON from json.Marshaler
// is always compact and needs no validation
CompactMarshaler Options = 1 << alg.BitCompactMarshaler
// NoQuoteTextMarshaler indicates that the output text from encoding.TextMarshaler
// is always escaped string and needs no quoting
NoQuoteTextMarshaler Options = 1 << alg.BitNoQuoteTextMarshaler
// NoNullSliceOrMap indicates all empty Array or Object are encoded as '[]' or '{}',
// instead of 'null'.
// NOTE: The priority of this option is lower than json tag `omitempty`.
NoNullSliceOrMap Options = 1 << alg.BitNoNullSliceOrMap
// ValidateString indicates that encoder should validate the input string
// before encoding it into JSON.
ValidateString Options = 1 << alg.BitValidateString
// NoValidateJSONMarshaler indicates that the encoder should not validate the output string
// after encoding the JSONMarshaler to JSON.
NoValidateJSONMarshaler Options = 1 << alg.BitNoValidateJSONMarshaler
// NoEncoderNewline indicates that the encoder should not add a newline after every message
NoEncoderNewline Options = 1 << alg.BitNoEncoderNewline
// CompatibleWithStd is used to be compatible with std encoder.
CompatibleWithStd Options = SortMapKeys | EscapeHTML | CompactMarshaler
// Encode Infinity or Nan float into `null`, instead of returning an error.
EncodeNullForInfOrNan Options = 1 << alg.BitEncodeNullForInfOrNan
)
StreamEncoder
type StreamEncoder struct {
Encoder
// contains filtered or unexported fields
}
StreamEncoder uses io.Writer as input.
NewStreamEncoder
func NewStreamEncoder(w io.Writer) *StreamEncoder
NewStreamEncoder adapts to encoding/json.NewDecoder API.
NewStreamEncoder returns a new encoder that write to w.
StreamEncoder.Encode
func (enc *StreamEncoder) Encode(val interface{}) (err error)
Encode encodes interface{} as JSON to io.Writer