jsonpb

package
v0.11.0 Latest Latest
Warning

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

Go to latest
Published: Feb 16, 2023 License: MPL-2.0 Imports: 18 Imported by: 0

Documentation

Overview

Package jsonpb provides functionality to marshal and unmarshal between a protocol buffer message and JSON. It follows the specification at https://developers.google.com/protocol-buffers/docs/proto3#json.

Do not rely on the default behavior of the standard encoding/json package when called on generated message types as it does not operate correctly.

Deprecated: Use the "google.golang.org/protobuf/encoding/protojson" package instead.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Marshal

func Marshal(m proto.Message) ([]byte, error)

func MarshalInto

func MarshalInto(w io.Writer, m proto.Message) error

Marshals a protobuf to json using lossless any encoding

func MarshalIntoV2

func MarshalIntoV2(w io.Writer, m protoV2.Message) error

func MarshalString

func MarshalString(m proto.Message) (string, error)

func MarshalV2

func MarshalV2(m protoV2.Message) ([]byte, error)

func Unmarshal

func Unmarshal(data []byte, m proto.Message) error

Unmarshal unmarshals a JSON object from r into m.

func UnmarshalInto

func UnmarshalInto(r io.Reader, m proto.Message) error

Unmarshal unmarshals a JSON object from r into m.

func UnmarshalNext

func UnmarshalNext(d *json.Decoder, m proto.Message) error

UnmarshalNext unmarshals the next JSON object from d into m.

func UnmarshalString

func UnmarshalString(s string, m proto.Message) error

UnmarshalString unmarshals a JSON object from s into m.

func UnmarshalV2

func UnmarshalV2(data []byte, m protoV2.Message) error

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 deprecated

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

Deprecated: Custom types should implement protobuf reflection instead.

type JSONPBUnmarshaler deprecated

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

Deprecated: Custom types should implement protobuf reflection instead.

type Marshaler

type Marshaler struct {
	// OrigName specifies whether to use the original protobuf name for fields.
	OrigName bool

	// EnumsAsInts specifies whether to render enum values as integers,
	// as opposed to string values.
	EnumsAsInts bool

	// EmitDefaults specifies whether to render fields with zero values.
	EmitDefaults bool

	// Indent controls whether the output is compact or not.
	// If empty, the output is compact JSON. Otherwise, every JSON object
	// entry and JSON array value will be on its own line.
	// Each line will be preceded by repeated copies of Indent, where the
	// number of copies is the current indentation depth.
	Indent string

	// AnyResolver is used to resolve the google.protobuf.Any well-known type.
	// If unset, the global registry is used by default.
	AnyResolver AnyResolver
}

Marshaler is a configurable object for marshaling protocol buffer messages to the specified JSON representation.

func (*Marshaler) Marshal

func (jm *Marshaler) Marshal(w io.Writer, m proto.Message) error

Marshal serializes a protobuf message as JSON into w.

func (*Marshaler) MarshalToString

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

MarshalToString serializes a protobuf message as JSON in string form.

func (*Marshaler) MarshalV2

func (jm *Marshaler) MarshalV2(w io.Writer, m protoV2.Message) error

type Unmarshaler

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

	// AnyResolver is used to resolve the google.protobuf.Any well-known type.
	// If unset, the global registry is used by default.
	AnyResolver AnyResolver
}

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, m proto.Message) error

Unmarshal unmarshals a JSON object from r into m.

func (*Unmarshaler) UnmarshalNext

func (u *Unmarshaler) UnmarshalNext(d *json.Decoder, m proto.Message) error

UnmarshalNext unmarshals the next JSON object from d into m.

func (*Unmarshaler) UnmarshalNextV2

func (u *Unmarshaler) UnmarshalNextV2(d *json.Decoder, m protoV2.Message) error

UnmarshalNext unmarshals the next JSON object from d into m.

func (*Unmarshaler) UnmarshalV2

func (u *Unmarshaler) UnmarshalV2(r io.Reader, m protoV2.Message) error

Jump to

Keyboard shortcuts

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