protojson

package
v0.14.9 Latest Latest
Warning

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

Go to latest
Published: Nov 11, 2025 License: GPL-3.0 Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrCodeProtoJSONMarshalFailed string
)

Functions

This section is empty.

Types

type Encoder

type Encoder struct {
	// contains filtered or unexported fields
}

Encoder is the implementation of the Encoder interface

func NewEncoder

func NewEncoder(
	mode *goflagsmode.Flag,
) *Encoder

NewEncoder creates a new Encoder instance

Parameters:

  • mode: the flag mode
  • logger: the logger (optional, can be nil)

Returns:

- *Encoder: the new Encoder instance

func (Encoder) Encode

func (e Encoder) Encode(
	body any,
) ([]byte, error)

Encode encodes the given body to JSON

Parameters:

  • body: The body to encode

Returns:

  • ([]byte, error): The encoded body and the error if any

func (Encoder) EncodeAndWrite added in v0.12.3

func (e Encoder) EncodeAndWrite(
	writer io.Writer,
	beforeWriteFn func() error,
	body any,
) error

EncodeAndWrite encodes and writes the given body to the writer

Parameters:

  • writer: The writer to write the encoded body to
  • beforeWriteFn: The function to call before writing the body
  • body: The body to encode

Returns:

- error: The error if any

func (Encoder) EncodeAndWriteResponse added in v0.12.3

func (e Encoder) EncodeAndWriteResponse(
	writer http.ResponseWriter,
	response gonethttpresponse.Response,
) error

EncodeAndWriteResponse encodes and writes the response to the HTTP response writer

Parameters:

  • writer: The HTTP response writer
  • response: The response to encode, must have a data field that is a proto.Message

Returns:

  • error: The error if any

func (Encoder) EncodeResponse added in v0.12.3

func (e Encoder) EncodeResponse(
	response gonethttpresponse.Response,
) ([]byte, error)

EncodeResponse encodes the given response to JSON

Parameters:

  • response: The response to encode

Returns:

  • ([]byte, error): The encoded response and the error if any

func (Encoder) PrecomputeMarshal added in v0.12.3

func (e Encoder) PrecomputeMarshal(
	body any,
) (map[string]any, error)

PrecomputeMarshal precomputes the marshaled body by reflecting on the instance

Parameters:

- body: The body to precompute the marshaled body for

Returns:

- (map[string]any, error): The precomputed marshaled body and the error if any

Jump to

Keyboard shortcuts

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