json

package
v0.9.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 4, 2025 License: GPL-3.0 Imports: 9 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrCodeUnmarshalRequestBodyFailed *string
	ErrCodeSyntaxError                *string
	ErrCodeUnmarshalTypeError         *string
	ErrCodeUnknownField               *string
	ErrCodeEmptyBody                  *string
	ErrCodeMaxBodySizeExceeded        *string
)
View Source
var (
	ErrCodeFailedToReadBody *string
	ErrCodeNilDestination   *string
)
View Source
var (
	ErrNilEncoder          = errors.New("json encoder is nil")
	ErrNilDecoder          = errors.New("json decoder is nil")
	ErrUnmarshalBodyFailed = errors.New("failed to unmarshal json body")
	ErrInvalidContentType  = gonethttpresponse.NewHeaderError(
		"Content-Type",
		"invalid content type, expected application/json",
		ErrCodeInvalidContentType,
		http.StatusUnsupportedMediaType,
	)
	ErrMaxBodySizeExceeded = "json body size exceeds the maximum allowed size, limit is %d bytes"
	ErrSyntaxError         = "json body contains badly-formed JSON at position %d"
	ErrUnexpectedEOF       = errors.New("json body contains badly-formed JSON")
	ErrEmptyBody           = errors.New("json body is empty")
	ErrUnknownField        = "json body contains an unknown field %s"
)
View Source
var (
	ErrCodeInvalidContentType *string
)
View Source
var (
	ErrCodeMarshalResponseBodyFailed *string
)

Functions

func BodyDecodeErrorHandler added in v0.9.1

func BodyDecodeErrorHandler(
	w http.ResponseWriter,
	err error,
	encoder Encoder,
) error

BodyDecodeErrorHandler handles the error on JSON body decoding

func CheckContentType added in v0.9.1

func CheckContentType(r *http.Request) bool

CheckContentType checks if the content type is JSON

func NewMaxBodySizeExceededErrorResponse added in v0.9.1

func NewMaxBodySizeExceededErrorResponse(limit int64) gonethttpresponse.Response

NewMaxBodySizeExceededErrorResponse creates a new response for a body size exceeded error

func NewSyntaxErrorResponse added in v0.9.1

func NewSyntaxErrorResponse(offset int64) gonethttpresponse.Response

NewSyntaxErrorResponse creates a new response for a SyntaxError

func NewUnknownFieldErrorResponse added in v0.9.1

func NewUnknownFieldErrorResponse(fieldName string) gonethttpresponse.Response

NewUnknownFieldErrorResponse creates a new response for an unknown field error

func NewUnmarshalTypeErrorResponse added in v0.9.1

func NewUnmarshalTypeErrorResponse(
	fieldName string,
	fieldTypeName string,
) gonethttpresponse.Response

NewUnmarshalTypeErrorResponse creates a new response for an UnmarshalTypeError

Types

type Decoder

type Decoder interface {
	Decode(
		w http.ResponseWriter,
		r *http.Request,
		dest interface{},
	) (err error)
}

Decoder interface

type DefaultDecoder

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

DefaultDecoder struct

func NewDefaultDecoder

func NewDefaultDecoder(
	mode *goflagsmode.Flag,
	encoder Encoder,
) (*DefaultDecoder, error)

NewDefaultDecoder creates a new JSON decoder

func (*DefaultDecoder) Decode

func (d *DefaultDecoder) Decode(
	w http.ResponseWriter,
	r *http.Request,
	dest interface{},
) (err error)

Decode decodes the JSON request body and stores it in the destination

type DefaultEncoder

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

DefaultEncoder struct

func NewDefaultEncoder

func NewDefaultEncoder(mode *mode.Flag) *DefaultEncoder

NewDefaultEncoder creates a new default JSON encoder

func (*DefaultEncoder) Encode

func (d *DefaultEncoder) Encode(
	w http.ResponseWriter,
	response gonethttpresponse.Response,
) (err error)

Encode encodes the body into JSON and writes it to the response

type DefaultStreamDecoder

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

DefaultStreamDecoder is the JSON decoder struct

func NewDefaultStreamDecoder

func NewDefaultStreamDecoder(
	mode *goflagsmode.Flag,
	encoder Encoder,
) (*DefaultStreamDecoder, error)

NewDefaultStreamDecoder creates a new JSON decoder

func (*DefaultStreamDecoder) Decode

func (d *DefaultStreamDecoder) Decode(
	w http.ResponseWriter,
	r *http.Request,
	dest interface{},
) (err error)

Decode decodes the JSON request body and stores it in the destination

type DefaultStreamEncoder

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

DefaultStreamEncoder is the JSON encoder struct

func NewDefaultStreamEncoder

func NewDefaultStreamEncoder(mode *goflagsmode.Flag) *DefaultStreamEncoder

NewDefaultStreamEncoder creates a new JSON encoder

func (*DefaultStreamEncoder) Encode

func (d *DefaultStreamEncoder) Encode(
	w http.ResponseWriter,
	response gonethttpresponse.Response,
) (err error)

Encode encodes the body into JSON and writes it to the response

type Encoder

type Encoder interface {
	Encode(
		w http.ResponseWriter,
		response gonethttpresponse.Response,
	) error
}

Encoder interface

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL