Help us shape the future of Go! Take the 2026 Go Developer Survey

jsonv2

package
v1.21.2 Latest Latest
Warning

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

Go to latest
Published: Oct 12, 2026 License: Apache-2.0 Imports: 4 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func MarshalMarshalerTo

func MarshalMarshalerTo[T json.MarshalerTo](v T) ([]byte, error)

MarshalMarshalerTo provides a MarshalJSON implementation for any type that implements json.MarshalerTo. json.Marshal dispatches to MarshalJSONTo, so this doesn't recurse; the constraint is what guarantees that at compile time.

This is the entry point into v2 that establishes v1 options, per the package-level comment above.

func WriteField

func WriteField[T json.MarshalerTo](e *jsontext.Encoder, name string, v T) error

WriteField writes the object member name and then v's JSON encoding, so that the member is written and checked in one statement. A nil v is written as JSON null rather than dispatched to MarshalJSONTo: v1's reflection-based encoder already does this for a nil pointer, so writing null here keeps output identical to v1, rather than panicking on the types whose MarshalJSONTo assumes a non-nil receiver.

func WriteFieldArray

func WriteFieldArray[T json.MarshalerTo](e *jsontext.Encoder, name string, items []T) error

WriteFieldArray writes the object member name and then the JSON array of items.

func WriteFieldValue

func WriteFieldValue(e *jsontext.Encoder, name string, v any) error

WriteFieldValue is WriteField for values that don't implement json.MarshalerTo.

func WriteMarshalerToArray

func WriteMarshalerToArray[T json.MarshalerTo](e *jsontext.Encoder, items []T) error

WriteMarshalerToArray writes the JSON array of items to the encoder.

func WriteMarshalerToArrayOrNull

func WriteMarshalerToArrayOrNull[T json.MarshalerTo](e *jsontext.Encoder, items []T) error

WriteMarshalerToArrayOrNull is WriteMarshalerToArray but writes null for a nil slice, as encoding/json v1 does. Types whose pre-1.27 MarshalJSON returns "[]" for an empty value must keep using WriteMarshalerToArray.

Types

This section is empty.

Jump to

Keyboard shortcuts

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