All packages

    encoding/json

    import "encoding/json"

    Package json implements encoding and decoding of JSON as defined in RFC 7159. The mapping between JSON and Go values is described in the documentation for the Marshal and Unmarshal functions. See "JSON and Go" for an introduction to this package: https://golang.org/doc/articles/json_and_go.html # Security Considerations The JSON standard (RFC 7159) is lax in its definition of a number of parser behaviors. As such, many JSON parsers behave differently in various scenarios. These differences...

    Go 1.26.1

    Functions

    func Compact(dst *bytes.Buffer, src []byte) error

    Compact appends to dst the JSON-encoded src with insignificant space characters elided.

    encoding/json/indent.go:43

    func HTMLEscape(dst *bytes.Buffer, src []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.

    encoding/json/indent.go:16

    func Indent(dst *bytes.Buffer, src []byte, prefix string, indent string) error

    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...

    encoding/json/indent.go:120

    func Marshal(v any) ([]byte, error)

    Marshal returns the JSON encoding of v. Marshal traverses the value v recursively. If an encountered value implements [Marshaler] and is not a nil pointer, Marshal calls [Marshaler.MarshalJSON] to produce JSON. If no [Marshaler.MarshalJSON] method is present but the value implements [encoding.TextMarshaler] instead, Marshal calls [encoding.TextMarshaler.MarshalText] and encodes the result as a JSON string. The nil pointer exception is not strictly necessary but mimics a similar, necessary ex...

    encoding/json/encode.go:205

    func MarshalIndent(v any, prefix string, indent string) ([]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.

    encoding/json/encode.go:221

    func NewDecoder(r io.Reader) *Decoder

    NewDecoder returns a new decoder that reads from r. The decoder introduces its own buffering and may read data from r beyond the JSON values requested.

    encoding/json/stream.go:33

    func NewEncoder(w io.Writer) *Encoder

    NewEncoder returns a new encoder that writes to w.

    encoding/json/stream.go:194

    func Unmarshal(data []byte, v any) error

    Unmarshal parses the JSON-encoded data and stores the result in the value pointed to by v. If v is nil or not a pointer, Unmarshal returns an [InvalidUnmarshalError]. Unmarshal uses the inverse of the encodings that [Marshal] uses, allocating maps, slices, and pointers as necessary, with the following additional rules: To unmarshal JSON into a pointer, Unmarshal first handles the case of the JSON being the JSON literal null. In that case, Unmarshal sets the pointer to nil. Otherwise, Unmars...

    encoding/json/decode.go:102

    func Valid(data []byte) bool

    Valid reports whether data is a valid JSON encoding.

    encoding/json/scanner.go:24

    Types

    type Decoder struct {
    	// contains filtered or unexported fields
    }

    A Decoder reads and decodes JSON values from an input stream.

    encoding/json/stream.go:16

    func (dec *Decoder) Buffered() io.Reader

    Buffered returns a reader of the data remaining in the Decoder's buffer. The reader is valid until the next call to [Decoder.Decode].

    encoding/json/stream.go:85

    func (dec *Decoder) Decode(v any) error

    Decode reads the next JSON-encoded value from its input and stores it in the value pointed to by v. See the documentation for [Unmarshal] for details about the conversion of JSON into a Go value.

    encoding/json/stream.go:51

    func (dec *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.

    encoding/json/stream.go:44

    func (dec *Decoder) InputOffset() int64

    InputOffset returns the input stream byte offset of the current decoder position. The offset gives the location of the end of the most recently returned token and the beginning of the next token.

    encoding/json/stream.go:512

    func (dec *Decoder) More() bool

    More reports whether there is another element in the current array or object being parsed.

    encoding/json/stream.go:485

    func (dec *Decoder) Token() (Token, error)

    Token returns the next JSON token in the input stream. At the end of the input stream, Token returns nil, [io.EOF]. Token guarantees that the delimiters [ ] { } it returns are properly nested and matched: if Token encounters an unexpected delimiter in the input, it will return an error. The input stream consists of basic JSON values—bool, string, number, and null—along with delimiters [ ] { } of type [Delim] to mark the start and end of arrays and objects. Commas and colons are elided.

    encoding/json/stream.go:370

    func (dec *Decoder) UseNumber()

    UseNumber causes the Decoder to unmarshal a number into an interface value as a [Number] instead of as a float64.

    encoding/json/stream.go:39

    type Delim rune

    A Delim is a JSON array or object delimiter, one of [ ] { or }.

    encoding/json/stream.go:353

    func (d Delim) String() string

    encoding/json/stream.go:355

    type Encoder struct {
    	// contains filtered or unexported fields
    }

    An Encoder writes JSON values to an output stream.

    encoding/json/stream.go:183

    func (enc *Encoder) Encode(v any) error

    Encode writes the JSON encoding of v to the stream, with insignificant space characters elided, followed by a newline character. See the documentation for [Marshal] for details about the conversion of Go values to JSON.

    encoding/json/stream.go:204

    func (enc *Encoder) SetEscapeHTML(on bool)

    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.

    encoding/json/stream.go:254

    func (enc *Encoder) SetIndent(prefix string, indent string)

    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.

    encoding/json/stream.go:242

    type InvalidUnmarshalError struct {
    	Type reflect.Type
    }

    An InvalidUnmarshalError describes an invalid argument passed to [Unmarshal]. (The argument to [Unmarshal] must be a non-nil pointer.)

    encoding/json/decode.go:158

    func (e *InvalidUnmarshalError) Error() string

    encoding/json/decode.go:162

    type InvalidUTF8Error struct {
    	S string // the whole string value that caused the error
    }

    Before Go 1.2, an InvalidUTF8Error was returned by [Marshal] when attempting to encode a string value with invalid UTF-8 sequences. As of Go 1.2, [Marshal] instead coerces the string to valid UTF-8 by replacing invalid bytes with the Unicode replacement rune U+FFFD. Deprecated: No longer used; kept for compatibility.

    encoding/json/encode.go:267

    func (e *InvalidUTF8Error) Error() string

    encoding/json/encode.go:271

    type Marshaler interface {
    	MarshalJSON() ([]byte, error)
    }

    Marshaler is the interface implemented by types that can marshal themselves into valid JSON.

    encoding/json/encode.go:236

    type MarshalerError struct {
    	Type reflect.Type
    	Err  error
    	// contains filtered or unexported fields
    }

    A MarshalerError represents an error from calling a [Marshaler.MarshalJSON] or [encoding.TextMarshaler.MarshalText] method.

    encoding/json/encode.go:277

    func (e *MarshalerError) Error() string

    encoding/json/encode.go:283

    func (e *MarshalerError) Unwrap() error

    Unwrap returns the underlying error.

    encoding/json/encode.go:294

    type Number string

    A Number represents a JSON number literal.

    encoding/json/decode.go:191

    func (n Number) Float64() (float64, error)

    Float64 returns the number as a float64.

    encoding/json/decode.go:197

    func (n Number) Int64() (int64, error)

    Int64 returns the number as an int64.

    encoding/json/decode.go:202

    func (n Number) String() string

    String returns the literal text of the number.

    encoding/json/decode.go:194

    type RawMessage []byte

    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.

    encoding/json/stream.go:261

    func (m RawMessage) MarshalJSON() ([]byte, error)

    MarshalJSON returns m as the JSON encoding of m.

    encoding/json/stream.go:264

    func (m *RawMessage) UnmarshalJSON(data []byte) error

    UnmarshalJSON sets *m to a copy of data.

    encoding/json/stream.go:272

    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.

    encoding/json/scanner.go:49

    func (e *SyntaxError) Error() string

    encoding/json/scanner.go:54

    type Token any

    A Token holds a value of one of these types: - [Delim], for the four JSON delimiters [ ] { } - bool, for JSON booleans - float64, for JSON numbers - [Number], for JSON numbers - string, for JSON string literals - nil, for JSON null

    encoding/json/stream.go:291

    type Unmarshaler interface {
    	UnmarshalJSON([]byte) error
    }

    Unmarshaler is the interface implemented by types that can unmarshal a JSON description of themselves. The input can be assumed to be a valid encoding of a JSON value. UnmarshalJSON must copy the JSON data if it wishes to retain the data after returning.

    encoding/json/decode.go:121

    type UnmarshalFieldError struct {
    	Key   string
    	Type  reflect.Type
    	Field reflect.StructField
    }

    An UnmarshalFieldError describes a JSON object key that led to an unexported (and therefore unwritable) struct field. Deprecated: No longer used; kept for compatibility.

    encoding/json/decode.go:146

    func (e *UnmarshalFieldError) Error() string

    encoding/json/decode.go:152

    type UnmarshalTypeError struct {
    	Value  string       // description of JSON value - "bool", "array", "number -5"
    	Type   reflect.Type // type of Go value it could not be assigned to
    	Offset int64 ...

    An UnmarshalTypeError describes a JSON value that was not appropriate for a value of a specific Go type.

    encoding/json/decode.go:127

    func (e *UnmarshalTypeError) Error() string

    encoding/json/decode.go:135

    type UnsupportedTypeError struct {
    	Type reflect.Type
    }

    An UnsupportedTypeError is returned by [Marshal] when attempting to encode an unsupported value type.

    encoding/json/encode.go:242

    func (e *UnsupportedTypeError) Error() string

    encoding/json/encode.go:246

    type UnsupportedValueError struct {
    	Value reflect.Value
    	Str   string
    }

    An UnsupportedValueError is returned by [Marshal] when attempting to encode an unsupported value.

    encoding/json/encode.go:252

    func (e *UnsupportedValueError) Error() string

    encoding/json/encode.go:257