protojson

package
v1.32.1-cherry-pick-fo... Latest Latest
Warning

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

Go to latest
Published: Jul 5, 2024 License: MIT, BSD-3-Clause Imports: 19 Imported by: 0

README

Here lies a vendored copy of protojson with some minor alterations.

Unfortunately we need a few things that the official stack won't let us do:

  1. We need to support our old JSON with camelCase enums while we migrate to the canonical SCREAMING_SNAKE_CASE enums
  2. We've decided to support a shorthand JSON serialization for our Payload type so that our users have a better experience

All code herein is governed by the LICENSE file in this directory (barring the maybe_marshal code we added).

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Unmarshal

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

Unmarshal reads the given []byte into the given proto.Message. The provided message must be mutable (e.g., a non-nil pointer to a message).

Types

type MarshalOptions

type MarshalOptions struct {
	// Metadata is used for storing request metadata, such as whether shorthand
	// payloads are disabled
	Metadata map[string]interface{}

	// Multiline 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.
	Multiline bool

	// Indent 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.
	Indent string

	// AllowPartial 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 missing required fields.
	AllowPartial bool

	// UseProtoNames uses proto field name instead of lowerCamelCase name in JSON
	// field names.
	UseProtoNames bool

	// UseEnumNumbers emits enum values as numbers.
	UseEnumNumbers bool

	// EmitUnpopulated 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                 ║
	//  ╚═══════╧════════════════════════════╝
	EmitUnpopulated bool

	// EmitDefaultValues specifies whether to emit default-valued primitive fields,
	// empty lists, and empty maps. The fields affected are as follows:
	//  ╔═══════╤════════════════════════════════════════╗
	//  ║ JSON  │ Protobuf field                         ║
	//  ╠═══════╪════════════════════════════════════════╣
	//  ║ false │ non-optional scalar boolean fields     ║
	//  ║ 0     │ non-optional scalar numeric fields     ║
	//  ║ ""    │ non-optional scalar string/byte fields ║
	//  ║ []    │ empty repeated fields                  ║
	//  ║ {}    │ empty map fields                       ║
	//  ╚═══════╧════════════════════════════════════════╝
	//
	// Behaves similarly to EmitUnpopulated, but does not emit "null"-value fields,
	// i.e. presence-sensing fields that are omitted will remain omitted to preserve
	// presence-sensing.
	// EmitUnpopulated takes precedence over EmitDefaultValues since the former generates
	// a strict superset of the latter.
	EmitDefaultValues bool

	// Resolver is used for looking up types when expanding google.protobuf.Any
	// messages. If nil, this defaults to using protoregistry.GlobalTypes.
	Resolver interface {
		protoregistry.ExtensionTypeResolver
		protoregistry.MessageTypeResolver
	}
}

MarshalOptions is a configurable JSON format marshaler.

func (MarshalOptions) Marshal

func (o MarshalOptions) Marshal(m proto.Message) ([]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 (MarshalOptions) MarshalAppend

func (o MarshalOptions) MarshalAppend(b []byte, m proto.Message) ([]byte, error)

MarshalAppend appends the JSON format encoding of m to b, returning the result.

type ProtoJSONMaybeMarshaler

type ProtoJSONMaybeMarshaler interface {
	// MaybeMarshalProtoJSON is for formatting the proto message as JSON. If the
	// "handled" result value is false, "err" are ignored and the default
	// protojson behavior occurs.
	MaybeMarshalProtoJSON(meta map[string]interface{}, enc *json.Encoder) (handled bool, err error)
}

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

type ProtoJSONMaybeUnmarshaler

type ProtoJSONMaybeUnmarshaler interface {
	// MaybeUnmarshalProtoJSON is for parsing the given JSON into the proto message.
	// If the "handled" result value is false, "err" is ignored and the default
	// protojson unmarshaling proceeds
	MaybeUnmarshalProtoJSON(meta map[string]interface{}, dec *json.Decoder) (handled bool, err error)
}

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

type UnmarshalOptions

type UnmarshalOptions struct {
	// Metadata is used for storing request metadata, such as whether shorthand
	// payloads are disabled
	Metadata map[string]interface{}

	// If AllowPartial is set, input for messages that will result in missing
	// required fields will not return an error.
	AllowPartial bool

	// If DiscardUnknown is set, unknown fields and enum name values are ignored.
	DiscardUnknown bool

	// Resolver is used for looking up types when unmarshaling
	// google.protobuf.Any messages or extension fields.
	// If nil, this defaults to using protoregistry.GlobalTypes.
	Resolver interface {
		protoregistry.MessageTypeResolver
		protoregistry.ExtensionTypeResolver
	}

	// RecursionLimit limits how deeply messages may be nested.
	// If zero, a default limit is applied.
	RecursionLimit int
}

UnmarshalOptions is a configurable JSON format parser.

func (UnmarshalOptions) Unmarshal

func (o UnmarshalOptions) Unmarshal(b []byte, m proto.Message) 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).

Directories

Path Synopsis
Package errors implements functions to manipulate errors.
Package errors implements functions to manipulate errors.
Package genid contains constants for declarations in descriptor.proto and the well-known types.
Package genid contains constants for declarations in descriptor.proto and the well-known types.
Package order provides ordered access to messages and maps.
Package order provides ordered access to messages and maps.
Package set provides simple set data structures for uint64s.
Package set provides simple set data structures for uint64s.
Package strs provides string manipulation functionality specific to protobuf.
Package strs provides string manipulation functionality specific to protobuf.
testprotos

Jump to

Keyboard shortcuts

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