Documentation
¶
Index ¶
- func Compact(dst *bytes.Buffer, src []byte) error
- func Indent(dst *bytes.Buffer, src []byte, prefix, indent string) error
- func Marshal(v any, opts ...EncOptsOption) ([]byte, error)
- func MarshalIndent(v any, prefix, indent string, opts ...EncOptsOption) ([]byte, error)
- func Valid(data []byte) bool
- type EmptyEncOptsOption
- type EncOptsOption
- func WithEncOpts(v encOpts) EncOptsOption
- func WithEncOptsEscapeHTML(v bool) EncOptsOption
- func WithEncOptsQuoted(v bool) EncOptsOption
- func WithEncOptsTruncate(v int) EncOptsOption
- func WithEncOptsTruncateBytes(v int) EncOptsOption
- func WithEncOptsTruncateMap(v int) EncOptsOption
- func WithEncOptsTruncateSliceOrArray(v int) EncOptsOption
- func WithEncOptsTruncateString(v int) EncOptsOption
- type EncOptsOptionFunc
- type Encoder
- type Marshaler
- type MarshalerError
- type Number
- type SyntaxError
- type UnsupportedTypeError
- type UnsupportedValueError
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Compact ¶
Compact appends to dst the JSON-encoded src with insignificant space characters elided.
func Indent ¶
Indent appends to dst an indented form of the JSON-encoded src. Each element in a JSON object or array begins on a new, indented line beginning with prefix followed by one or more copies of indent according to the indentation nesting. The data appended to dst does not begin with the prefix nor any indentation, to make it easier to embed inside other formatted JSON data. Although leading space characters (space, tab, carriage return, newline) at the beginning of src are dropped, trailing space characters at the end of src are preserved and copied to dst. For example, if src has no trailing spaces, neither will dst; if src ends in a trailing newline, so will dst.
func Marshal ¶
func Marshal(v any, opts ...EncOptsOption) ([]byte, error)
Marshal returns the JSON encoding of v, and support truncate.
func MarshalIndent ¶
func MarshalIndent(v any, prefix, indent string, opts ...EncOptsOption) ([]byte, error)
MarshalIndent is like Marshal 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.
Types ¶
type EmptyEncOptsOption ¶
type EmptyEncOptsOption struct{}
EmptyEncOptsOption does not alter the configuration. It can be embedded in another structure to build custom options.
This API is EXPERIMENTAL.
type EncOptsOption ¶
type EncOptsOption interface {
// contains filtered or unexported methods
}
A EncOptsOption sets options.
func WithEncOpts ¶ added in v1.2.84
func WithEncOpts(v encOpts) EncOptsOption
WithEncOpts sets encOpts.
func WithEncOptsEscapeHTML ¶
func WithEncOptsEscapeHTML(v bool) EncOptsOption
WithEncOptsEscapeHTML sets escapeHTML in encOpts. escapeHTML causes '<', '>', and '&' to be escaped in JSON strings.
func WithEncOptsQuoted ¶
func WithEncOptsQuoted(v bool) EncOptsOption
WithEncOptsQuoted sets quoted in encOpts. quoted causes primitive fields to be encoded inside JSON strings.
func WithEncOptsTruncate ¶
func WithEncOptsTruncate(v int) EncOptsOption
WithEncOptsTruncate sets truncate in encOpts.
func WithEncOptsTruncateBytes ¶
func WithEncOptsTruncateBytes(v int) EncOptsOption
WithEncOptsTruncateBytes sets truncateBytes in encOpts.
func WithEncOptsTruncateMap ¶
func WithEncOptsTruncateMap(v int) EncOptsOption
WithEncOptsTruncateMap sets truncateMap in encOpts.
func WithEncOptsTruncateSliceOrArray ¶ added in v1.2.84
func WithEncOptsTruncateSliceOrArray(v int) EncOptsOption
WithEncOptsTruncateSliceOrArray sets truncateSliceOrArray in encOpts.
func WithEncOptsTruncateString ¶
func WithEncOptsTruncateString(v int) EncOptsOption
WithEncOptsTruncateString sets truncateString in encOpts.
type EncOptsOptionFunc ¶
type EncOptsOptionFunc func(*encOpts)
EncOptsOptionFunc wraps a function that modifies encOpts into an implementation of the EncOptsOption interface.
type Encoder ¶ added in v1.2.84
type Encoder struct {
// contains filtered or unexported fields
}
An Encoder writes JSON values to an output stream.
func NewEncoder ¶ added in v1.2.84
NewEncoder returns a new encoder that writes to w.
func (*Encoder) Encode ¶ added in v1.2.84
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) SetEscapeHTML ¶ added in v1.2.84
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) SetIndent ¶ added in v1.2.84
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) SetTruncate ¶ added in v1.2.84
SetTruncate shrinks bytes, string, map, slice, array 's len to n at most
type Marshaler ¶ added in v1.2.84
Marshaler is the interface implemented by types that can marshal themselves into valid JSON.
type MarshalerError ¶
type MarshalerError = json.MarshalerError
A MarshalerError represents an error from calling a MarshalJSON or MarshalText method.
type SyntaxError ¶
type SyntaxError struct { Offset int64 // error occurred after reading Offset bytes // contains filtered or unexported fields }
A SyntaxError is a description of a JSON syntax error. Unmarshal will return a SyntaxError if the JSON can't be parsed.
func (*SyntaxError) Error ¶
func (e *SyntaxError) Error() string
type UnsupportedTypeError ¶
type UnsupportedTypeError = json.UnsupportedTypeError
An UnsupportedTypeError is returned by Marshal when attempting to encode an unsupported value type.
type UnsupportedValueError ¶
type UnsupportedValueError = json.UnsupportedValueError
An UnsupportedValueError is returned by Marshal when attempting to encode an unsupported value.