json

package
v0.12.4 Latest Latest
Warning

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

Go to latest
Published: Oct 17, 2025 License: GPL-3.0 Imports: 10 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 (
	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")
)

Functions

func BodyDecodeErrorHandler

func BodyDecodeErrorHandler(
	err error,
) error

BodyDecodeErrorHandler handles the error on JSON body decoding

Parameters:

  • err: The error that occurred during decoding

Returns:

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

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,
) *Decoder

NewDecoder creates a new JSON decoder

Parameters:

  • mode: The flag mode

Returns:

  • *Decoder: The default decoder

func (Decoder) Decode added in v0.11.2

func (d Decoder) Decode(
	body interface{},
	dest interface{},
) error

Decode decodes the JSON body from an any value and stores it in the destination

Parameters:

  • body: The body to decode
  • dest: The destination to store the decoded body

Returns:

  • error: The error if any

func (Decoder) DecodeReader added in v0.12.2

func (d Decoder) DecodeReader(
	reader io.Reader,
	dest interface{},
) (err error)

DecodeReader decodes the JSON body and stores it in the destination

Parameters:

  • reader: The reader to read the body from
  • dest: The destination to store the decoded body

Returns:

  • error: The error if any

func (Decoder) DecodeRequest added in v0.12.2

func (d Decoder) DecodeRequest(
	r *http.Request,
	dest interface{},
) error

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

Parameters:

  • request: 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,
) *StreamDecoder

NewStreamDecoder creates a new JSON decoder

Parameters:

  • mode: The flag mode
  • responsesHandler: The HTTP handler to handle errors

Returns:

  • *StreamDecoder: The default decoder

func (StreamDecoder) Decode added in v0.11.2

func (s StreamDecoder) Decode(
	body interface{},
	dest interface{},
) error

Decode decodes the JSON body from an any value and stores it in the destination

Parameters:

  • body: The body to decode
  • dest: The destination to store the decoded body

Returns:

  • error: The error if any

func (StreamDecoder) DecodeReader added in v0.12.2

func (s StreamDecoder) DecodeReader(
	reader io.Reader,
	dest interface{},
) error

DecodeReader decodes a JSON body from a reader into a destination

Parameters:

  • reader: The reader to read the body from
  • dest: The destination to store the decoded body

Returns:

  • error: The error if any

func (StreamDecoder) DecodeRequest added in v0.12.2

func (s StreamDecoder) DecodeRequest(
	r *http.Request,
	dest interface{},
) error

DecodeRequest decodes a JSON body from an HTTP request into a destination

Parameters:

  • request: The HTTP request to read the body from
  • 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