jsonpb

package
v1.24.0 Latest Latest
Warning

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

Go to latest
Published: Aug 18, 2023 License: MIT Imports: 13 Imported by: 0

README

temporaljsonpb

This package contains essentially a copy of https://github.com/gogo/protobuf/blob/v1.3.2/jsonpb/jsonpb.go but altered to support custom, optional JSON formatting/parsing for protobuf types.

Documentation

Overview

Package jsonpb provides marshaling and unmarshaling between protocol buffers and JSON. It follows the specification at https://developers.google.com/protocol-buffers/docs/proto3#json.

This package produces a different output than the standard "encoding/json" package, which does not operate correctly on protocol buffers.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Unmarshal

func Unmarshal(r io.Reader, pb proto.Message) error

Unmarshal unmarshals a JSON object stream into a protocol buffer. This function is lenient and will decode any options permutations of the related Marshaler.

func UnmarshalNext

func UnmarshalNext(dec *json.Decoder, pb proto.Message) error

UnmarshalNext unmarshals the next protocol buffer from a JSON object stream. This function is lenient and will decode any options permutations of the related Marshaler.

func UnmarshalString

func UnmarshalString(str string, pb proto.Message) error

UnmarshalString will populate the fields of a protocol buffer based on a JSON string. This function is lenient and will decode any options permutations of the related Marshaler.

Types

type AnyResolver

type AnyResolver interface {
	Resolve(typeUrl string) (proto.Message, error)
}

AnyResolver takes a type URL, present in an Any message, and resolves it into an instance of the associated message.

type JSONPBMarshaler

type JSONPBMarshaler interface {
	MarshalJSONPB(*Marshaler) ([]byte, error)
}

JSONPBMarshaler is implemented by protobuf messages that customize the way they are marshaled to JSON. Messages that implement this should also implement JSONPBUnmarshaler so that the custom format can be parsed.

The JSON marshaling must follow the proto to JSON specification:

https://developers.google.com/protocol-buffers/docs/proto3#json

type JSONPBMaybeMarshaler

type JSONPBMaybeMarshaler interface {
	// MaybeMarshalJSONPB is for formatting the proto message as JSON. If the
	// "handled" result value is false, "b" and "err" are ignored and the default
	// proto JSON behavior occurs. currIndent is the current prefix depth but
	// should not be applied if m.Indent is empty.
	MaybeMarshalJSONPB(m *Marshaler, currIndent string) (handled bool, b []byte, err error)
}

JSONPBMaybeMarshaler is implemented by any proto struct that wants to customize optional Temporal-specific JSON conversion.

type JSONPBMaybeUnmarshaler

type JSONPBMaybeUnmarshaler interface {
	// MaybeUnmarshalJSONPB is for parsing the given JSON into the proto message.
	// If the "handled" result value is false, "err" is ignored and the default
	// behavior proto JSON occurs.
	MaybeUnmarshalJSONPB(u *Unmarshaler, b []byte) (handled bool, err error)
}

JSONPBMaybeUnmarshaler is implemented by any proto struct that wants to customize optional Temporal-specific JSON conversion.

type JSONPBUnmarshaler

type JSONPBUnmarshaler interface {
	UnmarshalJSONPB(*Unmarshaler, []byte) error
}

JSONPBUnmarshaler is implemented by protobuf messages that customize the way they are unmarshaled from JSON. Messages that implement this should also implement JSONPBMarshaler so that the custom format can be produced.

The JSON unmarshaling must follow the JSON to proto specification:

https://developers.google.com/protocol-buffers/docs/proto3#json

type Marshaler

type Marshaler struct {
	// Whether to render enum values as integers, as opposed to string values.
	EnumsAsInts bool

	// Whether to render fields with zero values.
	EmitDefaults bool

	// A string to indent each level by. The presence of this field will
	// also cause a space to appear between the field separator and
	// value, and for newlines to be appear between fields and array
	// elements.
	Indent string

	// Whether to use the original (.proto) name for fields.
	OrigName bool

	// A custom URL resolver to use when marshaling Any messages to JSON.
	// If unset, the default resolution strategy is to extract the
	// fully-qualified type name from the type URL and pass that to
	// proto.MessageType(string).
	AnyResolver AnyResolver

	Metadata map[string]interface{}
}

Marshaler is a configurable object for converting between protocol buffer objects and a JSON representation for them.

func (*Marshaler) Marshal

func (m *Marshaler) Marshal(out io.Writer, pb proto.Message) error

Marshal marshals a protocol buffer into JSON.

func (*Marshaler) MarshalToString

func (m *Marshaler) MarshalToString(pb proto.Message) (string, error)

MarshalToString converts a protocol buffer object to JSON string.

type Unmarshaler

type Unmarshaler struct {
	// Whether to allow messages to contain unknown fields, as opposed to
	// failing to unmarshal.
	AllowUnknownFields bool

	// A custom URL resolver to use when unmarshaling Any messages from JSON.
	// If unset, the default resolution strategy is to extract the
	// fully-qualified type name from the type URL and pass that to
	// proto.MessageType(string).
	AnyResolver AnyResolver

	Metadata map[string]interface{}
}

Unmarshaler is a configurable object for converting from a JSON representation to a protocol buffer object.

func (*Unmarshaler) Unmarshal

func (u *Unmarshaler) Unmarshal(r io.Reader, pb proto.Message) error

Unmarshal unmarshals a JSON object stream into a protocol buffer. This function is lenient and will decode any options permutations of the related Marshaler.

func (*Unmarshaler) UnmarshalNext

func (u *Unmarshaler) UnmarshalNext(dec *json.Decoder, pb proto.Message) error

UnmarshalNext unmarshals the next protocol buffer from a JSON object stream. This function is lenient and will decode any options permutations of the related Marshaler.

Jump to

Keyboard shortcuts

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