json

package
v0.7.5 Latest Latest
Warning

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

Go to latest
Published: Aug 21, 2025 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

Package json provides utilities for handling JSON requests and responses in HTTP handlers. It offers structured error handling for JSON parsing and marshaling with configurable options.

Index

Constants

View Source
const (
	DefaultMaxBytes = 1_048_576 // Default max request body size (1MB)
)

Variables

This section is empty.

Functions

This section is empty.

Types

type JSONResponder

type JSONResponder struct {
	responder.Responder
	// contains filtered or unexported fields
}

JSONResponder handles JSON encoding and decoding for HTTP requests and responses. It provides structured error handling and configurable request body limits.

func New

func New(logger *slog.Logger, opts ...Options) *JSONResponder

New creates a new JSONResponder with the provided logger and optional configuration. The logger is used for structured logging throughout the JSON handling process. If no options are provided, DefaultMaxBytes (1MB) is used as the request body limit.

func (*JSONResponder) BadRequestResponse

func (jr *JSONResponder) BadRequestResponse(w http.ResponseWriter, r *http.Request, err error)

BadRequestResponse sends a 400 Bad Request response with the error message. It returns a JSON error response to the client without logging the error.

func (*JSONResponder) FailedValidationResponse

func (jr *JSONResponder) FailedValidationResponse(w http.ResponseWriter, r *http.Request, errors map[string]string)

FailedValidationResponse sends a 422 Unprocessable Entity response with validation errors. The errors parameter should contain field names mapped to their validation error messages.

func (*JSONResponder) InvalidBearerAuthenticationTokenResponse

func (jr *JSONResponder) InvalidBearerAuthenticationTokenResponse(w http.ResponseWriter, r *http.Request)

InvalidBearerAuthenticationTokenResponse sends a 401 Unauthorized response for invalid bearer tokens. It sets the WWW-Authenticate: Bearer header to inform the client that bearer token authentication is required.

func (*JSONResponder) InvalidCookieAuthenticationTokenResponse

func (jr *JSONResponder) InvalidCookieAuthenticationTokenResponse(w http.ResponseWriter, r *http.Request)

InvalidCookieAuthenticationTokenResponse sends a 401 Unauthorized response for invalid cookie tokens. It sets the WWW-Authenticate: Cookie header to inform the client that cookie authentication is required.

func (*JSONResponder) InvalidCredentialsResponse

func (jr *JSONResponder) InvalidCredentialsResponse(w http.ResponseWriter, r *http.Request)

InvalidCredentialsResponse sends a 401 Unauthorized response for invalid login credentials. This is typically used when username/password authentication fails.

func (*JSONResponder) NotFound

func (jr *JSONResponder) NotFound(w http.ResponseWriter, r *http.Request, err error)

NotFound sends a 404 Not Found response with the error message. It logs the error details and returns a JSON error response to the client.

func (*JSONResponder) ReadJSON

func (jr *JSONResponder) ReadJSON(w http.ResponseWriter, r *http.Request, dst any) error

ReadJSON reads and decodes JSON from the HTTP request body into the provided destination. It enforces the configured maximum body size and provides detailed error messages for various JSON parsing failures including syntax errors, type mismatches, and unknown fields. The method ensures only a single JSON value is present in the request body.

func (*JSONResponder) ServerErrorResponse

func (jr *JSONResponder) ServerErrorResponse(w http.ResponseWriter, r *http.Request, err error)

ServerErrorResponse sends a 500 Internal Server Error response with the error message. It logs the error details and returns a JSON error response to the client.

func (*JSONResponder) WriteJSON

func (jr *JSONResponder) WriteJSON(w http.ResponseWriter, status int, data responder.Envelope, headers http.Header) error

WriteJSON encodes the provided data as JSON and writes it to the HTTP response. It sets the Content-Type header to application/json and applies any custom headers. The data parameter should be of type responder.Envelope for consistent response structure.

type Options

type Options func(*JSONResponder)

Options is a function type for configuring JSONResponder instances.

func WithMaxBytes

func WithMaxBytes(b int64) Options

WithMaxBytes returns an option that sets the maximum request body size in bytes. This limit is enforced when reading JSON request bodies to prevent excessive memory usage.

Jump to

Keyboard shortcuts

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