protojson

package
v1.2.115 Latest Latest
Warning

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

Go to latest
Published: Feb 26, 2024 License: MIT Imports: 5 Imported by: 2

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Marshal

func Marshal(pb proto.Message, options ...MarshalerOption) ([]byte, error)

Marshal marshals the given proto.Message in the JSON format using options in MarshalOptions. Do not depend on the output being stable. It may change over time across different versions of the program.

func MarshalIndent

func MarshalIndent(pb proto.Message, prefix, indent string, options ...MarshalerOption) ([]byte, error)

MarshalIndent is like Marshal but applies Indent to format the output. Each JSON element in the output will begin on a new line beginning with prefix followed by one or more copies of indent according to the indentation nesting.

func Unmarshal

func Unmarshal(data []byte, pb proto.Message, options ...UnmarshalerOption) error

Unmarshal reads the given []byte and populates the given proto.Message using options in the UnmarshalOptions object. It will clear the message first before setting the fields. If it returns an error, the given message may be partially set. The provided message must be mutable (e.g., a non-nil pointer to a message).

Types

type EmptyMarshalerOption

type EmptyMarshalerOption struct{}

EmptyMarshalerOption does not alter the configuration. It can be embedded in another structure to build custom options.

This API is EXPERIMENTAL.

type EmptyUnmarshalerOption

type EmptyUnmarshalerOption struct{}

EmptyUnmarshalerOption does not alter the configuration. It can be embedded in another structure to build custom options.

This API is EXPERIMENTAL.

type Marshaler

type Marshaler struct {
	protojson.MarshalOptions
}

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

func (*Marshaler) ApplyOptions

func (o *Marshaler) ApplyOptions(options ...MarshalerOption) *Marshaler

ApplyOptions call apply() for all options one by one

type MarshalerOption

type MarshalerOption interface {
	// contains filtered or unexported methods
}

A MarshalerOption sets options.

func WithMarshalAllowPartial

func WithMarshalAllowPartial(allowPartial bool) MarshalerOption

WithMarshalAllowPartial allows messages that have missing required fields to marshal without returning an error. If AllowPartial is false (the default), Marshal will return error if there are any missed required fields.

func WithMarshalEmitUnpopulated

func WithMarshalEmitUnpopulated(emitUnpopulated bool) MarshalerOption

WithMarshalEmitUnpopulated specifies whether to emit unpopulated fields. It does not emit unpopulated oneof fields or unpopulated extension fields. The JSON value emitted for unpopulated fields are as follows:

╔═══════╤════════════════════════════╗
║ JSON  │ Protobuf field             ║
╠═══════╪════════════════════════════╣
║ false │ proto3 boolean fields      ║
║ 0     │ proto3 numeric fields      ║
║ ""    │ proto3 string/bytes fields ║
║ null  │ proto2 scalar fields       ║
║ null  │ message fields             ║
║ []    │ list fields                ║
║ {}    │ map fields                 ║
╚═══════╧════════════════════════════╝

func WithMarshalIndent

func WithMarshalIndent(indent string) MarshalerOption

WithMarshalIndent specifies the set of indentation characters to use in a multiline formatted output such that every entry is preceded by Indent and terminated by a newline. If non-empty, then Multiline is treated as true. Indent can only be composed of space or tab characters.

func WithMarshalMultiline

func WithMarshalMultiline(multiline bool) MarshalerOption

WithMarshalMultiline specifies whether the marshaler should format the output in indented-form with every textual element on a new line. If Indent is an empty string, then an arbitrary indent is chosen.

func WithMarshalResolver

func WithMarshalResolver(resolver interface {
	protoregistry.ExtensionTypeResolver
	protoregistry.MessageTypeResolver
}) MarshalerOption

WithMarshalResolver is used for looking up types when expanding google.protobuf.Any messages. If nil, this defaults to using protoregistry.GlobalTypes.

func WithMarshalUseEnumNumbers

func WithMarshalUseEnumNumbers(useEnumNumbers bool) MarshalerOption

WithMarshalUseEnumNumbers emits enum values as numbers.

func WithMarshalUseProtoNames

func WithMarshalUseProtoNames(useProtoNames bool) MarshalerOption

WithMarshalUseProtoNames uses proto field name instead of lowerCamelCase name in JSON field names.

func WithMarshaler added in v1.2.86

func WithMarshaler(v Marshaler) MarshalerOption

WithMarshaler sets Marshaler.

type MarshalerOptionFunc

type MarshalerOptionFunc func(*Marshaler)

MarshalerOptionFunc wraps a function that modifies Marshaler into an implementation of the MarshalerOption interface.

type Unmarshaler

type Unmarshaler struct {
	protojson.UnmarshalOptions
}

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

func (*Unmarshaler) ApplyOptions

func (o *Unmarshaler) ApplyOptions(options ...UnmarshalerOption) *Unmarshaler

ApplyOptions call apply() for all options one by one

type UnmarshalerOption

type UnmarshalerOption interface {
	// contains filtered or unexported methods
}

A UnmarshalerOption sets options.

func WithUnmarshalAllowPartial

func WithUnmarshalAllowPartial(allowPartial bool) UnmarshalerOption

WithUnmarshalAllowPartial will not return an error if input for messages that will result in missing required fields.

func WithUnmarshalDiscardUnknown

func WithUnmarshalDiscardUnknown(discardUnknown bool) UnmarshalerOption

WithUnmarshalDiscardUnknown ignore unknown fields.

func WithUnmarshalResolver

func WithUnmarshalResolver(resolver interface {
	protoregistry.ExtensionTypeResolver
	protoregistry.MessageTypeResolver
}) UnmarshalerOption

WithUnmarshalResolver is used for looking up types when unmarshaling google.protobuf.Any messages or extension fields. If nil, this defaults to using protoregistry.GlobalTypes.

func WithUnmarshaler added in v1.2.86

func WithUnmarshaler(v Unmarshaler) UnmarshalerOption

WithUnmarshaler sets Unmarshaler.

type UnmarshalerOptionFunc

type UnmarshalerOptionFunc func(*Unmarshaler)

UnmarshalerOptionFunc wraps a function that modifies Unmarshaler into an implementation of the UnmarshalerOption interface.

Jump to

Keyboard shortcuts

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