jsonbind

package
v0.2.10 Latest Latest
Warning

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

Go to latest
Published: Jul 31, 2026 License: Apache-2.0 Imports: 7 Imported by: 0

Documentation

Overview

Package jsonbind provides generated, reflection-free JSON document codecs.

Index

Constants

View Source
const DefaultMaxJSONBodyBytes int64 = 1 << 20

DefaultMaxJSONBodyBytes is the default JSON document limit (1 MiB).

Variables

View Source
var ErrBodyTooLarge = errors.New("jsonbind: JSON body too large")

ErrBodyTooLarge reports that a JSON document exceeded its configured limit.

Functions

func BytesJSONMap

func BytesJSONMap(data []byte) (map[string]json.RawMessage, error)

BytesJSONMap decodes a complete JSON object document.

func DecodeJSON

func DecodeJSON[T any](r io.Reader) (T, error)

DecodeJSON decodes one JSON value from r into T using a generated codec. It does not inspect HTTP headers or use reflection on T's fields.

func DecodeJSONBool

func DecodeJSONBool(raw json.RawMessage) (bool, error)

func DecodeJSONBoolSlice

func DecodeJSONBoolSlice(raw json.RawMessage) ([]bool, error)

func DecodeJSONFloat64

func DecodeJSONFloat64(raw json.RawMessage) (float64, error)

func DecodeJSONFloat64Slice

func DecodeJSONFloat64Slice(raw json.RawMessage) ([]float64, error)

func DecodeJSONInt

func DecodeJSONInt(raw json.RawMessage) (int, error)

func DecodeJSONInt64

func DecodeJSONInt64(raw json.RawMessage) (int64, error)

func DecodeJSONInt64Slice

func DecodeJSONInt64Slice(raw json.RawMessage) ([]int64, error)

func DecodeJSONIntSlice

func DecodeJSONIntSlice(raw json.RawMessage) ([]int, error)

func DecodeJSONLimit

func DecodeJSONLimit[T any](r io.Reader, limit int64) (T, error)

DecodeJSONLimit is DecodeJSON with a per-call byte limit. A non-positive limit uses MaxJSONBodyBytes.

func DecodeJSONMapStringString

func DecodeJSONMapStringString(raw json.RawMessage) (map[string]string, error)

func DecodeJSONString

func DecodeJSONString(raw json.RawMessage) (string, error)

func DecodeJSONStringSlice

func DecodeJSONStringSlice(raw json.RawMessage) ([]string, error)

func EncodeJSON

func EncodeJSON[T any](w io.Writer, v T) error

EncodeJSON encodes v as compact JSON to w using a generated codec. It does not set HTTP headers or status.

func FieldError

func FieldError(field, message string, cause error) error

FieldError annotates a JSON decoding error with its document field.

func MaxJSONBodyBytes

func MaxJSONBodyBytes() int64

MaxJSONBodyBytes returns the effective JSON document limit.

func RawJSONArray

func RawJSONArray(raw json.RawMessage) ([]json.RawMessage, error)

RawJSONArray decodes a JSON array into raw elements.

func RawJSONMap

func RawJSONMap(raw json.RawMessage) (map[string]json.RawMessage, error)

RawJSONMap decodes a JSON object into raw fields.

func ReadLimit

func ReadLimit(r io.Reader, limit int64) ([]byte, error)

ReadLimit reads at most limit bytes from r.

func RegisterDecode

func RegisterDecode[T any](fn func([]byte) (T, error))

RegisterDecode registers a generated JSON document decoder for T.

func RegisterEncode

func RegisterEncode[T any](fn func(io.Writer, T) error)

RegisterEncode registers a generated compact JSON encoder for T.

func RestJSONAny

func RestJSONAny(body map[string]json.RawMessage, exclude []string) (map[string]any, error)

RestJSONAny returns JSON fields not named in exclude.

func RestJSONRaw

func RestJSONRaw(body map[string]json.RawMessage, exclude []string) map[string]json.RawMessage

RestJSONRaw copies JSON fields not named in exclude.

func SetMaxJSONBodyBytes

func SetMaxJSONBodyBytes(n int64)

SetMaxJSONBodyBytes changes the process-wide JSON document limit.

Types

type Error

type Error struct {
	Code    string
	Message string
	Field   string
	// contains filtered or unexported fields
}

Error describes a transport-neutral JSON mapping failure.

func AsError

func AsError(err error) (*Error, bool)

AsError finds a JSON Error without using reflection-dependent errors.As.

func (*Error) Error

func (e *Error) Error() string

func (*Error) Unwrap

func (e *Error) Unwrap() error

Jump to

Keyboard shortcuts

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