jsonresp

package module
v0.9.2 Latest Latest
Warning

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

Go to latest
Published: Feb 7, 2024 License: BSD-3-Clause Imports: 4 Imported by: 6

README

JSON Response

PkgGoDev Build Status Code Coverage Go Report Card

The json-resp package contains a small set of functions that are used to marshall and unmarshall response data and errors in JSON format.

Go Version Compatibility

This module aims to maintain support for the two most recent stable versions of Go. This corresponds to the Go Release Maintenance Policy and Security Policy, ensuring critical bug fixes and security patches are available for all supported language versions.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ReadError added in v0.5.0

func ReadError(r io.Reader) error

ReadError attempts to unmarshal JSON-encoded error details from the supplied reader. It returns nil if an error could not be parsed from the response, or if the parsed error was nil.

func ReadResponse

func ReadResponse(r io.Reader, v interface{}) error

ReadResponse reads a JSON response, and unmarshals the supplied data.

func WriteError

func WriteError(w http.ResponseWriter, message string, code int) error

WriteError writes a status code and JSON response containing the supplied error message and status code to w.

func WriteResponse

func WriteResponse(w http.ResponseWriter, data interface{}, code int) error

WriteResponse writes a status code and JSON response containing data to w.

func WriteResponsePage added in v0.2.0

func WriteResponsePage(w http.ResponseWriter, data interface{}, pd *PageDetails, code int) error

WriteResponsePage writes a status code and JSON response containing data and pd to w.

Types

type Error

type Error struct {
	Code    int    `json:"code,omitempty"`
	Message string `json:"message,omitempty"`
}

Error describes an error condition.

func (*Error) Error

func (e *Error) Error() string

func (*Error) Is added in v0.7.0

func (e *Error) Is(target error) bool

Is compares e against target. If target is an Error and matches the non-zero fields of e, true is returned.

type PageDetails added in v0.2.0

type PageDetails struct {
	Prev      string `json:"prev,omitempty"`
	Next      string `json:"next,omitempty"`
	TotalSize int    `json:"totalSize,omitempty"`
}

PageDetails specifies paging information.

func ReadResponsePage added in v0.2.0

func ReadResponsePage(r io.Reader, v interface{}) (pd *PageDetails, err error)

ReadResponsePage reads a paged JSON response, and unmarshals the supplied data.

type Response

type Response struct {
	Data  interface{}  `json:"data,omitempty"`
	Page  *PageDetails `json:"page,omitempty"`
	Error *Error       `json:"error,omitempty"`
}

Response is the top level container of all of our REST API responses.

Jump to

Keyboard shortcuts

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