Documentation ¶
Overview ¶
Package json provides a on-the-fly change-able API for JSON serialization.
Index ¶
- Variables
- func ChangeImpl(impl Implementation)
- func Compact(dst *bytes.Buffer, src []byte) error
- func Dump(path string, v any, prefix, indent string) error
- func Fdump(w io.Writer, v any, prefix, indent string) error
- func HTMLEscape(dst *bytes.Buffer, src []byte)
- func Indent(dst *bytes.Buffer, src []byte, prefix, indent string) error
- func Load(path string, v any) error
- func Marshal(v any) ([]byte, error)
- func MarshalFastest(v any) ([]byte, error)
- func MarshalIndent(v any, prefix, indent string) ([]byte, error)
- func MarshalNoHTMLEscape(v any, prefix, indent string) ([]byte, error)
- func MarshalToString(v any) (string, error)
- func Unmarshal(data []byte, v any) error
- func UnmarshalFromString(data string, v any) error
- func Valid(data []byte) bool
- type Decoder
- type Encoder
- type Implementation
- type Marshaler
- type RawMessage
- type UnderlyingDecoder
- type UnderlyingEncoder
- type Unmarshaler
Constants ¶
This section is empty.
Variables ¶
var DefaultJSONIteratorImpl = NewJSONIteratorImpl( jsoniter.ConfigCompatibleWithStandardLibrary, true)
DefaultJSONIteratorImpl uses jsoniter.ConfigCompatibleWithStandardLibrary as the underlying implementation.
var HumanFriendly = struct { Marshal func(v any) ([]byte, error) MarshalToString func(v any) (string, error) MarshalIndent func(v any, prefix, indent string) ([]byte, error) MarshalIndentString func(v any, prefix, indent string) (string, error) NewEncoder func(w io.Writer) *Encoder }{ Marshal: hFriendlyMarshal, MarshalToString: hFriendlyMarshalToString, MarshalIndent: hFriendlyMarshalIndent, MarshalIndentString: hFriendlyMarshalIndentString, NewEncoder: newHumanFriendlyEncoder, }
HumanFriendly is a config which generates data that is more friendly for human reading. Also, this config can encode data with `interface{}` as map keys, in contrast, the standard library fails in this case.
Functions ¶
func ChangeImpl ¶ added in v2.13.0
func ChangeImpl(impl Implementation)
ChangeImpl changes the underlying JSON implementation on-the-fly at runtime.
You may see github.com/jxskiss/gopkg/_examples/perf/json/bytedance_sonic for an example to use github.com/bytedance/sonic as the underlying implementation.
func Compact ¶
Compact appends to dst the JSON-encoded src with insignificant space characters elided.
func Dump ¶
Dump writes v to the named file at path using JSON encoding. It disables HTMLEscape. Optionally indent can be applied to the output, empty prefix and indent disables indentation. The output is friendly to read by humans.
func Fdump ¶ added in v2.8.5
Fdump writes v to the given io.Writer using JSON encoding. It disables HTMLEscape. Optionally indent can be applied to the output, empty prefix and indent disables indentation. The output is friendly to read by humans.
func HTMLEscape ¶
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.
func Indent ¶
Indent appends to dst an indented form of the JSON-encoded src. See encoding/json.Indent for detailed document.
func Load ¶
Load reads JSON-encoded data from the named file at path and stores the result in the value pointed to by v.
func Marshal ¶
Marshal returns the JSON encoding of v.
See encoding/json.Marshal for detailed document.
func MarshalFastest ¶ added in v2.10.0
MarshalFastest uses the fastest config if the underlying implementation supports it, e.g. jsoniter and sonic. The result may be incompatible with std encoding/json in some ways, especially that map keys may be not sorted.
func MarshalIndent ¶
MarshalIndent is like Marshal but applies Indent to format the output.
See encoding/json.MarshalIndent for detailed document.
func MarshalNoHTMLEscape ¶
MarshalNoHTMLEscape is like Marshal but does not escape HTML characters. Optionally indent can be applied to the output, empty prefix and indent disables indentation. The output is more friendly to read for log messages.
func MarshalToString ¶
MarshalToString returns the JSON encoding of v as string.
See encoding/json.Marshal for detailed document.
func Unmarshal ¶
Unmarshal parses the JSON-encoded data and stores the result in the value pointed to by v.
See encoding/json.Unmarshal for detailed document.
func UnmarshalFromString ¶
UnmarshalFromString parses the JSON-encoded string data and stores the result in the value pointed to by v.
See encoding/json.Unmarshal for detailed document.
Types ¶
type Decoder ¶
type Decoder struct {
UnderlyingDecoder
}
Decoder is a wrapper of encoding/json.Decoder. It provides same methods as encoding/json.Decoder but with method chaining capabilities.
See encoding/json.Decoder for detailed document.
func NewDecoder ¶
NewDecoder returns a new Decoder that reads from r.
func (*Decoder) DisallowUnknownFields ¶
DisallowUnknownFields causes the Decoder to return an error when the destination is a struct and the input contains object keys which do not match any non-ignored, exported fields in the destination.
type Encoder ¶
type Encoder struct {
UnderlyingEncoder
}
Encoder is a wrapper of encoding/json.Encoder. It provides same methods as encoding/json.Encoder but with method chaining capabilities.
See encoding/json.Encoder for detailed document.
func NewEncoder ¶
NewEncoder returns a new Encoder that writes to w.
func (*Encoder) SetEscapeHTML ¶
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.
type Implementation ¶ added in v2.13.0
type Implementation interface { Marshal(v any) ([]byte, error) MarshalIndent(v any, prefix, indent string) ([]byte, error) Unmarshal(data []byte, v any) error Valid(data []byte) bool MarshalToString(v any) (string, error) UnmarshalFromString(data string, v any) error Compact(dst *bytes.Buffer, src []byte) error HTMLEscape(dst *bytes.Buffer, src []byte) Indent(dst *bytes.Buffer, src []byte, prefix, indent string) error MarshalFastest(v any) ([]byte, error) MarshalNoHTMLEscape(v any, prefix, indent string) ([]byte, error) NewEncoder(w io.Writer) UnderlyingEncoder NewDecoder(r io.Reader) UnderlyingDecoder }
Implementation is the interface of an underlying JSON implementation. User can change the underlying implementation on-the-fly at runtime.
var StdImpl Implementation = stdImpl{}
StdImpl uses package "encoding/json" in the standard library as the underlying implementation.
func NewJSONIteratorImpl ¶ added in v2.13.0
func NewJSONIteratorImpl(api jsoniter.API, useConfigFastest bool) Implementation
NewJSONIteratorImpl returns an implementation which uses api as the underlying config. If useConfigFastest is true, it uses jsoniter.ConfigFastest for method MarshalFastest, else it uses api.Marshal.
type Marshaler ¶
Marshaler is an alias name of encoding/json.Marshaler. See encoding/json.Marshaler for detailed document.
type RawMessage ¶
type RawMessage = json.RawMessage
RawMessage is a raw encoded JSON value. It implements Marshaler and Unmarshaler and can be used to delay JSON decoding or precompute a JSON encoding.
type UnderlyingDecoder ¶ added in v2.13.0
type UnderlyingEncoder ¶ added in v2.13.0
type Unmarshaler ¶
type Unmarshaler = json.Unmarshaler
Unmarshaler is an alias name of encoding/json.Unmarshaler. See encoding/json.Unmarshaler for detailed document.