json

package
v2.4.0 Latest Latest
Warning

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

Go to latest
Published: Nov 5, 2022 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package json provides helpers to work with JSON.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrDecodeJSON        = errors.New("json: error decoding json data")
	ErrMultipleJSONValue = errors.New("json: body must have only a single json value")
)

Functions

This section is empty.

Types

type JSON

type JSON struct {
	AllowUnknownFields bool
	MaxBytes           int
	Logger             logger.Logger
}

func (*JSON) ErrorJSON

func (j *JSON) ErrorJSON(w http.ResponseWriter, err error, status ...int) error

ErrorJSON takes an error and optionally a status code, generates and send a JSON error message.

func (*JSON) PushJSONToRemote

func (j *JSON) PushJSONToRemote(uri string, data any, client ...*http.Client) (*http.Response, int, error)

PushJSONToRemote posts arbitrary data to a remote URL as JSON, and returns the response, status code and error if any. The final parameter client is optional. If none is specified, we use the standard http.Client.

func (*JSON) ReadJSON

func (j *JSON) ReadJSON(w http.ResponseWriter, r *http.Request, data any) error

ReadJSON tries to read the body of a request and coverts from json into a go data variable.

func (*JSON) WriteJSON

func (j *JSON) WriteJSON(w http.ResponseWriter, status int, data any, headers ...http.Header) error

WriteJSON takes a response status code and arbitrary data and writes json to the client.

type JSONResponse

type JSONResponse struct {
	Error   bool   `json:"error"`
	Message string `json:"message"`
	Data    any    `json:"data,omitempty"`
}

JSONResponse is the type used for sending JSON.

Jump to

Keyboard shortcuts

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