Skip to content

std/encoding/json

import "github.com/eugenioenko/goalchemy/std/encoding/json"

Package json encodes and decodes JSON with the semantics of Go’s encoding/json. Without reflection, the compiler describes each statically known type passed to Marshal or pointed to by an Unmarshal target. Values held in interfaces are encoded when they are JSON-shaped (nil, booleans, numbers and strings of predeclared types, []byte, []any, map[string]any) or implement Marshaler or encoding.TextMarshaler, as Number, RawMessage and jsonvalue.Value do; other dynamic values return *UnsupportedTypeError.

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

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

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.

func Indent(dst *bytes.Buffer, src []byte, prefix, 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 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(v any) ([]byte, error)

Marshal returns the JSON encoding of v, as encoding/json.Marshal does.

func MarshalIndent(v any, prefix, 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.

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

Unmarshal parses the JSON-encoded data and stores the result in the value pointed to by v, as encoding/json.Unmarshal does.

func Valid(data []byte) bool

Valid reports whether data is a valid JSON encoding.

type Decoder struct {
}

A Decoder reads and decodes JSON values from an input stream, with the buffering, tokens and errors of the reference toolchain’s encoding/json.Decoder, which is built on encoding/json/jsontext.

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.

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

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.

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.

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.

func (dec *Decoder) More() bool

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

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.

func (dec *Decoder) UseNumber()

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

type Delim rune

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

func (d Delim) String() string
type Encoder struct {
}

An Encoder writes JSON values to an output stream.

func NewEncoder(w io.Writer) *Encoder

NewEncoder returns a new encoder that writes to w.

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, as encoding/json.Encoder.Encode does.

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 &, <, and >.

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 Indent(dst, src, prefix, indent). Calling SetIndent(“”, “”) disables indentation.

type InvalidUnmarshalError struct {
Type string
}

An InvalidUnmarshalError describes an invalid argument passed to Unmarshal. Type is the Go type name of the argument, or empty for nil.

func (e *InvalidUnmarshalError) Error() string
type Marshaler interface {
MarshalJSON() ([]byte, error)
}

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

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

A MarshalerError represents an error from calling a MarshalJSON or MarshalText method. Type is the Go type name.

func (e *MarshalerError) Error() string
func (e *MarshalerError) Unwrap() error

Unwrap returns the underlying error.

type Number string

A Number represents a JSON number literal.

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

Float64 returns the number as a float64.

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

Int64 returns the number as an int64.

func (n Number) MarshalJSON() ([]byte, error)

MarshalJSON returns the number literal, or 0 when n is empty.

func (n Number) String() string

String returns the literal text of the number.

func (n *Number) UnmarshalJSON(data []byte) error

UnmarshalJSON sets *n to a number literal, or to the number in a JSON string; null leaves *n unchanged.

type RawMessage []byte

RawMessage is a raw encoded JSON value.

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

MarshalJSON returns m as the JSON encoding of m.

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

UnmarshalJSON sets *m to a copy of data.

type SyntaxError struct {
Offset int64
// 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 (e *SyntaxError) Error() string
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.

type UnmarshalTypeError struct {
Value string
Type string
Offset int64
Struct string
Field string
Err error
}

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

func (e *UnmarshalTypeError) Error() string
func (e *UnmarshalTypeError) Unwrap() error

Unwrap returns the underlying error.

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

Unmarshaler is the interface implemented by types that can unmarshal a JSON description of themselves.

type UnsupportedTypeError struct {
Type string
}

An UnsupportedTypeError is returned by Marshal when attempting to encode an unsupported value type. Type is the Go type name.

func (e *UnsupportedTypeError) Error() string
type UnsupportedValueError struct {
Str string
}

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

func (e *UnsupportedValueError) Error() string