Documentation
¶
Index ¶
- Constants
- Variables
- func ByteArrayAsString(es *encodeState)
- func Marshal(v interface{}) ([]byte, error)
- func MarshalTo(v interface{}, w Writer) error
- func NilMapEmpty(es *encodeState)
- func NilSliceEmpty(es *encodeState)
- func NoStringEscaping(es *encodeState)
- func RawByteSlices(es *encodeState)
- func Register(typ reflect.Type) error
- func UnixTimestamp(es *encodeState)
- func UnsortedMap(es *encodeState)
- type DurationFmt
- type Encoder
- type Instruction
- type MarshalerError
- type Option
- type TypeMismatchError
- type UnsupportedTypeError
- type UnsupportedValueError
- type Writer
Constants ¶
const ( DurationString = iota DurationMinutes DurationSeconds DurationMilliseconds DurationMicroseconds DurationNanoseconds )
Duration formats.
Variables ¶
var ErrInvalidWriter = errors.New("invalid writer")
ErrInvalidWriter is the error returned by an Encoder's Encode method when the given Writer is invalid.
Functions ¶
func ByteArrayAsString ¶
func ByteArrayAsString(es *encodeState)
ByteArrayAsString encodes byte arrays as raw JSON strings.
func Marshal ¶ added in v0.3.0
Marshal returns the JSON encoding of v. It uses a pre-compiled encoder from the package's cache, or create and store one on the fly.
func MarshalTo ¶ added in v0.3.0
MarshalTo writes the JSON encoding of v to w. It uses a pre-compiled encoder from the package's cache, or create and store one on the fly.
func NilMapEmpty ¶
func NilMapEmpty(es *encodeState)
NilMapEmpty encodes a nil Go map as an empty JSON object, rather than null.
func NilSliceEmpty ¶
func NilSliceEmpty(es *encodeState)
NilSliceEmpty encodes a nil Go slice as an empty JSON array, rather than null.
func NoStringEscaping ¶
func NoStringEscaping(es *encodeState)
NoStringEscaping disables string escaping.
func RawByteSlices ¶
func RawByteSlices(es *encodeState)
RawByteSlices disables the default behavior that encodes byte slices as base64-encoded strings.
func Register ¶ added in v0.3.0
Register records a new compiled encoder for the given type to the cache used by the global functions Marshal and MarshalTo. This may be used during the initialization of a program to speed up the first calls to previously mentioned functions.
func UnixTimestamp ¶
func UnixTimestamp(es *encodeState)
UnixTimestamp configures the encoder to encode time.Time value as Unix timestamps. This setting has precedence over any time layout.
Types ¶
type DurationFmt ¶
type DurationFmt int
DurationFmt represents the format used to encode a time.Duration.
func (DurationFmt) String ¶
func (df DurationFmt) String() string
String implements the fmt.Stringer interface.
type Encoder ¶
type Encoder struct {
// contains filtered or unexported fields
}
Encoder writes the JSON values of a specific type using a set of instructions compiled when the encoder is instantiated.
func NewEncoder ¶
NewEncoder returns a new encoder that can marshal the values of the given type. The Encoder can be explicitly initialized by calling its Compile method, otherwise the operation is done on first call to Marshal.
type Instruction ¶
Instruction represents a function that writes the JSON representation of a value to a stream.
type MarshalerError ¶
MarshalerError represents an error from calling a MarshalJSON or MarshalText method.
func (*MarshalerError) Error ¶
func (e *MarshalerError) Error() string
Error implements the builtin error interface.
func (*MarshalerError) Unwrap ¶ added in v0.3.0
func (e *MarshalerError) Unwrap() error
type Option ¶
type Option func(*encodeState)
Option represents an option that defines the behavior of an encoder.
func DurationFormat ¶
func DurationFormat(df DurationFmt) Option
DurationFormat sets the format used to encode a time.Duration value.
func TimeLayout ¶
TimeLayout sets the time layout used to encode a time.Time value.
type TypeMismatchError ¶ added in v0.3.0
TypeMismatchError is the error returned by an Encoder's Encode method whhen the type of the input value does not match the type for which the encoder was compiled.
func (*TypeMismatchError) Error ¶ added in v0.3.0
func (e *TypeMismatchError) Error() string
Error implements the builtin error interface.
type UnsupportedTypeError ¶
UnsupportedTypeError is the error returned by an Encoder's Encode method when attempting to encode an unsupported value type.
func (*UnsupportedTypeError) Error ¶
func (e *UnsupportedTypeError) Error() string
Error implements the bultin error interface.
type UnsupportedValueError ¶
type UnsupportedValueError struct {
Str string
}
UnsupportedValueError is the error returned by an Encoder's Encode method when attempting to encode an unsupported value.
func (*UnsupportedValueError) Error ¶
func (e *UnsupportedValueError) Error() string
Error implements the builtin error interface.
type Writer ¶
type Writer interface {
io.Writer
io.StringWriter
io.ByteWriter
}
Writer is an interface that groups the io.Writer, io.StringWriter and io.ByteWriter interfaces.
