json

package
v0.10.7 Latest Latest
Warning

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

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

Documentation

Index

Constants

View Source
const (
	ErrMaxBodySizeExceeded = "json body size exceeds the maximum allowed size, limit is %d bytes"
	ErrSyntaxError         = "json body contains badly-formed JSON at position %d"
	ErrUnknownField        = "json body contains an unknown field %s"
)

Variables

View Source
var (
	ErrCodeInvalidContentType         *string
	ErrCodeFailedToReadBody           *string
	ErrCodeNilDestination             *string
	ErrCodeMarshalResponseBodyFailed  *string
	ErrCodeUnmarshalRequestBodyFailed *string
	ErrCodeSyntaxError                *string
	ErrCodeUnmarshalTypeError         *string
	ErrCodeUnknownField               *string
	ErrCodeEmptyBody                  *string
	ErrCodeMaxBodySizeExceeded        *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")
	ErrUnexpectedEOF       = errors.New("json body contains badly-formed JSON")
	ErrEmptyBody           = errors.New("json body is empty")
	ErrInvalidContentType  = gonethttpresponse.NewFailResponseError(
		"Content-Type",
		"invalid content type, expected application/json",
		ErrCodeInvalidContentType,
		http.StatusUnsupportedMediaType,
	)
)

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

Parameters:

  • w: The HTTP response writer
  • err: The error that occurred during decoding
  • encoder: The encoder to use for the response

Returns:

  • error: An error if the encoder is nil or if encoding the response fails

func CheckContentType added in v0.9.1

func CheckContentType(r *http.Request) bool

CheckContentType checks if the content type is JSON

Parameters:

  • r: The HTTP request

Returns:

  • bool: True if the content type is JSON, false otherwise

func NewMaxBodySizeExceededErrorResponse added in v0.9.1

func NewMaxBodySizeExceededErrorResponse(limit int64) gonethttpresponse.Response

NewMaxBodySizeExceededErrorResponse creates a new response for a body size exceeded error

Parameters:

  • limit: The maximum allowed body size

Returns:

  • gonethttpresponse.Response: The response

func NewSyntaxErrorResponse added in v0.9.1

func NewSyntaxErrorResponse(
	offset int64,
) gonethttpresponse.Response

NewSyntaxErrorResponse creates a new response for a SyntaxError

Parameters:

  • offset: The offset where the error occurred

func NewUnknownFieldErrorResponse added in v0.9.1

func NewUnknownFieldErrorResponse(fieldName string) gonethttpresponse.Response

NewUnknownFieldErrorResponse creates a new response for an unknown field error

Parameters:

  • fieldName: The name of the unknown field

Returns:

  • gonethttpresponse.Response: The response

func NewUnmarshalTypeErrorResponse added in v0.9.1

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

NewUnmarshalTypeErrorResponse creates a new response for an UnmarshalTypeError

Parameters:

  • fieldName: The name of the field that caused the error
  • fieldTypeName: The type name of the field that caused the error

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

Parameters:

  • mode: The flag mode
  • encoder: The JSON encoder

Returns:

  • *DefaultDecoder: The default decoder
  • error: The error if any

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

Parameters:

  • w: The HTTP response writer
  • r: The HTTP request
  • dest: The destination to store the decoded body

Returns:

  • error: The error if any

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

Parameters:

  • mode: The flag mode

Returns:

  • *DefaultEncoder: The default 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

Parameters:

  • w: The HTTP response writer
  • response: The response to encode

Returns:

  • error: The error if any

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

Parameters:

  • mode: The flag mode
  • encoder: The JSON encoder

Returns:

  • *DefaultStreamDecoder: The default decoder
  • error: The error if any

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

Parameters:

  • w: The HTTP response writer
  • r: The HTTP request
  • dest: The destination to store the decoded body

Returns:

  • error: The error if any

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

Parameters:

  • mode: The flag mode

Returns:

  • *DefaultStreamEncoder: The default encoder

func (DefaultStreamEncoder) Encode

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

Parameters:

  • w: The HTTP response writer
  • response: The response to encode

Returns:

  • error: The error if any

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