Involved Source Filesdecode.go
Package protojson marshals and unmarshals protocol buffer messages as JSON
format. It follows the guide 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 buffer messages.
encode.gowell_known_types.go
Package-Level Type Names (total 8, in which 2 are exported)
/* sort exporteds by: | */
MarshalOptions is a configurable JSON format marshaler.
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.
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 ║
╚═══════╧════════════════════════════╝
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.
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.
NoUnkeyedLiteralspragma.NoUnkeyedLiterals
Resolver is used for looking up types when expanding google.protobuf.Any
messages. If nil, this defaults to using protoregistry.GlobalTypes.
UseEnumNumbers emits enum values as numbers.
UseProtoNames uses proto field name instead of lowerCamelCase name in JSON
field names.
Format formats the message as a string.
This method is only intended for human consumption and ignores errors.
Do not depend on the output being stable. It may change over time across
different versions of the program.
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.
marshal is a centralized function that all marshal operations go through.
For profiling purposes, avoid changing the name of this function or
introducing other code paths for marshal that do not go through this.
UnmarshalOptions is a configurable JSON format parser.
AllowPartial accepts input for messages that will result in missing
required fields. If AllowPartial is false (the default), Unmarshal will
return error if there are any missing required fields.
DiscardUnknown specifies whether to ignore unknown fields when parsing.
An unknown field is any field whose field name or field number does not
resolve to any known or extension field in the message.
By default, unmarshal rejects unknown fields as an error.
NoUnkeyedLiteralspragma.NoUnkeyedLiterals
Resolver is used for looking up types when unmarshaling
google.protobuf.Any messages or extension fields.
If nil, this defaults to using protoregistry.GlobalTypes.
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).
unmarshal is a centralized function that all unmarshal operations go through.
For profiling purposes, avoid changing the name of this function or
introducing other code paths for unmarshal that do not go through this.
Decoder*json.Decoder
in contains the unconsumed input.
lastCall is last method called, either readCall or peekCall.
Initial value is readCall.
lastErr contains the last read error.
lastToken contains the last read token.
openStack is a stack containing ObjectOpen and ArrayOpen values. The
top of stack represents the object or the array the current value is
directly located in.
orig is used in reporting line and column.
optsUnmarshalOptions
Clone returns a copy of the Decoder for use in reading ahead the next JSON
object, array or other values without affecting current Decoder.
Peek looks ahead and returns the next token kind without advancing a read.
Position returns line and column number of given index of the original input.
It will panic if index is out of range.
Read returns the next JSON token.
It will return an error if there is no valid token.
consume consumes n bytes of input and any subsequent whitespace.
consumeBoolToken constructs a Token for a Bool kind with raw value derived from
current d.in and given size.
consumeStringToken constructs a Token for a String kind with raw value derived
from current d.in and given size.
consumeToken constructs a Token for given Kind with raw value derived from
current d.in and given size, and consumes the given size-lenght of it.
currPos returns the current index position of d.in from d.orig.
isValueNext returns true if next type should be a JSON value: Null,
Number, String or Bool.
newError returns an error object with position info.
newSyntaxError returns an error with line and column information useful for
syntax errors.
parseNext parses for the next JSON token. It returns a Token object for
different types, except for Name. It does not handle whether the next token
is in a valid sequence or not.
( decoder) parseString(in []byte) (string, int, error)
skipJSONValue parses a JSON value (null, boolean, string, number, object and
array) in order to advance the read to the next JSON value. It relies on
the decoder returning an error if the types are not in valid sequence.
syntaxError returns a syntax error for given position.
unexpectedTokenError returns a syntax error for the given unexpected token.
( decoder) unmarshalAny(m protoreflect.Message) error
unmarshalAnyValue unmarshals the given custom-type message from the JSON
object's "value" field.
( decoder) unmarshalDuration(m protoreflect.Message) error( decoder) unmarshalEmpty(protoreflect.Message) error( decoder) unmarshalFieldMask(m protoreflect.Message) error( decoder) unmarshalKnownValue(m protoreflect.Message) error( decoder) unmarshalList(list protoreflect.List, fd protoreflect.FieldDescriptor) error( decoder) unmarshalListValue(m protoreflect.Message) error( decoder) unmarshalMap(mmap protoreflect.Map, fd protoreflect.FieldDescriptor) error
unmarshalMapKey converts given token of Name kind into a protoreflect.MapKey.
A map key type is any integral or string type.
unmarshalMessage unmarshals a message into the given protoreflect.Message.
unmarshalScalar unmarshals to a scalar/enum protoreflect.Value specified by
the given FieldDescriptor.
unmarshalSingular unmarshals to the non-repeated field specified
by the given FieldDescriptor.
( decoder) unmarshalStruct(m protoreflect.Message) error( decoder) unmarshalTimestamp(m protoreflect.Message) error( decoder) unmarshalWrapperType(m protoreflect.Message) error
func findTypeURL(d decoder) (json.Token, error)
Encoder*json.EncoderEncoder.indentstringEncoder.indents[]byteEncoder.lastKindjson.kindEncoder.out[]byteoptsMarshalOptions
Bytes returns the content of the written bytes.
EndArray writes out the ']' symbol.
EndObject writes out the '}' symbol.
StartArray writes out the '[' symbol.
StartObject writes out the '{' symbol.
WriteBool writes out the given boolean value.
WriteFloat writes out the given float and bitSize in JSON number value.
WriteInt writes out the given signed integer in JSON number value.
WriteName writes out the given string in JSON string value and the name
separator ':'. Returns error if input string contains invalid UTF-8, which
should not be likely as protobuf field names should be valid.
WriteNull writes out the null value.
WriteString writes out the given string in JSON string value. Returns error
if input string contains invalid UTF-8.
WriteUint writes out the given unsigned integer in JSON number value.
( encoder) marshalAny(m protoreflect.Message) error( encoder) marshalDuration(m protoreflect.Message) error( encoder) marshalEmpty(protoreflect.Message) error( encoder) marshalFieldMask(m protoreflect.Message) error( encoder) marshalKnownValue(m protoreflect.Message) error
marshalList marshals the given protoreflect.List.
( encoder) marshalListValue(m protoreflect.Message) error
marshalMap marshals given protoreflect.Map.
marshalMessage marshals the fields in the given protoreflect.Message.
If the typeURL is non-empty, then a synthetic "@type" field is injected
containing the URL as the value.
marshalSingular marshals the given non-repeated field value. This includes
all scalar types, enums, messages, and groups.
( encoder) marshalStruct(m protoreflect.Message) error( encoder) marshalTimestamp(m protoreflect.Message) error
marshalValue marshals the given protoreflect.Value.
( encoder) marshalWrapperType(m protoreflect.Message) error
prepareNext adds possible comma and indentation for the next value based
on last type and indent option. It also updates lastKind to next.
unpopulatedFieldRanger wraps a protoreflect.Message and modifies its Range
method to additionally iterate over unpopulated fields.
Messageprotoreflect.Message
Clear clears the field such that a subsequent Has call reports false.
Clearing an extension field clears both the extension type and value
associated with the given field number.
Clear is a mutating operation and unsafe for concurrent use.
Descriptor returns message descriptor, which contains only the protobuf
type information for the message.
Get retrieves the value for a field.
For unpopulated scalars, it returns the default value, where
the default value of a bytes scalar is guaranteed to be a copy.
For unpopulated composite types, it returns an empty, read-only view
of the value; to obtain a mutable reference, use Mutable.
GetUnknown retrieves the entire list of unknown fields.
The caller may only mutate the contents of the RawFields
if the mutated bytes are stored back into the message with SetUnknown.
Has reports whether a field is populated.
Some fields have the property of nullability where it is possible to
distinguish between the default value of a field and whether the field
was explicitly populated with the default value. Singular message fields,
member fields of a oneof, and proto2 scalar fields are nullable. Such
fields are populated only if explicitly set.
In other cases (aside from the nullable cases above),
a proto3 scalar field is populated if it contains a non-zero value, and
a repeated field is populated if it is non-empty.
Interface unwraps the message reflection interface and
returns the underlying ProtoMessage interface.
IsValid reports whether the message is valid.
An invalid message is an empty, read-only value.
An invalid message often corresponds to a nil pointer of the concrete
message type, but the details are implementation dependent.
Validity is not part of the protobuf data model, and may not
be preserved in marshaling or other operations.
Mutable returns a mutable reference to a composite type.
If the field is unpopulated, it may allocate a composite value.
For a field belonging to a oneof, it implicitly clears any other field
that may be currently set within the same oneof.
For extension fields, it implicitly stores the provided ExtensionType
if not already stored.
It panics if the field does not contain a composite type.
Mutable is a mutating operation and unsafe for concurrent use.
New returns a newly allocated and mutable empty message.
NewField returns a new value that is assignable to the field
for the given descriptor. For scalars, this returns the default value.
For lists, maps, and messages, this returns a new, empty, mutable value.
ProtoMethods returns optional fast-path implementions of various operations.
This method may return nil.
The returned methods type is identical to
"google.golang.org/protobuf/runtime/protoiface".Methods.
Consult the protoiface package documentation for details.
( unpopulatedFieldRanger) Range(f func(protoreflect.FieldDescriptor, protoreflect.Value) bool)
Set stores the value for a field.
For a field belonging to a oneof, it implicitly clears any other field
that may be currently set within the same oneof.
For extension fields, it implicitly stores the provided ExtensionType.
When setting a composite type, it is unspecified whether the stored value
aliases the source's memory in any way. If the composite value is an
empty, read-only value, then it panics.
Set is a mutating operation and unsafe for concurrent use.
SetUnknown stores an entire list of unknown fields.
The raw fields must be syntactically valid according to the wire format.
An implementation may panic if this is not the case.
Once stored, the caller must not mutate the content of the RawFields.
An empty RawFields may be passed to clear the fields.
SetUnknown is a mutating operation and unsafe for concurrent use.
Type returns the message type, which encapsulates both Go and protobuf
type information. If the Go type information is not needed,
it is recommended that the message descriptor be used instead.
WhichOneof reports which field within the oneof is populated,
returning nil if none are populated.
It panics if the oneof descriptor does not belong to this message.
unpopulatedFieldRanger : google.golang.org/protobuf/internal/order.FieldRanger
unpopulatedFieldRanger : google.golang.org/protobuf/reflect/protoreflect.Message
unpopulatedFieldRanger : database/sql/driver.Validator
Package-Level Functions (total 17, in which 3 are exported)
Format formats the message as a multiline string.
This function is only intended for human consumption and ignores errors.
Do not depend on the output being stable. It may change over time across
different versions of the program.
Marshal writes the given proto.Message in JSON format using default options.
Do not depend on the output being stable. It may change over time across
different versions of the program.
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).
findTypeURL returns the token for the "@type" field value from the given
JSON bytes. It is expected that the given bytes start with json.ObjectOpen.
It returns errEmptyObject if the JSON object is empty or errMissingType if
@type field does not exist. It returns other error if the @type field is not
valid or other decoding issues.
parseDuration parses the given input string for seconds and nanoseconds value
for the Duration JSON format. The format is a decimal number with a suffix
's'. It can have optional plus/minus sign. There needs to be at least an
integer or fractional part. Fractional part is limited to 9 digits only for
nanoseconds precision, regardless of whether there are trailing zero digits.
Example values are 1s, 0.1s, 1.s, .1s, +1s, -1s, -.1s.