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.
Compact
Section titled “Compact”func Compact(dst *bytes.Buffer, src []byte) errorCompact appends to dst the JSON-encoded src with insignificant space characters elided.
HTMLEscape
Section titled “HTMLEscape”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.
Indent
Section titled “Indent”func Indent(dst *bytes.Buffer, src []byte, prefix, indent string) errorIndent 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.
Marshal
Section titled “Marshal”func Marshal(v any) ([]byte, error)Marshal returns the JSON encoding of v, as encoding/json.Marshal does.
MarshalIndent
Section titled “MarshalIndent”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.
Unmarshal
Section titled “Unmarshal”func Unmarshal(data []byte, v any) errorUnmarshal 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) boolValid reports whether data is a valid JSON encoding.
type Decoder
Section titled “type Decoder”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.
NewDecoder
Section titled “NewDecoder”func NewDecoder(r io.Reader) *DecoderNewDecoder 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.
Decoder.Buffered
Section titled “Decoder.Buffered”func (dec *Decoder) Buffered() io.ReaderBuffered returns a reader of the data remaining in the Decoder’s buffer. The reader is valid until the next call to Decode.
Decoder.Decode
Section titled “Decoder.Decode”func (dec *Decoder) Decode(v any) errorDecode reads the next JSON-encoded value from its input and stores it in the value pointed to by v.
Decoder.DisallowUnknownFields
Section titled “Decoder.DisallowUnknownFields”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.
Decoder.InputOffset
Section titled “Decoder.InputOffset”func (dec *Decoder) InputOffset() int64InputOffset 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.
Decoder.More
Section titled “Decoder.More”func (dec *Decoder) More() boolMore reports whether there is another element in the current array or object being parsed.
Decoder.Token
Section titled “Decoder.Token”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.
Decoder.UseNumber
Section titled “Decoder.UseNumber”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
Section titled “type Delim”type Delim runeA Delim is a JSON array or object delimiter, one of [ ] { or }.
Delim.String
Section titled “Delim.String”func (d Delim) String() stringtype Encoder
Section titled “type Encoder”type Encoder struct {}An Encoder writes JSON values to an output stream.
NewEncoder
Section titled “NewEncoder”func NewEncoder(w io.Writer) *EncoderNewEncoder returns a new encoder that writes to w.
Encoder.Encode
Section titled “Encoder.Encode”func (enc *Encoder) Encode(v any) errorEncode 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.
Encoder.SetEscapeHTML
Section titled “Encoder.SetEscapeHTML”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 >.
Encoder.SetIndent
Section titled “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 Indent(dst, src, prefix, indent). Calling SetIndent(“”, “”) disables indentation.
type InvalidUnmarshalError
Section titled “type InvalidUnmarshalError”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.
InvalidUnmarshalError.Error
Section titled “InvalidUnmarshalError.Error”func (e *InvalidUnmarshalError) Error() stringtype Marshaler
Section titled “type Marshaler”type Marshaler interface { MarshalJSON() ([]byte, error)}Marshaler is the interface implemented by types that can marshal themselves into valid JSON.
type MarshalerError
Section titled “type MarshalerError”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.
MarshalerError.Error
Section titled “MarshalerError.Error”func (e *MarshalerError) Error() stringMarshalerError.Unwrap
Section titled “MarshalerError.Unwrap”func (e *MarshalerError) Unwrap() errorUnwrap returns the underlying error.
type Number
Section titled “type Number”type Number stringA Number represents a JSON number literal.
Number.Float64
Section titled “Number.Float64”func (n Number) Float64() (float64, error)Float64 returns the number as a float64.
Number.Int64
Section titled “Number.Int64”func (n Number) Int64() (int64, error)Int64 returns the number as an int64.
Number.MarshalJSON
Section titled “Number.MarshalJSON”func (n Number) MarshalJSON() ([]byte, error)MarshalJSON returns the number literal, or 0 when n is empty.
Number.String
Section titled “Number.String”func (n Number) String() stringString returns the literal text of the number.
Number.UnmarshalJSON
Section titled “Number.UnmarshalJSON”func (n *Number) UnmarshalJSON(data []byte) errorUnmarshalJSON sets *n to a number literal, or to the number in a JSON string; null leaves *n unchanged.
type RawMessage
Section titled “type RawMessage”type RawMessage []byteRawMessage is a raw encoded JSON value.
RawMessage.MarshalJSON
Section titled “RawMessage.MarshalJSON”func (m RawMessage) MarshalJSON() ([]byte, error)MarshalJSON returns m as the JSON encoding of m.
RawMessage.UnmarshalJSON
Section titled “RawMessage.UnmarshalJSON”func (m *RawMessage) UnmarshalJSON(data []byte) errorUnmarshalJSON sets *m to a copy of data.
type SyntaxError
Section titled “type SyntaxError”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.
SyntaxError.Error
Section titled “SyntaxError.Error”func (e *SyntaxError) Error() stringtype Token
Section titled “type Token”type Token anyA 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
Section titled “type UnmarshalTypeError”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.
UnmarshalTypeError.Error
Section titled “UnmarshalTypeError.Error”func (e *UnmarshalTypeError) Error() stringUnmarshalTypeError.Unwrap
Section titled “UnmarshalTypeError.Unwrap”func (e *UnmarshalTypeError) Unwrap() errorUnwrap returns the underlying error.
type Unmarshaler
Section titled “type Unmarshaler”type Unmarshaler interface { UnmarshalJSON([]byte) error}Unmarshaler is the interface implemented by types that can unmarshal a JSON description of themselves.
type UnsupportedTypeError
Section titled “type UnsupportedTypeError”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.
UnsupportedTypeError.Error
Section titled “UnsupportedTypeError.Error”func (e *UnsupportedTypeError) Error() stringtype UnsupportedValueError
Section titled “type UnsupportedValueError”type UnsupportedValueError struct { Str string}An UnsupportedValueError is returned by Marshal when attempting to encode an unsupported value.
UnsupportedValueError.Error
Section titled “UnsupportedValueError.Error”func (e *UnsupportedValueError) Error() string