Source File
encode.go
Belonging Package
google.golang.org/protobuf/encoding/protojson
package protojson
import (
pref
)
const defaultIndent = " "
type MarshalOptions struct {
pragma.NoUnkeyedLiterals
Resolver interface {
protoregistry.ExtensionTypeResolver
protoregistry.MessageTypeResolver
}
}
func ( MarshalOptions) ( proto.Message) string {
if == nil || !.ProtoReflect().IsValid() {
return "<nil>" // invalid syntax, but okay since this is for debugging
}
.AllowPartial = true
, := .Marshal()
return string()
}
func ( MarshalOptions) ( proto.Message) ([]byte, error) {
if .Multiline && .Indent == "" {
.Indent = defaultIndent
}
if .Resolver == nil {
.Resolver = protoregistry.GlobalTypes
}
, := json.NewEncoder(.Indent)
if != nil {
return nil,
}
if == nil {
return []byte("{}"), nil
}
:= encoder{, }
if := .marshalMessage(.ProtoReflect()); != nil {
return nil,
}
if .AllowPartial {
return .Bytes(), nil
}
return .Bytes(), proto.CheckInitialized()
}
type encoder struct {
*json.Encoder
opts MarshalOptions
}
func ( encoder) ( pref.Message) error {
if := wellKnownTypeMarshaler(.Descriptor().FullName()); != nil {
return (, )
}
.StartObject()
defer .EndObject()
if := .marshalFields(); != nil {
return
}
return nil
}
func ( encoder) ( pref.Message) error {
:= .Descriptor()
if !flags.ProtoLegacy && messageset.IsMessageSet() {
return errors.New("no support for proto1 MessageSets")
}
:= .Fields()
for := 0; < .Len(); {
:= .Get()
if := .ContainingOneof(); != nil {
= .WhichOneof()
+= .Fields().Len()
if == nil {
continue // unpopulated oneofs are not affected by EmitUnpopulated
}
} else {
++
}
:= .Get()
if !.Has() {
if !.opts.EmitUnpopulated {
continue
}
:= .Syntax() == pref.Proto2 && .Default().IsValid()
:= .Cardinality() != pref.Repeated && .Message() != nil
= pref.Value{}
}
}
:= .JSONName()
if .opts.UseProtoNames {
if := .marshalExtensions(); != nil {
return
}
return nil
}
func ( encoder) ( pref.Value, pref.FieldDescriptor) error {
switch {
case .IsList():
return .marshalList(.List(), )
case .IsMap():
return .marshalMap(.Map(), )
default:
return .marshalSingular(, )
}
}
func ( encoder) ( pref.Value, pref.FieldDescriptor) error {
if !.IsValid() {
.WriteNull()
return nil
}
switch := .Kind(); {
case pref.BoolKind:
.WriteBool(.Bool())
case pref.StringKind:
if .WriteString(.String()) != nil {
return errors.InvalidUTF8(string(.FullName()))
}
case pref.Int32Kind, pref.Sint32Kind, pref.Sfixed32Kind:
.WriteInt(.Int())
case pref.Uint32Kind, pref.Fixed32Kind:
.WriteUint(.Uint())
case pref.Int64Kind, pref.Sint64Kind, pref.Uint64Kind,
.WriteString(.String())
.WriteFloat(.Float(), 32)
.WriteFloat(.Float(), 64)
case pref.BytesKind:
.WriteString(base64.StdEncoding.EncodeToString(.Bytes()))
case pref.EnumKind:
if .Enum().FullName() == genid.NullValue_enum_fullname {
.WriteNull()
} else {
:= .Enum().Values().ByNumber(.Enum())
if .opts.UseEnumNumbers || == nil {
.WriteInt(int64(.Enum()))
} else {
.WriteString(string(.Name()))
}
}
case pref.MessageKind, pref.GroupKind:
if := .marshalMessage(.Message()); != nil {
return
}
default:
panic(fmt.Sprintf("%v has unknown kind: %v", .FullName(), ))
}
return nil
}
func ( encoder) ( pref.Map, pref.FieldDescriptor) error {
.StartObject()
defer .EndObject()
func ( pref.Kind, []mapEntry) {
sort.Slice(, func(, int) bool {
switch {
case pref.Int32Kind, pref.Sint32Kind, pref.Sfixed32Kind,
pref.Int64Kind, pref.Sint64Kind, pref.Sfixed64Kind:
return [].key.Int() < [].key.Int()
case pref.Uint32Kind, pref.Fixed32Kind,
pref.Uint64Kind, pref.Fixed64Kind:
return [].key.Uint() < [].key.Uint()
}
return [].key.String() < [].key.String()
})
}
var []
.Range(func( pref.FieldDescriptor, pref.Value) bool {
if !.IsExtension() {
return true
}
:= .FullName()
if messageset.IsMessageSetExtension() {
= .Parent()
}
if := .WriteName("[" + . + "]"); != nil {
return
}
if := .marshalValue(., .); != nil {
return
}
}
return nil
![]() |
The pages are generated with Golds v0.3.2-preview. (GOOS=darwin GOARCH=amd64) Golds is a Go 101 project developed by Tapir Liu. PR and bug reports are welcome and can be submitted to the issue list. Please follow @Go100and1 (reachable from the left QR code) to get the latest news of Golds. |