marshal

package
v0.13.0 Latest Latest
Warning

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

Go to latest
Published: Nov 1, 2023 License: MIT Imports: 17 Imported by: 3

Documentation

Overview

Package marshal provides some common handlers for encoding or decoding json

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Decode

func Decode(r io.Reader, result interface{}) error

Decode will read the json from the supplied reader, and decode it into the supplied result instance.

func DecodeBody

func DecodeBody(w http.ResponseWriter, r *http.Request, result interface{}) error

DecodeBody will read the json from the HTTP request body, and decode it into the supplied result instance. If error occured, then it will write to the response

func DecodeBytes

func DecodeBytes(data []byte, result interface{}) error

DecodeBytes is a helper that takes the supplied json and decodes it into the supplied result instance.

func DecoderHandle

func DecoderHandle() *codec.JsonHandle

DecoderHandle returns a code handle pre-configured for decoding json back into go types. It is setup to error when a field in the json has no matching field in the go type, and that all maps are deserialized into a map[stirng]interface{} the returned handle is shared, it should not be mutated by callers.

func EncodeBytes

func EncodeBytes(printSetting PrettyPrintSetting, value interface{}) ([]byte, error)

EncodeBytes is a helper that takes the supplied go value, and encoded it to json and returned the byte slice containing the encoded value.

func NewEncoder

func NewEncoder(w io.Writer, r *http.Request) *codec.Encoder

NewEncoder returns a new Encoder ready to write a value, its configured based on the request [currently just if it should pretty print or not]

func NewRequest

func NewRequest(method string, url string, req interface{}) (*http.Request, error)

NewRequest returns http.Request

func WriteJSON

func WriteJSON(w http.ResponseWriter, r *http.Request, bodies ...interface{})

WriteJSON will serialize the supplied body parameter as a http response. If the body value implements the WriteHTTPResponse interface, then that will be called to have it do the response generation if body implements error, then that's returned as a server error use the Error type to fully specify your error response otherwise body is assumed to be a succesful response, and its serialized and written as a json response with a 200 status code.

multiple body parameters can be supplied, in which case the first non-nil one will be used. This is useful as it allows you to do

	x, err := doSomething()
	WriteJSON(logger,w,r,err,x)
and if there was an error, that's what'll get returned

func WritePlainJSON

func WritePlainJSON(w http.ResponseWriter, statusCode int, body interface{}, printSetting PrettyPrintSetting)

WritePlainJSON will serialize the supplied body parameter as a http response.

Types

type PrettyPrintSetting

type PrettyPrintSetting int

PrettyPrintSetting controls how to format json when encoding a go type -> json

const (
	// DontPrettyPrint the output has no additional space/line breaks, and has random ordering of map keys
	DontPrettyPrint PrettyPrintSetting = 0

	// PrettyPrint provides an output more suitable for human consumption, it has line breaks/indenting
	// and map keys are generated in order.
	PrettyPrint PrettyPrintSetting = 1
)

type WriteHTTPResponse

type WriteHTTPResponse interface {
	WriteHTTPResponse(w http.ResponseWriter, r *http.Request)
}

WriteHTTPResponse is for types to implement this interface to get full control over how they are written out as a http response

Jump to

Keyboard shortcuts

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