json

package
v0.11.2 Latest Latest
Warning

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

Go to latest
Published: Oct 13, 2025 License: GPL-3.0 Imports: 8 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
	ErrCodeNilDestination             *string
	ErrCodeFailedToReadBody           *string
	ErrCodeUnmarshalRequestBodyFailed *string
	ErrCodeSyntaxError                *string
	ErrCodeUnmarshalTypeError         *string
	ErrCodeUnknownField               *string
	ErrCodeEmptyBody                  *string
	ErrCodeMaxBodySizeExceeded        *string
)
View Source
var (
	ErrUnexpectedEOF       = errors.New("json body contains badly-formed JSON")
	ErrEmptyBody           = errors.New("json body is empty")
	ErrUnmarshalBodyFailed = errors.New("failed to unmarshal json body")
)
View Source
var (
	ErrNilDestination     = errors.New("json destination is nil")
	ErrInvalidContentType = gonethttpresponse.NewFailResponseError(
		"Content-Type",
		"invalid content type, expected application/json",
		ErrCodeInvalidContentType,
		http.StatusUnsupportedMediaType,
	)
)

Functions

func BodyDecodeErrorHandler

func BodyDecodeErrorHandler(
	w http.ResponseWriter,
	err error,
	encoder gonethttpresponse.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

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

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

func NewSyntaxErrorResponse(
	offset int64,
) gonethttpresponse.Response

NewSyntaxErrorResponse creates a new response for a SyntaxError

Parameters:

  • offset: The offset where the error occurred

func NewUnknownFieldErrorResponse

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

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 added in v0.11.2

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

Decoder struct

func NewDecoder added in v0.11.2

func NewDecoder(
	mode *goflagsmode.Flag,
	encoder gonethttpresponse.Encoder,
) (*Decoder, error)

NewDecoder creates a new JSON decoder

Parameters:

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

Returns:

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

func (Decoder) Decode added in v0.11.2

func (d Decoder) 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 StreamDecoder added in v0.11.2

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

StreamDecoder is the JSON decoder struct

func NewStreamDecoder added in v0.11.2

func NewStreamDecoder(
	mode *goflagsmode.Flag,
	encoder gonethttpresponse.Encoder,
) (*StreamDecoder, error)

NewStreamDecoder creates a new JSON decoder

Parameters:

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

Returns:

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

func (StreamDecoder) Decode added in v0.11.2

func (d StreamDecoder) 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

Jump to

Keyboard shortcuts

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